当我滚动列表时。然后点击任何视图。我的位置错了

时间:2013-05-15 06:33:26

标签: android listview

这里我正在维护一个包含按钮和列表的列表视图。编辑框。但是当我滚动列表然后我在任何按钮上执行onclick操作时。它表现错误。就像我点击第3行然后选择在第二行执行。那怎么能处理这个

适配器代码

public class ItemAdapter extends ArrayAdapter<ItemData> {
        Context context;
        int layoutResourceId;
        ArrayList<ItemData> data = null;
        ViewHolder holder = null;
        private int selectedPos = -1; // init value for not-selected

        public ItemAdapter(Context context, int layoutResourceId, ArrayList<ItemData> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }

        public void setSelectedPosition(int pos) {

            selectedPos = pos;
            System.out.println("MainActivity.ItemAdapter.setSelectedPosition()-----"+selectedPos);
            // inform the view of this change
            notifyDataSetChanged();
        }

        public int getSelectedPosition() {
            return selectedPos;
        }

        public View getView(int position, final View convertView, final ViewGroup parent) {
            View row = convertView;

            if (row == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);

                holder = new ViewHolder();
                holder._itemName = (TextView) row.findViewById(R.id.item_name);
                holder._itemPrice = (TextView) row.findViewById(R.id.item_price);
                holder._list_image = (ImageView) row.findViewById(R.id.list_image);
                holder._plusButton = (Button) row.findViewById(R.id.plusButton);
                holder._minusButton = (Button) row.findViewById(R.id.minusButton);
                holder._cancelButton = (Button) row.findViewById(R.id.cancel);
                holder._quantityEditText = (TextView) row.findViewById(R.id.quantityEditText);
                holder._radio1 = (RadioButton) row.findViewById(R.id.firstRadio);
                holder._radio2 = (RadioButton) row.findViewById(R.id.secondRadio);
                holder._radio3 = (RadioButton) row.findViewById(R.id.thirdRadio);
                System.out.println("MainActivity.ItemAdapter.getView()-----"+selectedPos);

                    holder._plusButton.setContentDescription(position + "");
                    holder._minusButton.setContentDescription(position + "");

            }
            // } else {
            // holder = (ViewHolder) row.getTag();
            // }
            final ItemData item_data = data.get(position);
            System.out.println("MainActivity.ItemAdapter.getView()--position---"+position);
            System.out.println("MainActivity.ItemAdapter.getView()--------name------" + item_data.ItemNme);
            holder._itemName.setText(item_data.ItemNme);
            holder._plusButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    View singleItemView = mainListView.getChildAt(Integer.parseInt(v.getContentDescription().toString()));
                    TextView editText = (TextView) singleItemView.findViewById(R.id.quantityEditText);
                    TextView priceTaxt = (TextView) singleItemView.findViewById(R.id.item_price);
                    int itemQuantity = (Integer.parseInt(editText.getText().toString())) + 1;
                    editText.setText(String.valueOf(itemQuantity));
                    editText.invalidate();
                    RadioButton button1 = (RadioButton) singleItemView.findViewById(R.id.firstRadio);
                    RadioButton button2 = (RadioButton) singleItemView.findViewById(R.id.secondRadio);
                    RadioButton button3 = (RadioButton) singleItemView.findViewById(R.id.thirdRadio);
                    if (!button1.isChecked() && !button3.isChecked())
                        button2.setChecked(true);
                }
            });

            holder._minusButton.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    View singleItemView = mainListView.getChildAt(Integer.parseInt(v.getContentDescription().toString()));
                    TextView editText = (TextView) singleItemView.findViewById(R.id.quantityEditText);
                    TextView priceTaxt = (TextView) singleItemView.findViewById(R.id.item_price);
                    int itemQuantity = (Integer.parseInt(editText.getText().toString()));
                    if (itemQuantity != 0) {
                        itemQuantity = itemQuantity - 1;
                    }
                    editText.setText(String.valueOf(itemQuantity));
                    editText.invalidate();
                    RadioButton button1 = (RadioButton) singleItemView.findViewById(R.id.firstRadio);
                    RadioButton button2 = (RadioButton) singleItemView.findViewById(R.id.secondRadio);
                    RadioButton button3 = (RadioButton) singleItemView.findViewById(R.id.thirdRadio);
                    if (itemQuantity == 0) {
                        button1.setChecked(false);
                        button2.setChecked(false);
                        button3.setChecked(false);
                    }
                }
            });

            row.setTag(holder);
            return row;
        }

    }

1 个答案:

答案 0 :(得分:0)

此代码中的问题是,在某些情况下,当row不为null时,holder不会使用当前元素的数据进行初始化,并且侦听器将设置在前一个元素或下一个元素的持有者上。您需要重构代码以解决问题。