2

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

4 Answers 4

11

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());
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I implemented this and got a toast to display stating the item position. Could you explain this array of checkboxes? The problem I am having now is transferring a clicked item to check the row's checkbox
sure, i have been away from my computer for a few hours but hopefully this is still helpful, Whenever you instantiate your checkboxes, if its in code, keep a reference to them in an array, if its in XML you will have to do findViewById for each one. If yo uneed more info i can check back in the morning.
1

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();

        }
    });

1 Comment

Also how to toast corresponding a column value ?, Here is a only toast position, then how to toast that positioned column valu
0

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?

1 Comment

I don't think this is an answer, but am not certain.
0

You can access all the elements of your listview row using View parameter of OnItemClick() method. e.g.

    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;
    });

1 Comment

Please also try to explain your solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.