简体   繁体   中英

Content of Edittext is not maintained on scrolling of ListView

I have Object(name,qty) List which I am displaying in dialog box. User can enter the quantity in EditText of any item and I have a TextView outside of the List in which I have to show auto sum of complete list.

On scroll of List random behavior occurs and data of some items has gone.

my adapter code is

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;
}


}

And the auto sum code is:

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 + "");
        }
    });

I have tried every possible way but on scrolling data of edittext has gone. Kindly give me some suggestion that how would i achieve this task. Thanks in advance!

You need to call adapterInstance.notifyDataSetChanged() once data is updated in afterTextChanged() .

Try it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM