Question

i have a checked text view inside of a listview and whenever I click an item in the list view a random checked text view will check (not neccessarily the one I pressed) Here is my code

lv2.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            final CheckedTextView checkedTextView = (CheckedTextView) findViewById(R.id.checkedTextView1);

            checkedTextView.toggle();

        }
    });

Where lv2 is my list view and checkedTextView1 is my check view inside each listview item. How do I call a specific checkedTextView. Is there an array format that I can call? eg checkedTextView[1].toggle();?

Edit here is my adapter

public class SpecialAdapter2 extends ArrayAdapter<String> {

private int[] colors = new int[] { R.drawable.row_background_grey,
        R.drawable.row_background_white };

public SpecialAdapter2(Context context, int resource, String[] names) {
    super(context, resource, names);
    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    View view = super.getView(position, convertView, parent);

    int colorPos = position % colors.length;
    view.setBackgroundResource(colors[colorPos]);
    return view;
}
Was it helpful?

Solution

Try changing the code in onItemClick() to:

CheckedTextView checkedTextView = (CheckedTextView)arg1.findViewById(R.id.checkedTextView1);
checkedTextView.toggle();

The problem is that you are implicitly calling findViewById() on this - ie. your Activity. Invoking findViewById() on your activity will cause it to search through your entire view hierarchy for the first View it can find with the id checkedTextView1. That's not what you want though - you want to find the specific CheckedTextView in the row item that was clicked. Hence the need to invoke findViewById() on arg1.

OTHER TIPS

try this

final CheckedTextView checkedTextView = (CheckedTextView) arg1;
checkedTextView.toggle();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top