2

I have a list that gets loaded from the server. Below is the task that does this:

class LoadActivities extends AsyncTask <String, String, String> {
     protected String doInBackground(String ... args) {
         final RestAdapter restAdapter = new RestAdapter.Builder().setServer("http://10.0.2.2:8080").build();
         final MyService apiManager = restAdapter.create(MyService.class);
         final Activity activity = apiManager.getActivity("some user", act_id);
         //tasks in activity
         for (Tasks t : activity.getTasks()) {
               String r_id = t.getId()+"";
               String name = t.getName();
               HashMap<String, String> map = new HashMap<String, String>();

               map.put("activity_id", act_id);
               map.put("t_id", t_id);
               map.put("t_name", name);
               tasksList.add(map);
          }
          return null;
     }

    protected void onPostExecute(String file_url) {
        runOnUiThread(new Runnable() {
            public void run() {
                ListAdapter adapter = new SimpleAdapter(
                        TaskActivity.this, tasksList,
                        R.layout.list_item_rec, new String[] { "act_id", "t_id", "t_name"}, new int[] {
                        R.id.act_id, R.id.task_id,R.id.task_name });
                setListAdapter(adapter);
            }
        });
    }
 }

All of this is working fine. However, on another screen I am adding an item on the server and after that I come back to this screen to show the list again. At the time of coming back I want to refresh the list so that it reflects the newly added item.

Questions

Should I refresh the entire list? I have tried doing this by calling the above class again. like so:

public boolean onOptionsItemSelected(MenuItem menuItem) {
    if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
        new CreateTask(this,activityName.getText().toString(), actId).execute();
        Intent returnIntent = new Intent();
        setResult(RESULT_OK, returnIntent);
        finish();
        return true;
    }
    return true;
}

...back on this screen

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Log.d("This is result", result);
            new LoadActivities().execute();
        }
    }
}

problem with this is that It is repopulating the list. Meaning I have duplicates of every activity. How can I resolve this?

OR Is there a way so that I won't have to reload the entire list but rather just add an item(s) to the existing list?

birdy
  • 9,286
  • 24
  • 107
  • 171

2 Answers2

0

First,in the method "onPostExecute", you don't need to call "runOnUiThread", because the "onPostExecute" was run in UI thread.

Second, if you want to refresh the ListView in front of the page, you can use "onActivityResult" in the front page, but if your server data was updated, just get data from server again and update your data set(list), then call adapter.notifyDataSetChanged().

Wish to help you!

Ryan
  • 59
  • 5
  • Hi Thanks for your answer. It helps. I am following your Second point, however, I am getting data from server in `doInBackground()` method. If I try to call that method from `onActivityResult` then I get error that ` The application may be doing too much work on its main thread.`. What is a way to load data from server in `onActivityResult ` – birdy Dec 07 '13 at 02:19
  • doInBackground() was run in another thread(not ui thread), onActivityResult() was run in ui thread, if you load data in the ui thread, it will get this error. – Ryan Dec 08 '13 at 03:37
  • you can setResult(intent,tag) in the onPostExecute(), and then get the extra data from intent in method onActivityResult(). it would be approach your goal. – Ryan Dec 08 '13 at 03:38
0

You should us and ArrayAdapter and let it handle the list.

Create and set the ArrayAdapter right away, then add items to it as necessary. You'll have to override getView in the adapter, but for a simple view that won't be complex code.

The general structure will look like:

onCreate(...) {
    // It's okay if the adapter is empty when you attach it to the ListView
    setListAdapter(new ArrayAdapter<ListItemType>(...));
}

onPostExecute(...) {
    // Once you've retrieved the list of items from the server, add them to
    // the adapter
    ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
    adapter.add([items retrieved from server]);
}

onActivityResult(..., Intent data) {
    // Add the newly added item, either pass it back directly, or get the new 
    // list from the server and compare to see which item needs adding.
    // For simplicity, we'll assume it was passed back by the activity
    ListItemType newlyAddedItem = (ListItemType) data.getParcelableExtra("key");
    ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
    adapter.add(newlyAddedItem);
}
blahdiblah
  • 33,069
  • 21
  • 98
  • 152