Skip to content Skip to sidebar Skip to footer

Listview Is Not Updating After Changes To Data

I am using two ListViews (List1 has data and List2 is empty). The user can enter a name as input and if List1 contains name and the add Button is pressed then marks should be decre

Solution 1:

From what i understand if the name entered in editText matches the name in the list reduce marks by 1. Add the same content to the list 2 and set marks as 1.

If you click add for the same name list 1 marks for the name reduces while marks in list2 becomes2.

Changes to be made for the add part

 btn1.setOnClickListener(new OnClickListener() {

        @Override
        publicvoidonClick(View v) {

            DocItem changeItem = null;
            for (int i = 0; i < docDet1.size(); i++) {
                DocItem docItem = docDet1.get(i);
                if (docItem.name.equals(editText.getText().toString())) {
                    changeItem = docDet1.get(i);
                    changeItem.marks = changeItem.marks - 1;
                    if (findDocItem(editText.getText().toString()) != null) {
                        DocItem docI = findDocItem(editText.getText().toString());
                        docI.marks = docI.marks + 1;
                    } else {
                        docDet2.add(new DocItem(changeItem.docNo, changeItem.name, 1));
                    }

                }

            }

            adapter2.notifyDataSetChanged();
            adapter1.notifyDataSetChanged();
        }
    });

To find and item

DocItemfindDocItem(String name) {
    for (DocItem item : docDet2) {
        if (item.name.equals(name)) {
            return item;
        }
    }
    returnnull;
}

Avoid calling notifyDataSetChanged inside the for loop.

You would do something similar for sub.

For the sub part from the discussion in chat

    btn2.setOnClickListener(new OnClickListener() {

        @Override
        publicvoidonClick(View v) {

            int changedmarks=0;
            for (int i = 0; i < docDet2.size(); i++) {
                DocItem docItem = docDet2.get(i);
                if (docItem.name.equals(editText.getText().toString())) {
                    changedmarks =docDet2.get(i).marks;
                    docDet2.remove(i);

                }
            }
            if(findDocItem2(editText.getText().toString())!=null )
            {
                DocItem  docitem = findDocItem2(editText.getText().toString());
                docitem.marks = docitem.marks+ changedmarks;
            }

            adapter1.notifyDataSetChanged();
            adapter2.notifyDataSetChanged();
        }
    });

Then

DocItemfindDocItem2(String name) {
    for (DocItem item : docDet1) {
        if (item.name.equals(name)) {
            return item;
        }
    }
    returnnull;
 }

Post a Comment for "Listview Is Not Updating After Changes To Data"