Содержимое Edittext не поддерживается при прокрутке ListView - PullRequest
0 голосов
/ 09 февраля 2020

У меня есть список объектов (name, qty), который я отображаю в диалоговом окне. Пользователь может ввести количество в EditText любого элемента, и у меня есть 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 имеет ушел. Пожалуйста, дайте мне несколько советов о том, как мне решить эту задачу. Заранее спасибо!

1 Ответ

0 голосов
/ 09 февраля 2020

Вам нужно позвонить adapterInstance.notifyDataSetChanged() после обновления данных в afterTextChanged().

Попробуйте

    @Override
    public void afterTextChanged(Editable s) {
        countCheckLists.get(position).setDisplayQuantity(value);
        notifyDataSetChanged(); //YOU NEED TO ADD THIS LINE
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...