I created a custom ArrayAdapter that on click of the add button in the menu will pop-up a dialog (Entering item's name, price and quantity) to add a new item.
(我创建了一个自定义ArrayAdapter,单击菜单中的“添加”按钮将弹出一个对话框(输入项目的名称,价格和数量)以添加新项目。)
However, if I add another item. (但是,如果我添加另一个项目。)
The price I entered on the second item will also affect the first item. (我在第二个项目上输入的价格也会影响第一个项目。)
And if I add another more items, the latest price will be the same values for the other items. (如果我再添加其他项目,则其他项目的最新价格将相同。)
If I try to change the item's value (using its EditText), it gives unpredictable results. (如果我尝试更改项目的值(使用其EditText),则会产生不可预测的结果。)
And if I try deleting any items, the program crashes. (如果我尝试删除任何项目,程序将崩溃。)
I narrowed down why it acts like that and the problem was that the TextWatcher gets invoke a lot of times and the position inside is not consistent to how it was defined.
(我缩小了为什么会这样,问题是TextWatcher被调用了很多次,并且内部位置与定义的方式不一致。)
I want to know how to solve this problem.
(我想知道如何解决这个问题。)
Here's an image of the program: My App (After entering the third item, all of the price values are the same as the third item, which is c)
(这是程序的图像: 我的应用程序(输入第三项后,所有价格值都与第三项相同,即c))
And here's the code:
(这是代码:)
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (inflater == null) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_listview, null);
}
ImageButton deleteButton = convertView.findViewById(R.id.deleteImageButton);
TextView itemTextView = convertView.findViewById(R.id.itemTextView);
EditText quantityEditText = convertView.findViewById(R.id.quantityEditText);
EditText priceEditText = convertView.findViewById(R.id.priceEditText);
priceEditText.setText(String.format(Locale.ENGLISH, "%.2f", prices.get(position)));
itemTextView.setText(getItem(position));
quantityEditText.setText(String.valueOf(quantities.get(position)));
/*
some filters here...
*/
priceEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String value = editable.toString();
if (editable.toString().isEmpty() || editable.toString().equals("."))
value = "0";
prices.set(position, Double.parseDouble(value));
listener.onItemCountChange(getCount() + 1, getCount());
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
remove(getItem(position));
prices.remove(position);
quantities.remove(position);
listener.onItemCountChange(getCount() + 1, getCount());
}
});
return convertView;
}
Oh and I tried looking for solutions here too in StackOverFlow (holder; get/set tag; other random stuffs).
(哦,我也尝试过在StackOverFlow中寻找解决方案(holder; get / set标签;其他随机的东西)。)
I saw around 10 posts and to no avail did I comprehend how to fix this. (我看到了大约10个帖子,但没有理解如何解决此问题。)
Feel free to ask me for missing info and correct me where I'm doing it wrong.
(随时问我丢失的信息,并在错误的地方纠正我。)
I appreciate your help! (我感谢您的帮助!)
And also forgive me I'm still a bit new to Android development. (同时请原谅我对Android开发还是有点陌生??。)
ask by ドラゴンハートゼクモル translate from so