3

我有 aListViewTextViewa的习惯CheckBox。我还有一个自定义方法,SimpleAdapter我在其中覆盖了该getView()方法并能够检索对TextViewCheckBox更改的点击。我的问题是我不知道如何在 or 内获得正确ListItemCheckBox点击OnCheckedChangedOnClick

更新增加了整个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;
    }
}

任何帮助表示赞赏。

4

2 回答 2

1

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

于 2012-02-01T19:33:50.567 回答
0

OP的解决方案有效。但是如果你在扩展一个 CursorAdapter,那么 newView() 和 bindView() 中就不存在 position 参数。该怎么办?

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

于 2012-03-27T03:07:25.283 回答