0

我有我在对话框中显示的对象(名称,数量)列表。用户可以在 EditText 中输入任何项目的数量,并且我在 List 之外有一个 TextView,我必须在其中显示完整列表的自动总和。

滚动列表时会发生随机行为,并且某些项目的数据已经消失。

我的适配器代码是

public class CountCheckListAdapter extends ArrayAdapter<CountCheckList> {

List<CountCheckList> countCheckLists;
Context context;
String inputChange;
int value=0;

public CountCheckListAdapter(@NonNull Context context, int resource, @NonNull List<CountCheckList> objects) {
    super(context, resource, objects);
    this.countCheckLists = objects;
    this.context = context;
}

public int getCount() {
    return countCheckLists.size();
}

public CountCheckList getItem(int position) {
    return countCheckLists.get(position);
}

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

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItem = convertView;
    if (listItem == null)
        listItem = LayoutInflater.from(context).inflate(R.layout.count_check_list_item, parent, false);

    CountCheckList countCheckList = countCheckLists.get(position);

    TextView title = (TextView) listItem.findViewById(R.id.count_check_list_title);
    title.setText(countCheckList.getBrandName());

    TextInputEditText quantity = listItem.findViewById(R.id.quantity);
    quantity.setText(String.valueOf(countCheckList.getDisplayQuantity()));

    quantity.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 (s == null || s.toString().isEmpty()) {
            } else {
                value = Integer.parseInt(s.toString());
            }

        }

        @Override
        public void afterTextChanged(Editable s) {
            countCheckLists.get(position).setDisplayQuantity(value);

        }
    });



    return listItem;
}


}

自动总和代码是:

listView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            int totalQuantity = 0;
            for (CountCheckList countCheckList : countCheckLists) {
                totalQuantity = totalQuantity + countCheckList.getDisplayQuantity();
            }

            totalQuan.setText(totalQuantity + "");
        }
    });

我已经尝试了所有可能的方法,但是edittext的滚动数据已经消失了。请给我一些建议,我将如何完成这项任务。提前致谢!

4

1 回答 1

0

您需要在adapterInstance.notifyDataSetChanged()中更新数据后调用afterTextChanged()

试试看

    @Override
    public void afterTextChanged(Editable s) {
        countCheckLists.get(position).setDisplayQuantity(value);
        notifyDataSetChanged(); //YOU NEED TO ADD THIS LINE
    }
于 2020-02-09T14:53:59.957 回答