我正在尝试实现一个片段,该片段包含一个绑定到自定义适配器的 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);
}
}
}
}
谢谢。