有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

滚动ListView时不维护Edittext的java内容

我在对话框中显示了对象(名称、数量)列表。用户可以在EditText中输入任何项目的数量,我在列表外有一个文本视图,其中必须显示完整列表的自动求和

在列表的滚动条上出现随机行为,并且某些项目的数据已丢失

我的适配器代码是

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的数据时,它已经消失了。请给我一些建议,我将如何完成这项任务。提前谢谢


共 (1) 个答案

  1. # 1 楼答案

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

    试试看

        @Override
        public void afterTextChanged(Editable s) {
            countCheckLists.get(position).setDisplayQuantity(value);
            notifyDataSetChanged(); //YOU NEED TO ADD THIS LINE
        }