我写了一个关于购物的简单测试代码。就像在图片中一样。add from right to left
这很简单。 ListViews显示'Item'。 'Item'只有名字和计数。在右侧,我更改了计数并将项目添加到左侧的购物车中。
重点在于,当我更改之前添加的项目计数并再次将项目添加到购物车时,购物车和菜单中的所有相同项目都会更改为最后一次计数。
拍完第一张照片后,我更改了同一项目的计数并再次添加。因此,购物车中的两个项目具有相同的最后计数。我无法理解代码中的错误。 Second screenshot after added the same item with different count
提前致谢。
我发现了一些有关代码的内容。所有ArrayLists都表现得像静态一样。如果我将菜单中的添加按钮的onClick元标记更改为:
ImageButton ekle = (ImageButton) view.findViewById(R.id.imageButton2);
ekle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//After changing this row, all the ArrayList's (menu and cart) items
//in this position is changing to the same number 5.
//And I delete the EditText's TextChangeListener, as well.
menu.get(position).setCount(5);
((BaseAdapter) context.list1.getAdapter()).notifyDataSetChanged();
}
});
public class Item {
private String name;
private int count;
public Item(String s, int i) {
name = s;
count = i;
}
public String getName() {return name;}
public int getCount() {return count;}
public void setName(String s) {name = s;}
public void setCount(int i) {count = i;}
}
public class MainActivity extends AppCompatActivity {
ListView list1;
ListView list2;
static ArrayList<Item> menu = new ArrayList<>();
static ArrayList<Item> cart = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu.add(new Item("Item1", 1));
menu.add(new Item("Item2", 1));
menu.add(new Item("Item3", 1));
menu.add(new Item("Item4", 1));
menu.add(new Item("Item5", 1));
list1 = (ListView) findViewById(R.id.list1);
list1.setAdapter(new CartAdapter(this, cart));
list2 = (ListView) findViewById(R.id.list2);
list2.setAdapter(new MenuAdapter(this, menu));
}
}
public class CartAdapter extends BaseAdapter {
MainActivity context;
ArrayList<Item> cart;
public CartAdapter(Activity activity, ArrayList<Item> m) {
context = (MainActivity) activity;
cart = m;
}
@Override
public int getCount() {
return cart.size();
}
@Override
public Object getItem(int position) {
return cart.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_list, null);
TextView name = (TextView) view.findViewById(R.id.textView);
name.setText(cart.get(position).getName());
EditText count = (EditText) view.findViewById(R.id.editText);
count.setText("" + cart.get(position).getCount());
return view;
}
}
public class MenuAdapter extends BaseAdapter {
MainActivity context;
ArrayList<Item> menu;
public MenuAdapter(Activity activity, ArrayList<Item> m) {
context = (MainActivity) activity;
menu = m;
}
@Override
public int getCount() {
return menu.size();
}
@Override
public Object getItem(int position) {
return menu.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_list2, null);
ImageButton ekle = (ImageButton) view.findViewById(R.id.imageButton2);
ekle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.cart.add(menu.get(position));
((BaseAdapter) context.list1.getAdapter()).notifyDataSetChanged();
}
});
TextView name = (TextView) view.findViewById(R.id.textView2);
name.setText(menu.get(position).getName());
EditText miktar = (EditText) view.findViewById(R.id.editText2);
miktar.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count != 0) {
int i = Integer.parseInt(s.toString());
Item item = menu.get(position);
item.setCount(i);
MainActivity.menu.set(position, item);
CharSequence text = menu.get(position).getName() +" : "+ menu.get(position).getCount();
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
我找到了问题的原因。两个ArrayLists都像静态(但它们不是!)。所以我改变了Item类的decleration,使得count最终。就是这样,问题就解决了。
public class Item
{
private String name;
private final int count;
public Item(String s, int i) {
name = s;
count = i;
}
public String getName() {return name;}
public int getCount() {return count;}
public void setName(String s) {name = s;}
}