java - 获取 ListItem 在自定义 SimpleLayout 中的位置

标签 java android listview android-layout android-adapter

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

UPDATE 添加了整个 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;
    }
}

感谢任何帮助。

最佳答案

我找到了一个解决方案。如果有人知道更好的,请告诉我。工作 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() 实例。

关于java - 获取 ListItem 在自定义 SimpleLayout 中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057673/

相关文章:

java - 从 Gradle 添加 URL 到 BuildConfig 文件

Android 共享 Intent EXTRA_STREAM

Android ListView如何禁用多点击

android - 在android自定义 ListView 中重复列出项目

java - Android 应用内购买 NullPointerException

java - 没有与文件名或扩展名 "true"关联的序列化程序?

Java 字符串方形图案

java - 在java中打印N维数组

java - 如何在 web 服务响应后反转 json 数组?

android - 在项目上单击监听器以扩展 ArrayAdapter 的 ListView