混乱ListView(Android)中的标签

时间:2013-02-14 13:35:28

标签: android listview baseadapter custom-adapter

我正在尝试实现一个片段,该片段包含绑定到自定义适配器的ListView。 基本上,一行看起来像这样:TextViewName ButtonPlus TextViewMarker ButtonMinus ButtonRemove 按钮加号和减号改变TextViewMarker显示的数字。 ButtonRemove从列表中删除一行。最后,有一个按钮可以在列表中添加新项目。 一切似乎工作正常,因为我可以添加和删除项目并在运行时更改标记的数量。不幸的是,如果我删除一个项目,列表中的下一个项目将获取已删除项目的标记数量。就像ViewHolder仍然在同一个地方,而下一个项目只是改变了他的标签。当列表为空时我遇到同样的问题,如果我添加一个新项目,它将显示列表中最后一个项目的标记数...

以下是代码:

public class MarkerFragment extends Fragment
{
    private MarkerAdapter mAdapter = null;
    private EditText mAddItemEditText = null;
    private MainActivity mParent = null;

    public MarkerFragment(MainActivity parent){mParent = parent;}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_marker, null);

        Button b = (Button) view.findViewById(R.id.buttonAddItem);
        b.setOnClickListener(new ClickListener(this));

        mAddItemEditText = (EditText) view.findViewById(R.id.editTextItem);

        mAdapter = new MarkerAdapter(this, mParent);
        ListView listView = (ListView) view.findViewById(R.id.markerList);
        listView.setAdapter(mAdapter);

        return view;
    }

    public void addItem(String name)
    {
        mAdapter.addItem(name);
    }

    public void removeItem(int position)
    {
        mAdapter.removeItem(position);
    }

    static class ViewHolder
    {
        TextView name;
        TextView markers;
        int position = -1;
    }

    private class MarkerAdapter extends BaseAdapter
    {
        private MarkerFragment mParent;
        private LayoutInflater mInflater;
        private ArrayList<String> mList;

        public MarkerAdapter(MarkerFragment parent, Context context)
        {
            super();

            this.mParent = parent;
            mInflater = LayoutInflater.from(context);

            mList = new ArrayList<String>();
            mList.add("item1");
            mList.add("item2");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder;

            if(convertView == null)
            {
                convertView = mInflater.inflate(R.layout.marker_row, null);
                holder = new ViewHolder();

                holder.name = (TextView) convertView.findViewById(R.id.textViewRowName);
                holder.markers = (TextView) convertView.findViewById(R.id.textViewMarker);

                holder.markers.setText("0");

                Button b = (Button) convertView.findViewById(R.id.buttonPlus1Marker);
                b.setRotation(180);
                b.setOnClickListener(new ClickListener(this.mParent));

                b = (Button) convertView.findViewById(R.id.buttonMinus1Marker);
                b.setOnClickListener(new ClickListener(this.mParent));

                b = (Button) convertView.findViewById(R.id.buttonRemoveMarker);
                b.setOnClickListener(new ClickListener(this.mParent));

                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.name.setText(mList.get(position));
            holder.position = position;

            return convertView;
        }

        public void addItem(String name)
        {
            mList.add(name);
            this.notifyDataSetChanged();
        }

        public void removeItem(int position)
        {
            mList.remove(position);
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount()
        {
            return mList.size();
        }

        @Override
        public Object getItem(int position)
        {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }
    }

    private class ClickListener implements OnClickListener
    {
        private StringBuilder sb = new StringBuilder();
        private MarkerFragment mParent = null;

        public ClickListener(MarkerFragment parent)
        {
            mParent = parent;
        }

        @Override
        public void onClick(View v)
        {
            switch(v.getId())
            {
            case R.id.buttonAddItem:
                addItem();
                break;
            case R.id.buttonMinus1Marker:
                alterMarkerNumber(v, -1);
                break;
            case R.id.buttonPlus1Marker:
                alterMarkerNumber(v, 1);
                break;
            case R.id.buttonRemoveMarker:
                removeItem(v);
                break;
            default:
                break;
            }
        }

        private void alterMarkerNumber(View v, int point)
        {
            View row = (View) v.getParent();
            ViewHolder holder = (ViewHolder) row.getTag();
            int markers = Integer.parseInt(holder.markers.getText().toString()) + point;
            sb.delete(0, sb.capacity()).append(markers);
            holder.markers.setText(sb.toString());
            mAdapter.notifyDataSetChanged();
        }

        private void addItem()
        {
            String name = mAddCardEditText.getText().toString();
            if(name.length() > 0)
            {
                name = name.replace("'", " ").replace("\"", " ");
                mAddItemEditText.setText("");
                this.mParent.addItem(name);
            }
        }

        private void removeItem(View v)
        {
            View row = (View) v.getParent();
            ViewHolder holder = (ViewHolder) row.getTag();
            if(holder.position != -1)
            {
                this.mParent.removeItem(holder.position);
            }
        }
    }
}

感谢。

1 个答案:

答案 0 :(得分:1)

通过实现ArrayAdapter而不是BaseAdapter,问题就消失了。