Question

I have a ListView with some CheckBoxes. When I click on each row of the ListView I want to save the CheckBox's text in a String.

I set the ListView row to not focusable so that the user can click on the CheckBox itself. The CheckBox cannot be checked or unchecked by the user himself, but instead, when the user clicks on it, he is lead to another activity where he needs to input data. I am checking whether the user is inputing data or not by returning a boolean value.

If the value is true, I need to set the checkbox to checked. In order to do so I need to compare the text of the CheckBox to another String value before actually checking it.

In other words the checked state of each CheckBox is determined by whether or not the user entered data in the previous activity.

What I cannot do is get the CheckBox text from the clicked/ touched/ selected CheckBox

Why are the below code snippets not working? clickedError is empty after running this code.

@Override
    public void onClick(final View v) 
    {
        CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelected);
        clickedError = cb.getText().toString() ;

AND

@Override
public void onClick(final View v) 
{
    CheckBox cb = (CheckBox) v;
    clickedError = cb.getText().toString() ;
}

Have already been tried

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{   
        ......
        holder.chk.setOnClickListener(new MyClickListener(holder.chk));
        ......
}


private class MyClickListener implements OnClickListener
{
    CheckBox checkbox = null;

    public MyClickListener(CheckBox cb)
    {
        checkbox = cb;
    }
    @Override
    public void onClick(final View v) 
    {
//          CheckBox cb = new CheckBox(mContext);
//          cb = (CheckBox) v;
//          clickedError = cb.getText().toString() ;

        CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelected);
        clickedError = cb.getText().toString() ;

Thanks.

Was it helpful?

Solution

Use

ListView.onListItemClick(ListView parent, View row, int position, long id); 

to get the reference for CheckBox in the list.

OTHER TIPS

@Override
    public void onClick(final View v) 
    {
        CheckBox cb = (CheckBox) v;
        clickedError = cb.getText().toString() ;
    }

try this.

Edit : in your new code what are you trying to do with this

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{   
        ......
        holder.chk.setOnClickListener(new MyClickListener(holder.chk));
        ......
}

cant you do it like this for eg:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{   
        ......
        holder.chk.setOnClickListener(new OnClickListener(){
         public void onClick(View v){
            CheckBox cb = (CheckBox) v;
            clickedError = cb.getText().toString() ;
         }
        });
        ......
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top