Nikhil Nikhil - 1 month ago 7
Android Question

Listiew is not updating after changes to data

I am using two

ListvViews
(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 decreased by 1 and both the
ListViews
should be updated.

Example:

If List1 has 10 marks and a name is entered then List1 should have marks set to 9 and List2 should have marks set to 1.

If we did this 10 times then List1 names should be removed and List2 has marks set to 10.

Example 2:

Suppose List1 has marks set to 8 and List2 marks set to 2. If the sub button is pressed then both should lists should be added. At this point, List2 should be deleted and List1 should have total marks as 10.

Below is the logic I have used however it is not working it is creating multiple rows and marks which not updating properly.

HomeAct class

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class HomeAct extends Activity {

List<DocItem> docDet1 = new ArrayList<DocItem>();
List<DocItem> docDet2 = new ArrayList<DocItem>();
ListView lv1, lv2;
EditText editText;
Button btn1, btn2;
DocDetAdapter adapter1, adapter2;
int n=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_act);
lv1 = (ListView)findViewById(R.id.lv_det1);
lv2 = (ListView)findViewById(R.id.lv_det2);
editText = (EditText)findViewById(R.id.editText1);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
adapter1 = new DocDetAdapter(1);
adapter2 = new DocDetAdapter(2);

docDet1.add(new DocItem("1", "john", 20));
docDet1.add(new DocItem("2", "james", 5));
docDet1.add(new DocItem("3", "shaun", 60));
docDet1.add(new DocItem("4", "joy", 10));
docDet1.add(new DocItem("5", "mark", 1));

lv1.setAdapter(adapter1);
lv2.setAdapter(adapter2);

btn1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

for(int j =0;j<docDet1.size();j++){
final DocItem curItem = docDet1.get(j);
if(curItem.name.equals(editText.getText().toString())){
n++;
DocItem docItem = docDet1.get(j);
docDet1.get(j).marks = n;
docDet2.add(0, docItem);
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
editText.setText("");
}
}
editText.setText("");
}
});

btn2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
for(int j =0;j<docDet2.size();j++){
final DocItem curItem = docDet2.get(j);
if(curItem.name.equals(editText.getText().toString())){

DocItem docItem = docDet1.get(j);
docDet2.remove(j);
docDet1.add(0, docItem);
adapter1.notifyDataSetChanged();
adapter2.notifyDataSetChanged();
editText.setText("");
}
}
editText.setText("");
}
});
}

private class DocDetAdapter extends BaseAdapter {

int mode; // 1 or 2

public DocDetAdapter(int mode) {
this.mode = mode;
}

@Override
public int getCount() {
if (mode == 1)
return docDet1.size();
else
return docDet2.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater li = getLayoutInflater();

if (convertView == null)
convertView = li.inflate(R.layout.row_tray_det, null);

TextView tvItemName = (TextView) convertView
.findViewById(R.id.tv_item_name);
TextView tvRack = (TextView) convertView.findViewById(R.id.tv_rack);
TextView tvQty = (TextView) convertView.findViewById(R.id.tv_qty);

DocItem invItem;
if (mode == 1)
invItem = docDet1.get(position);
else
invItem = docDet2.get(position);

tvItemName.setText(invItem.docNo);
tvRack.setText(invItem.name);
tvQty.setText(invItem.marks + "");

return convertView;
}
}
}


DocItem class

public class DocItem {

public String docNo, name;
public Integer marks;

public DocItem(String docNo, String name, Integer marks) {
super();
this.docNo = docNo;
this.name = name;
this.marks = marks;
}
}


home_act.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:text="Add" />

<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:text="Sub" />
</LinearLayout>

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="list 1"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>

<ListView
android:id="@+id/lv_det1"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:text="list 2"
android:textSize="20sp"
android:textStyle="bold" >
</TextView>

<ListView
android:id="@+id/lv_det2"
android:layout_width="fill_parent"
android:layout_height="250dp" />

</LinearLayout>


row_tray_det

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<View
android:id="@+id/v_doc_seperator"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/blue"
android:visibility="gone" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/row_bg_transparent_white"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_rack"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="5"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Sl no"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_item_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_qty"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:ems="10"
android:gravity="center_vertical"
android:paddingRight="3dp"
android:text="Marks"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>




Here is an image of the expected output:
Expected Output

In the above output, Joy had 10 marks and if "Joy" is entered as input then list should shown as above.

I could really use help, thanks in advance.

Answer

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
        public void onClick(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

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

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
        public void onClick(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

DocItem findDocItem2(String name) {
    for (DocItem item : docDet1) {
        if (item.name.equals(name)) {
            return item;
        }
    }
    return null;
 }
Comments