-1

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.

ClaireG
  • 1,244
  • 2
  • 11
  • 23
  • http://stackoverflow.com/questions/18162931/android-get-selected-item-using-checkbox-in-listview-when-i-click-a-button/18164177#18164177. not sure if i understand your requirement clearly – Raghunandan Aug 22 '13 at 12:37
  • Not very clear - what text you want to return? The listview item data or the checkbox state? – Vrashabh Irde Aug 22 '13 at 12:38
  • Confirm which type of "View" are you getting in onClick(final View v). Is it the CheckBox you are expecting ? – binaryKarmic Aug 22 '13 at 12:39
  • The text I want to get is the text assigned to the `CheckBox` – ClaireG Aug 22 '13 at 12:41
  • @jagz87 It is a `CheckBox`, at least while hovering on v @ `cb = (CheckBox)` while in debugging, it shows that v is a `CheckBox`. – ClaireG Aug 22 '13 at 12:42
  • Well.. v.findViewById(R.id.idcheckbox) could help you!! – Tizianoreica Aug 22 '13 at 12:43
  • Yes it is a String, of course – ClaireG Aug 22 '13 at 12:44
  • @AntonioCalì Please be more clear. How am I suppose to get the assigned `CheckBox` text using that? – ClaireG Aug 22 '13 at 12:46
  • Well I suppose your checkbox is inside a custom layout for every child of your list view, doesn't? If so, with v.findViewById you will obtain a reference to that checkbox – Tizianoreica Aug 22 '13 at 12:48
  • @AntonioCalì Yes, left it out as usual. Anyway, it is still not getting the text. Updated original post. – ClaireG Aug 22 '13 at 12:51
  • @ClaireG you are creating a new checkbox object which have no text set for it so the returned string will be empty. – Siddhesh Aug 22 '13 at 12:54
  • Whatever for ListView you should implements a AdapterView.OnItemClickListener with onItemClick method.. http://developer.android.com/reference/android/widget/AdapterView.html#setOnClickListener%28android.view.View.OnClickListener%29 – Tizianoreica Aug 22 '13 at 12:55
  • @Siddhesh You are right, but I have no other choice since `v.getText()` is not allowed. Would you suggest anything else? – ClaireG Aug 22 '13 at 13:00
  • @ClaireG can you paste proper code and frame your question according to whats not working in your code so that everyone here can help you – Siddhesh Aug 22 '13 at 13:02

2 Answers2

1

Use

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

to get the reference for CheckBox in the list.

Ameer Moaaviah
  • 1,530
  • 12
  • 27
  • The pleasure is all mine. I just put down the reference and left the implementation for you to try. I'm glad that you solved it. – Ameer Moaaviah Aug 22 '13 at 14:50
0
@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() ;
         }
        });
        ......
}
Siddhesh
  • 1,370
  • 11
  • 28
  • what are you trying to do in this line? holder.chk.setOnClickListener(new MyClickListener(holder.chk)); – Siddhesh Aug 22 '13 at 13:15
  • @Edit if I do that the last item in the list gets checked all the time, no matter which one I choose. That is why I created a separate class for the OnClickListener so I can reference the `CheckBox` accordingly. – ClaireG Aug 22 '13 at 13:28