0

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

}
sarah
  • 37
  • 8
  • Show us the interaction between listview.java and edit.java please. – Leandro Ocampo Jul 22 '17 at 17:44
  • @LeandroOcampo Hi, i've edited it, please take a look thanks! – sarah Jul 22 '17 at 17:53
  • Great!! But I need the code =). Show the code that is being used for that interaction please. @sarah did you try using adapter.notifyDataSetChanged(); after editing the object that is part of that row ? – Leandro Ocampo Jul 22 '17 at 17:57
  • @LeandroOcampo Hello! I've edited it once again! In the first grey box i have commented out where i placed the adapter.notifyDataSetChanged() codes. Please take a look thanks! – sarah Jul 22 '17 at 18:08

5 Answers5

1

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();
Ihdina
  • 950
  • 6
  • 21
0

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.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
0

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.

  • The CRUD operation is done in another activity. How do I pass the adapter to the other activity? – sarah Jul 22 '17 at 18:19
  • @sarah For SQLite databases in Android, you should display those using a CursorAdapter. You don't pass an adapter, you pass the cursor or the database object – OneCricketeer Jul 22 '17 at 18:21
  • Hey Sarah, have you tried an alternate approach to finishing the activity and passing the altered data through a cursor as @cricket_007 suggested. I wrote my answer such that you'll understand how the notifyDataSetChanged works. Have a look at the Ridcully's answer to get a perspective on how to actually pass the data from one activity to another. – Pushkaraj Joshi Jul 22 '17 at 18:32
0

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).

Leandro Ocampo
  • 1,894
  • 18
  • 38
  • Hi, I've declared the saleitem2 as a serializable actually. The onActivityResult() is done in edit.java or listview.java? – sarah Jul 22 '17 at 18:22
  • listview.java. Also put breakpoints in that method in order to check that you are getting the same object that you sent. If there are the same, then call adapter.notifyDataSetChanged(); Otherwise you need to override equals() and hascode () in the object SalesItemInformationLV and search that object in the arraylist to update it. After that you need to call adapter.notifyDataSetChanged() – Leandro Ocampo Jul 22 '17 at 18:28
0

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

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35