使用客户适配器的 ListView 将编辑文本值从删除的项目转移到下一个项目
ListView utilising customer adapter shifts edit text value from item removed to next item
我正在为我的 ListView 使用自定义适配器,它会为每个列表项扩充布局。对于每个列表项,布局中都有一个按钮,允许用户从列表视图中删除该项目。还有一个用于用户输入的编辑文本。我 运行 遇到以下问题:当编辑文本已填充,但随后用户删除该项目时,该值会转移到其下方列表项的编辑文本。例如,如果我的列表视图位置 1 中的编辑文本中有值“25”,然后用户单击以删除该项目,值“25”将转移到位置 1 中的新项目。
我的适配器class
public class PartOrderAddPartAdapter extends ArrayAdapter<Part> {
private static final int layoutResourceId = R.layout.part_information_list_item;
private List<Part> partList;
private final Context context;
public PartOrderAddPartAdapter(Context context, List<Part> partList) {
super(context, layoutResourceId, partList);
this.context = context;
this.partList = partList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(layoutResourceId, null);
}
Part p = getItem(position);
TextView partNumber = (TextView) view.findViewById(R.id.part_number);
TextView partDescription = (TextView) view.findViewById(R.id.part_description);
EditText quantity = (EditText) view.findViewById(R.id.quantity);
Button removePartButton = (Button) view.findViewById(R.id.remove_part_button);
partNumber.setText(p.getPartNumber());
partDescription.setText(p.getDescription());
removePartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
partList.remove(position);
notifyDataSetChanged();
}
});
return view;
}
@Override
public Part getItem(int position) {
return partList.get(position);
}
}
您需要记住,该行的视图是重复使用的。
当一个未使用的存在时,它不会创建一个新的。您在 convertView 中收到它以进行重新填充 - 正如您在自己的代码中看到的那样。
所以,如果那里有以前的值,那么它仍然会在那里。
由您来清除它。
简而言之,您必须为行中的所有字段设置值。你不能假设那里的价值。
当您重复使用视图时,您对视图所做的任何更改都将在重复使用时(即滚动或刷新列表时)可见。在您的情况下,您要从列表中删除该项目,但不会从视图中清除文本。当您调用 notifyDataSetChanged() 并且文本仍然可见时,视图会向上移动。
您需要做的是在调用 notifyDataSetChanged() 之前清除视图中该位置的文本。
另一件需要注意的事情是在列表适配器中重用视图时使用条件语句。简而言之,始终在 if 和 else 之后,以确保在语句计算结果为 false 时将视图重置为默认状态。
我正在为我的 ListView 使用自定义适配器,它会为每个列表项扩充布局。对于每个列表项,布局中都有一个按钮,允许用户从列表视图中删除该项目。还有一个用于用户输入的编辑文本。我 运行 遇到以下问题:当编辑文本已填充,但随后用户删除该项目时,该值会转移到其下方列表项的编辑文本。例如,如果我的列表视图位置 1 中的编辑文本中有值“25”,然后用户单击以删除该项目,值“25”将转移到位置 1 中的新项目。
我的适配器class
public class PartOrderAddPartAdapter extends ArrayAdapter<Part> {
private static final int layoutResourceId = R.layout.part_information_list_item;
private List<Part> partList;
private final Context context;
public PartOrderAddPartAdapter(Context context, List<Part> partList) {
super(context, layoutResourceId, partList);
this.context = context;
this.partList = partList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(layoutResourceId, null);
}
Part p = getItem(position);
TextView partNumber = (TextView) view.findViewById(R.id.part_number);
TextView partDescription = (TextView) view.findViewById(R.id.part_description);
EditText quantity = (EditText) view.findViewById(R.id.quantity);
Button removePartButton = (Button) view.findViewById(R.id.remove_part_button);
partNumber.setText(p.getPartNumber());
partDescription.setText(p.getDescription());
removePartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
partList.remove(position);
notifyDataSetChanged();
}
});
return view;
}
@Override
public Part getItem(int position) {
return partList.get(position);
}
}
您需要记住,该行的视图是重复使用的。 当一个未使用的存在时,它不会创建一个新的。您在 convertView 中收到它以进行重新填充 - 正如您在自己的代码中看到的那样。
所以,如果那里有以前的值,那么它仍然会在那里。 由您来清除它。
简而言之,您必须为行中的所有字段设置值。你不能假设那里的价值。
当您重复使用视图时,您对视图所做的任何更改都将在重复使用时(即滚动或刷新列表时)可见。在您的情况下,您要从列表中删除该项目,但不会从视图中清除文本。当您调用 notifyDataSetChanged() 并且文本仍然可见时,视图会向上移动。
您需要做的是在调用 notifyDataSetChanged() 之前清除视图中该位置的文本。
另一件需要注意的事情是在列表适配器中重用视图时使用条件语句。简而言之,始终在 if 和 else 之后,以确保在语句计算结果为 false 时将视图重置为默认状态。