在自定义SimpleLayout中获取ListItem位置

时间:2012-01-29 23:15:01

标签: java android listview android-layout android-adapter

我的自定义ListView包含TextViewCheckBox。我还有一个自定义SimpleAdapter,其中我覆盖了getView()方法,并且可以检索TextViewCheckBox更改的点击次数。我的问题是,我不知道如何在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;
    }
}

感谢任何帮助。

2 个答案:

答案 0 :(得分: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()实例。

答案 1 :(得分:0)

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

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