【问题标题】:Get ListItem position in custom SimpleLayout在自定义 SimpleLayout 中获取 ListItem 位置
【发布时间】:2012-01-29 23:15:00
【问题描述】:

我有一个自定义的ListView 和一个TextView 和一个CheckBox。我还有一个自定义的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;
    }
}

感谢任何帮助。

【问题讨论】:

  • 你有参数位置,还需要什么?
  • 对不起,如果我没有正确解释。 position 参数总是返回某个位置,但如果我单击不同的行或复选框,它不会更新。
  • 这很奇怪,你能把整个适配器贴出来吗?
  • 更新为CustomAdapter
  • 仍然不清楚点击位置是什么意思?只能有一个位置给出你的列表项的位置,你还在说什么“位置”?

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


【解决方案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() 实例。

【讨论】:

  • 您应该考虑重用 OnClickListener 和 OnCheckedChangeListener 的实例。每次滚动并重新绘制视图时,您都会分配新的视图。
  • 不错的解决方案!我希望有更优雅的东西,但这很有效。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多