deathnote
deathnote

Reputation: 95

how to know the check box value of a particular row of a listview

hey how can i know that the below checkbox when clicked is from which row of the list. is there any way of knowing that. Every time yo is returning false which is its default value.

checkBox.setOnClickListener( new View.OnClickListener() {
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v ;
                        callBlockOptions callBlockOptions = (callBlockOptions) cb.getTag();

                        callBlockOptions.setChecked( cb.isChecked() );

                        String yo;

                        if(callBlockOptions.getPosition()=="0")
                        {
                            callBlockOptions.setAllCalls();
                        }


                        if(callBlockOptions.getAllCalls() ==true)
                            yo="true";
                        else
                            yo="false";

                        Toast.makeText(getContext(), callBlockOptions.getPosition(), Toast.LENGTH_LONG).show();

                    }
                });        
            }

Upvotes: 0

Views: 52

Answers (1)

Jared Price
Jared Price

Reputation: 5375

After seeing your code, one thing you might want to try is setting listeners for the individual children within the ListView. You could do something like this:

ListView listView = (ListView)findViewById(R.id.listView);
int count = listView.getChildCount();
for (int x = 0; x < count; x++)
{
    Class<? extends View> c = listView.getChildAt(x).getClass();
    if (c == CheckBox.class)
    {
        CheckBox checkBox = (CheckBox)(listView.getChildAt(x));
        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton compoutButton, boolean isChecked) {
                // TODO Auto-generated method stub
                callBlockOptions.isChecked = isChecked;
            }
        });
    }
    else if (c == TextView.class)
    {
        TextView textView = (TextView)(listView.getChildAt(x));
        textView.setOnEditorActionListener(new OnEditorActionListener(){

            @Override
            public boolean onEditorAction(TextView tView, int arg1, KeyEvent arg2) {
                // TODO Auto-generated method stub
                callBlockOptions.name = tView.getText().toString();
                return true;
            }

        });
    }

You probably want your other class to be like this:

public class callBlockOptions {
     public static String name;
     public static Boolean isChecked;
}

Then you can get and set name and isChecked like this:

callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;

Upvotes: 1

Related Questions