1

I've a listView inside a FrameLayout Activity (not list Activity).

I can listen to clicks using checkbox.setOnCheckedChangeListener(this)

How do I determine which checkbox was clicked? I dont get any index.

ps OnListItemClicked is out of scope since I'm not using ListActivity

4 Answers 4

1

You can implement onListItemClickListener(). Iterate all the listview items and check each item (each item child view).

for(int i = 0; i  <= listView.getLastVisiblePosition(); i++)
    {
        if(listView.getChildAt(i)!= null)
        { 
            if(((CheckBox)listView.getChildAt(i).findViewById(R.id.checkbox)).isChecked())
            {  
          //do something
             }else { //do something else}

        }
    }

or you can check in getView() method if the item checkbox is checked or not.

Sign up to request clarification or add additional context in comments.

3 Comments

if(((CheckBox)listView.getChildAt(i).findViewById(R.id.checkbox)).isChecked()) you can simplify this to View parentView = listView.getChildAt(i); CheckBox chkbox = (CheckBox)parentView.findViewById(R.id.checkbox); if(chkbox.isChecked())
I meant I wouldn't want to loop over all the children, There should be a better way to do it.
There is ofcourse,in getView() method in your Adapter, there you can check.
1

do something like this

Vector vect = new Vector();
lv1.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, item));
    lv1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lv1.setClickable(true);
    lv1.setScrollingCacheEnabled(true); 
    lv1.setOnItemClickListener(deleteclicklistener);


private OnItemClickListener deleteclicklistener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position,
            long id) {
        if(vect.contains(position))
        {
            vect.removeElement(position);
        }else
        {
            vect.addElement(position);
        }
    }
};

I think this should solve your problem

1 Comment

Line item should be separately clickable. Setting clickable to True makes checkbox + line item one clickable thing.
0

Try using listView1.getCheckedItemPosition(). You could also try doing this:

private int checkedItemPosition;

listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
    public void onItemClick(AdapterView<?> listView, View selectedItem, int position, long itemId)
    {
        CheckBox checkbox = (CheckBox)selectedItem.findViewById(R.id.checkbox);
        if (checkbox.isChecked())
        {
            checkedItemPosition = position;
        }
    }
});

Comments

0

ListView still has a setOnListItemClickedListener() method you can just create your listener as an object and pass it in, rather than implementing it on your Activities context.

Edit: Yes it does, it gets inherited from AdapterView.

Go to ListView docs scroll down to the Inherited methods section and click the arrow to the left of AdapterView you'll find setOnItemClickListener(). I have used it in code before I assure that it is there.

Comments

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.