sarah
sarah

Reputation: 37

How to refresh a listview in an Activity after finishing the previous Activity?

The interaction between my Activities:

  1. Click on a row in listview in listview.java >

  2. Goes to Edit.java to edit information about that specific row >

  3. When done editing, click button, which will finish the edit.java activity >

  4. returns back to listview.java.

However, upon returning to listview.java FROM edit.java, it does not display the new updated information of the specific row of the listview that was previously clicked on.

Only when I leave the listview Activity and return back again, then the information is newly updated.

listview.java:

    MyItems mi;
    private ArrayList<SalesItemInformationLV> displayiteminfo;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_list_sale_item);
    mi = MyItems.getInstance();
            displayiteminfo = mi.retrieveAllForlist(getApplicationContext());

            final ArrayAdapter<SalesItemInformationLV> adapter = new itemArrayAdapter(this, 0, displayiteminfo);


            final ListView listView = (ListView)findViewById(R.id.customListview);
            listView.setAdapter(adapter);

   // adapter.notifyDataSetChanged(); Place this code here but it does not work

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                SalesItemInformationLV saleitem2 = displayiteminfo2.get(info.position);

        String namevalue = saleitem2.getItemname();
        Double costpvalue = saleitem2.getCostprice();
        Double sellpvalue = saleitem2.getSellingprice();
        int qtyvalue = saleitem2.getItemquantity();
        String datevalue = saleitem2.getDatesold();
        int staffvalue = saleitem2.getStaffdiscount();

        Intent myintent = new Intent(List.this, Edit.class);
        myintent.putExtra("array", saleitem2);
        myintent.putExtra("itemname", namevalue);
        myintent.putExtra("itemcp", costpvalue);
        myintent.putExtra("itemsp", sellpvalue);
        myintent.putExtra("itemqty", qtyvalue);
        myintent.putExtra("itemds", datevalue);
        myintent.putExtra("itemsstaffdis", staffvalue);

        startActivity(myintent);

}

Upvotes: 0

Views: 2198

Answers (5)

Leandro Ocampo
Leandro Ocampo

Reputation: 2004

There are many problems there. First you are sending the attributes of that object in the intent, not the object itself. The quickest way (not the best maybe) is to send the object (SalesItemInformationLV saleitem2). To do so that object needs to implement the Serializable interface.

This is the way to pass a serializable object: Passing data through intent using Serializable

It is important that you use this solution: https://developer.android.com/training/basics/intents/result.html?hl=en-419

Then, in the onActivityResult() you should call adapter.notifyDataSetChanged();

Remember to declare the adapter as a global attribute (outside the method).

Upvotes: 0

Ihdina
Ihdina

Reputation: 1770

Data flows from steps 1, 2 and 3.

listview.java:

// Step 1 (Send data to edit.class)
Intent i = new Intent(this, Edit.class);
startActivityForResult(i, 1);

// Step 3 (Receive new data)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            String strEditText = data.getStringExtra("key");

           // update your listview in here with new data
        }     
    }
} 

Edit.java:

// Step 2 (Edit data in here, and send to listview.java with setResult)
Intent intent = new Intent();
intent.putExtra("key", "value")    
setResult(RESULT_OK, intent);        
finish();

Upvotes: 1

Morteza Jalambadani
Morteza Jalambadani

Reputation: 2465

You can use adapter.notifyDataSetChanged(); in onResume method activity but it is better to use startActivityForResult and setResult methods.

Upvotes: 0

Pushkaraj Joshi
Pushkaraj Joshi

Reputation: 159

The following code demonstrates the basic implementation of a listview.

In the activities onCreate Method:

    ListView listView = (ListView) findViewById(R.id.list);

    // Defined Array values to show in ListView
    String[] values = new String[] { "Android List View",
            "Adapter",
            "Simple List View ",
            "Create List View ",
            "Android Example",
            "List View ",
            "Array Adapter",
            "Android List View"
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values);

    listView.setAdapter(adapter);

Following your add/delete/update on the array of Strings(i.e. values),then you call the adapter.notifydatasetchanged(); For eg: In my example, are 8 items in the values array:

    values[7] = "Android Example List View";
    adapter.notifyDataSetChanged();

Then you'll see that the listview has been updated. Effectively speaking, your adapter takes the Array, and then sends that data to the listview. Listview directly does not handle the array of Strings. Everytime you do some kind of CRUD operation on the Array, you call adapter.notifyDataSetChanged(); for the listview to reflect the changes that you have made. During application, I usually swap out the Array for a Arraylist, as that makes the adding and deleting a lot easier.

Upvotes: 0

Ridcully
Ridcully

Reputation: 23665

It depends on where your items are stored (database?) and whether the edit activity saves the changes to the database or not. Asuming that this is the case, you should

  • use startActivityForResult() in the listview activity to start the edit activity
  • in the edit activity, when user is done, use setResult() and finish() to go back to the listview activity
  • use onActivityResult() in the listview activity to check if you came back from the edit activity, and if so, reload data from database and use listview's notifyDataSetChanged()

This is just a basic workflow, various optimizations are possible.

Upvotes: 0

Related Questions