获取自定义 SimpleLayout 中 ListItem 的位置

发布于 2024-12-29 19:38:43 字数 2276 浏览 5 评论 0原文

我有一个带有 TextViewCheckBox 的自定义 ListView。我还有一个自定义的 SimpleAdapter,其中我重写了 getView() 方法,并且能够检索对 TextView 和 CheckBox 的点击改变。我的问题是我不知道如何在 OnCheckedChangedOnClick 内获取正确的点击 ListItemCheckBox >。

更新添加了整个CustomAdapter类:

public class CustomAdapter extends SimpleAdapter {
    private LayoutInflater mInflater;
    private List<HashMap<String, String>> mItems = null;
    private Context mContext;
    private int mPosicion;

    public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
        super(context, items, resource, from, to);
        mInflater = LayoutInflater.from(context);
        mItems = items;
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
        ViewHolder holder;
        mPosicion = position;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row_view, null);

            holder = new ViewHolder();
            holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
            holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
            }
        });

        holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i("Posicion",""+mPosicion);//Or checked
            }
        });

        return convertView;

    }


    private static class ViewHolder {
        TextView txtTextoAgenda;
        CheckBox chkbxEstado;
    }
}

任何帮助表示赞赏。

I have a custom ListView with a TextView and a CheckBox. I also have a custom SimpleAdapter in which I override the getView() method and am able to retrieve clicks on the TextView and CheckBox changes. My problem is that I don't know how to get the correct clicked ListItem or CheckBox inside the OnCheckedChanged or OnClick.

UPDATE added whole CustomAdapter class:

public class CustomAdapter extends SimpleAdapter {
    private LayoutInflater mInflater;
    private List<HashMap<String, String>> mItems = null;
    private Context mContext;
    private int mPosicion;

    public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
        super(context, items, resource, from, to);
        mInflater = LayoutInflater.from(context);
        mItems = items;
        mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
        ViewHolder holder;
        mPosicion = position;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row_view, null);

            holder = new ViewHolder();
            holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
            holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Posicion",""+mPosicion);//I don't know how to retrieve clicked position
            }
        });

        holder.chkbxEstado.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i("Posicion",""+mPosicion);//Or checked
            }
        });

        return convertView;

    }


    private static class ViewHolder {
        TextView txtTextoAgenda;
        CheckBox chkbxEstado;
    }
}

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬 2025-01-05 19:38:43

我找到了解决方案。如果有人知道更好的,请告诉我。工作 CustomAdapter 类:

    public class CustomAdapter extends SimpleAdapter {
        private LayoutInflater mInflater;
        private List<HashMap<String, String>> mItems = null;
        private Context mContext;

        private OnClickListener mClick;
        private OnCheckedChangeListener mChecked;

        public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
            super(context, items, resource, from, to);
            mInflater = LayoutInflater.from(context);
            mItems = items;
            mContext = context;
        }

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

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_row_view, null);

                holder = new ViewHolder();
                holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
                holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
                holder.posicion = position; //Add the new position into the holder for each row.

                convertView.setTag(holder);

                mClick = new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row.
                        Log.i("Posicion",""+v.posicion);
                    }
                };

                mChecked = new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox
                        Log.i("Posicion",""+viewHolder.posicion);
                    }
                };

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));

            convertView.setOnClickListener(mClick);

            holder.chkbxEstado.setOnCheckedChangeListener(mChecked);

            return convertView;

        }

        public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag.
            if(v.getTag() == null){
                return getViewHolder((View)v.getParent());
            }
            return (ViewHolder)v.getTag();
        }

        private static class ViewHolder {
            TextView txtTextoAgenda;
            CheckBox chkbxEstado;
            int posicion; //Added position attribute to ViewHolder class.
        }
    }

编辑以重用 onClickListener() 和 onCheckedChangedListener() 实例。

I found a solution. If anybody knows a better one, please let me know. Working CustomAdapter class:

    public class CustomAdapter extends SimpleAdapter {
        private LayoutInflater mInflater;
        private List<HashMap<String, String>> mItems = null;
        private Context mContext;

        private OnClickListener mClick;
        private OnCheckedChangeListener mChecked;

        public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
            super(context, items, resource, from, to);
            mInflater = LayoutInflater.from(context);
            mItems = items;
            mContext = context;
        }

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

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_row_view, null);

                holder = new ViewHolder();
                holder.chkbxEstado = (CheckBox) convertView.findViewById(R.id.chkbxCompletado);
                holder.txtTextoAgenda = (TextView) convertView.findViewById(R.id.txtTextoLista);
                holder.posicion = position; //Add the new position into the holder for each row.

                convertView.setTag(holder);

                mClick = new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ViewHolder viewHolder = getViewHolder(v); //Get the ViewHolder for the clicked row.
                        Log.i("Posicion",""+v.posicion);
                    }
                };

                mChecked = new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        ViewHolder viewHolder = getViewHolder(buttonView); //Get the ViewHolder for the clicked CheckBox
                        Log.i("Posicion",""+viewHolder.posicion);
                    }
                };

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtTextoAgenda.setText(mItems.get(position).get("descripcion"));

            convertView.setOnClickListener(mClick);

            holder.chkbxEstado.setOnCheckedChangeListener(mChecked);

            return convertView;

        }

        public ViewHolder getViewHolder(View v){ //This method returns the ViewHolder stored in the tag if available, if not it recursively checks the parent's tag.
            if(v.getTag() == null){
                return getViewHolder((View)v.getParent());
            }
            return (ViewHolder)v.getTag();
        }

        private static class ViewHolder {
            TextView txtTextoAgenda;
            CheckBox chkbxEstado;
            int posicion; //Added position attribute to ViewHolder class.
        }
    }

Edited to re-use onClickListener() and onCheckedChangedListener() instances.

开始看清了 2025-01-05 19:38:43

OP的解决方案有效。但是,如果您要扩展 CursorAdapter,则 newView() 和 bindView() 中不存在位置参数。该怎么办?

他们确实提供了一个游标。使用cursor.getPosition()。它做同样的事情。

The OP's solution works. But if you are extending a CursorAdapter, the position parameter does not exist in newView() and bindView(). What to do?

They do supply a Cursor. Use cursor.getPosition(). It does the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文