Android: Listview Elements With Multiple Clickable Elements
I've a ListView where every element in the list contains a TextView and two different Buttons. Something like this: ListView -------------------- [ImageView][Text][CheckBox][Button
Solution 1:
you need to make baseAdpter to achieve this
publicclassContactsAdapterextendsBaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
publicBluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
@OverridepublicintgetCount() {
return mlist.size();
}
@Overridepublic Object getItem(int postion) {
return mlist.get(postion);
}
@OverridepubliclonggetItemId(int position) {
return position;
}
@Overridepublic View getView(int position, View convertview, ViewGroup viewgroup){
Viewview=null;
if(convertview == null){
LayoutInflaterinflater= context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolderholder=newContactHolder();
holder.txtviewfirstname = (TextView)view.findViewById(R.id.firstname);
holder.txtviewphone = (TextView)view.findViewById(R.id.phone);
holder.chkselected = (CheckBox)view.findViewById(R.id.check);
setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View arg0) {
// to open the selected file in resp// do your work here
}});
chkselected .setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
// Toast.makeText(context,// "checked is clicke="+pos, 12).show();if (chkselected.isChecked())
{
// do your work here
} else {
// do your work here
}
}
});
view.setTag(holder);
}
else{
view = convertview;
}
ContactHolderholder2= (ContactHolder) view.getTag();
holder2.txtviewfirstname.setText(list.get(position).firstname);
holder2.txtviewphone.setText(list.get(position).phonenumber);
holder2.chkselected.setChecked(list.get(position).selected);
return view;
}
}
Solution 2:
i solved the same problem in this way: in the layout.xml of the ListViewItem give the elements a tag, where you want to add the OnClickListener:
android:id="@+id/CheckBox"
android:tag="CheckBox" />
....
android:id="@+id/Button"
android:tag="Button" />
In the code, where your ListView is inflated add the OnClickListener with the following code:
listView.findViewWithTag("CheckBox").setOnClickListener(createCheckBoxOnClickListener(act));
listView.findViewWithTag("Button").setOnClickListener(createButtonOnClickListener(act));
Solution 3:
You must do this in the getView
method in your adapter..
Solution 4:
easy, just implement the OnClickListener
for these two buttons, and get the position that you get from getView()
, make sure you set the position to final
in order to get it in OnClickListener
Solution 5:
you can add listeners in your getView() method of Adapter.
Post a Comment for "Android: Listview Elements With Multiple Clickable Elements"