Event handle on customized ListView
public class ActivityForShow extends Activity
{
//have a ListView layout
ListView.setAdapter(SingleRowAdapter);
}
public class SingleRowAdapter extends ArrayAdapter
{
//every row has a CheckBox ,a TextView and a Button
}
Now I want to handle CheckBox and Button events,do some background work and show result in a dialog.
But开发者_如何转开发 how to getRefences of Checkbox and Button in ActivityForShow? findViewById() will cause Exception.I don't think you want to do that in the ActivityForShow class. Instead, register all event handlers inside of your SingleRowAdapter#getItem(int) method (you will have have to override this). In that method you create the view for the given row, so you know what the row is (position) and you can register event handlers for the CheckBox, Button, etc
It sounds like you want to handle the onCheckedChanged
event or onClick
event in ActivityForShow
.
If so, have ActivityForShow
create a OnCheckedChangeListener
instance and OnClickListener
instance (e.g. as local anonymous inner classes) and have ActivityForShow
give them to SingleRowAdapter
(e.g. as constructor params) to attach to the checkbox and button widgets as their callbacks.
Alternatively, define your own custom handler interface(s) to be called when the button is clicked or the checkbox is checked. Have ActivityForShow
to instantiate an instance of the custom interface(s) with code to process the event. Have ActivityForShow
give them to SingleRowAdapter
. Finally, have SingleRowAdapter
create event handlers to attach to the checkboxes and buttons - these event handlers simply call your own custom handlers.
Hope the following link may help you, event on an item of ListView custom row layout.
It's obvious it gives error with finviewbyid method because it is used for when reference by xml ele. You can use this link for study:
http://developer.android.com/reference/android/widget/ArrayAdapter.html
精彩评论