开发者

copying view objects

开发者 https://www.devze.com 2023-01-13 07:04 出处:网络
I have a ListView that I am populating with LinearLayouts that contain one CheckBox apiece.The LinearLayout\'s are added dynamically for each object I am returning from a web service I have.

I have a ListView that I am populating with LinearLayouts that contain one CheckBox apiece. The LinearLayout's are added dynamically for each object I am returning from a web service I have. I need for a user to be able to click a done button and for the application to go through the list of CheckBox's and update a database for each checked item. The database and the done button were a piece of cake, but obtaining a reference to or making a copy, how ever you choose to look at it, of the CheckBox thus far has stumped me for days.

my code as of now for this process looks something like this:

private ListView List = (ListView)findViewById(R.id.list);

LinearLayout temp; CheckBox curr;

for ( int i = 0; i< List.getChildCount(); i++) { temp = (LinearLayout)List.getChildAt(i); curr = (CheckBox)temp.getChildAt(1); if ( curr.isChecked() ) { //do stuff here } }

as soon as I try and reference either the temp object or the curr object the program crashes. So in the above code it crashes at curr.isChecked(). I discovered previously that if you forget that the CheckBox is wrapped in a LinearLayout and try to copy a LinearLayout object to a CheckBox object the program crashes, so I know the system sees the correct object to copy, even at the 开发者_StackOverflow中文版curr copy... however if i try to log the temp class type like this Log.d("temp Class type", temp.getClass().toString()); the system crashes

However the following works perfectly fine... Log.d("List Class type", List.getChildAt(i).getClass().toString());

The only thing I can come up with at this point is that I need to overload a copy construct somewhere, but I can not find anything that advises you on how to begin this or what the copy function might be called. Even google code searches have proved fruitless for me...

I have a feeling this is just something I am looking at completely 180 degrees the wrong way and it is going to be ridiculously simple, but if there were a way to rip the emulator off the screen and throw it through the wall I would have done it by now, please help!

-David

...and thanks in advance.


The Java language works by reference, when you write Checkbox curr = [an expression of type checkbox], you are not making a copy of the Checkbox object. To help you with your issue, it would be very helpful to see the stack trace of the crash (you'll find it in the logs.)

0

精彩评论

暂无评论...
验证码 换一张
取 消