开发者

how to access checked text view in a list view?

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开发者_JAVA百科 (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;
}


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.


try this

final CheckedTextView checkedTextView = (CheckedTextView) arg1;
checkedTextView.toggle();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜