0

我在列表视图中有一个选中的文本视图,每当我单击列表视图中的一个项目时,都会检查一个随机选中的文本视图(不一定是我按下的那个)这是我的代码

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();

        }
    });

其中 lv2 是我的列表视图,而 checkedTextView1 是我在每个列表视图项中的检查视图。如何调用特定的checkedTextView。有我可以调用的数组格式吗?例如checkedTextView[1].toggle();

编辑这里是我的适配器

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;
}
4

2 回答 2

1

尝试将 onItemClick() 中的代码更改为:

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

问题是你隐含地调用findViewById()这个-即。你的活动。调用findViewById()您的活动将导致它在整个视图层次结构中搜索它可以找到的第一个视图,其 id 为checkedTextView1。但这不是您想要的 - 您想在单击的行项目中找到特定的 CheckedTextView。因此需要findViewById()在 arg1 上调用。

于 2011-08-19T18:13:21.310 回答
1

尝试这个

final CheckedTextView checkedTextView = (CheckedTextView) arg1;
checkedTextView.toggle();
于 2012-01-13T18:12:09.667 回答