我有一个的ListView
,其中列表中的每个元素都包含一个TextView和两个不同的按钮。事情是这样的:
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]
--------------------
[ImageView][Text][CheckBox][Button]
--------------------
... (and so on) ...
通过这个code,我可以创建一个 OnItemClickListener
整个项目:
With this code I can create an OnItemClickListener
for the whole item:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> list, View view, int position, long id) {
Log.i(TAG, "onListItemClick: " + position);
}
}
});
不过,我不希望整个项目可以点击,但只有复选框,每个列表元素的按钮。
However, I don't want the whole item to be clickable, but only the checkbox and the button of each list element.
所以我的问题是,如何实现一个onClickListener为这两个按钮具有下列参数:
So my question is, how do I implement a onClickListener for these two buttons with the following parameters:
内部ID
(有些ID在列表中每个项目相关联的)
INT位置
(这是在其上单击按钮发生了列表中的元素)
int id
(some id associated with each item in list)
int position
(which is the element in the list on which the button click happened)
您需要baseAdpter实现这个
you need to make baseAdpter to achieve this
public class ContactsAdapter extends BaseAdapter {
ArrayList<ContactInfo> mlist;
Context mcontext;
public BluetoothChatadpter(Context context,ArrayList<ChatInfo> mchtlist) {
mlist = mchtlist;
mcontext = context;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int postion) {
return mlist.get(postion);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertview, ViewGroup viewgroup){
View view = null;
if(convertview == null){
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.contactrow, null);
ContactHolder holder = new ContactHolder();
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(new OnClickListener() {
@Override
public void onClick(View arg0) {
// to open the selected file in resp
// do your work here
}});
chkselected .setOnClickListener(new OnClickListener() {
@Override
public void onClick(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;
}
ContactHolder holder2 = (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;
}
}