android - 查看 ListView 行内的事件

标签 android view listactivity listeners

大家好,我有一个使用自定义行布局的列表 Activity 。该布局由 2 个标签、一个 ImageView 和一个复选框组成。

我正在尝试在复选框被选中时触发一个监听器。我编写的监听器有效,但不是针对单个行触发,而是针对列表 Activity 中的每一行触发。就像我选中第一行中的复选框一样,对话框将打开并正常工作,但是第二、第三和第四行也会“触发”。

这是我的代码的适当区域:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }
    final item o = items.get(position);
    if (o != null) {
        TextView tt = (TextView) v.findViewById(R.id.name);
        TextView bt = (TextView) v.findViewById(R.id.game);
        CheckBox cb = (CheckBox) v.findViewById(R.id.caught);
        if (cb != null && o.getStatus()) {
            cb.setChecked(true);
        } else {
            cb.setChecked(false);
        }
        if (tt != null) {
            tt.setText(o.getName());
        }
        if (bt != null && o.getStatus()) {
            bt.setText("Game: " + o.getGame());
        }

        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                o.setStatus(isChecked);
                if (isChecked) {
                AlertDialog.Builder builder2 = new AlertDialog.Builder(
                        context);
                builder2.setTitle("What game are we talkin gabout?");
                builder2.setItems(Checklist.GAMES,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int item) {
                                Toast.makeText(context,
                                        Checklist.GAMES[item],
                                        Toast.LENGTH_SHORT).show();
                                o.setGame(Checklist.GAMES[item]);
                                o.setStatus(true);
                                me.notifyDataSetChanged();
                            }
                        });
                builder2.show();
                } else {
                    o.setGame("");
                    o.setStatus(false);
                    me.notifyDataSetChanged();
                }
            }
        });
    }
    return v;

}

是否有更合适的地方为复选框创建我的监听器?

最佳答案

我认为每次用值更新 View 时都分配一个新的 OnCheckedChangeListener 不是一个好的解决方案。最好只在这个新 View 膨胀时将一个听众设置为一个 View 一次。您可以通过将 o-Object 放入 cb 的标记 (setTag(..)) 来使 o-Object 对监听器可用。请注意,当您通过 cb.setChecked() 设置 CB 的状态时,也会触发 onCheckedChanged 事件。

例如:

CheckBox cb = null;
if (v == null) {
    LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
            Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.row, null);
    cb = (CheckBox) v.findViewById(R.id.caught);
    // set a listener
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            item o = (item) buttonView.getTag();
            // show your dialog
        }
    });
}

item o = items.get(position);
if (o != null) {
    TextView tt = (TextView) v.findViewById(R.id.name);
    TextView bt = (TextView) v.findViewById(R.id.game);
    cb = (CheckBox) v.findViewById(R.id.caught);
    cb.setTag(o);
    // set checked state & text
    // ...
}

关于android - 查看 ListView 行内的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559941/

相关文章:

android - fragment 中的 Facebook 登录

android - 使用 ItemTouchHelper 滑动到 recyclerView 中的另一个 View

c# - 在 Xamarin 应用程序中将项目添加到 Android 上的 ListView

Android - ListActivity,添加页眉和页脚 View

android - 设备关闭时 Android TV 应用程序会关闭吗?

java - DJI Phantom 3 自定义任务 App,任务步骤之间存在延迟 :

ios - TableView Controller 的奇怪行为

android - 如何禁用Android手机的触摸屏?

java - 遍历java中的对象数组

android - 从 Canvas 上的单个位图绘制多次