Can someone point give me an example of a On item click listener used for a list view with base adapter?
My list view contains two text views and a check box. I want to create an on item click listener so that when a item (or row) is pressed, it ticks the corresponding row check box.
I tried to Google it but could not find any examples of it
Thank you
So, the adapter does not really matter, but i believe what you are trying to do is pretty simple, first you need to get a refrence to your ListView
which i will refer to as listView
after setting your adapter you can use setOnItemClickListener
to create the click action,
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public boolean onItemClick(AdapterView<?> parent, View view, int position, long id) {
//here you can use the position to determine what checkbox to check
//this assumes that you have an array of your checkboxes as well. called checkbox
checkBox[position].setChecked(!checkBox.isChecked());
}
});
It's actually quite simple, after you perform the action "setAdapter" You have to do this:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You click on position:"+position, Toast.LENGTH_SHORT).show();
}
});
The following code works correctly:
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Toast.makeText(getApplicationContext(),
"You click on position:"+position, Toast.LENGTH_SHORT).show();
}
});
But when I clicked items
then it goes to other activities. What should I do?
You can access all the elements of your listview row using View parameter of OnItemClick() method. eg
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// second parameter View view entire row object that is clicked - in your case
// two text views and the checkbox
// supposing your checkbox is set up with an id in the xml as idchkbox
// You can access it by findview by id and set it true or false;
android.widget.CheckBox chkbox = view.findViewbyId(R.id.idchkbox);
chkbox.setChecked = true;
return;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.