Question

Im trying to create an application that enable user to create event, then invite participant. So when user go to "add participant" page, after entering all information, im trying to direct back to the "participant list" page by using onResume(), but how to update the listview? i tried to use notifyDataSetChanged() but not working.

Here is the code for participant list:

public class EventPage extends ListActivity implements OnClickListener {
Intent intent;
TextView friendId;
EventController controller = new EventController(this);
TabHost th;
Button addparticipant;
String eventId;
ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.eventpage);
    addparticipant = (Button) findViewById(R.id.addpart);
    addparticipant.setOnClickListener(this);
    // create tabs
    th = (TabHost) findViewById(R.id.tabhost);

    th.setup();
    TabSpec specs = th.newTabSpec("tag1"); // setting up a tabspec in
                                            // tabhost(th) call it tag1
    specs.setContent(R.id.tab1);// set content of "tab1" xml
    specs.setIndicator("Participants");
    th.addTab(specs);
    specs = th.newTabSpec("tag2"); // setting up a tabspec in tabhost(th)
                                    // call it tag1
    specs.setContent(R.id.tab2);// set content of "tab1" xml
    specs.setIndicator("Total Expenses");
    th.addTab(specs);
    specs = th.newTabSpec("tag3"); // setting up a tabspec in tabhost(th)
    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getIntent();
    eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);
    ArrayList<HashMap<String, String>> friendList = controller
            .getAllFriends(queryValues);
    if (friendList.size() != 0) {
        lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                friendId = (TextView) view.findViewById(R.id.friendId);
                String valFriendId = friendId.getText().toString();
                Intent objIndent = new Intent(getApplicationContext(),
                        EventPage.class);
                objIndent.putExtra("friendId", valFriendId);
                startActivity(objIndent);
            }
        });
        SimpleAdapter adapter = new SimpleAdapter(EventPage.this,
                friendList, R.layout.view_friend_entry, new String[] {
                        "friendId", "friendName" }, new int[] {
                        R.id.friendId, R.id.friendName });
        adapter.notifyDataSetChanged();
        setListAdapter(adapter);
        registerForContextMenu(lv);
    }
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent objIntent = new Intent(getApplicationContext(),
            AddParticipant.class);
    objIntent.putExtra("eventId", eventId);
    startActivity(objIntent);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    String[] menuItems = getResources().getStringArray(R.array.menu);
    for (int i = 0; i < menuItems.length; i++) {
        menu.add(Menu.NONE, i, i, menuItems[i]);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    int menuItemIndex = item.getItemId();
    switch (menuItemIndex) {
    case 0:
        friendId = (TextView) findViewById(R.id.friendId);
        String valFriendId = friendId.getText().toString();
        Intent objIndent = new Intent(getApplicationContext(),
                EditParticipant.class);
        objIndent.putExtra("friendId", valFriendId);
        startActivity(objIndent);
        break;
    case 1:
        friendId = (TextView) findViewById(R.id.friendId);
        String valFriendId2 = friendId.getText().toString();
        controller.deleteFriend(valFriendId2);
        onResume();
    }

    return true;
}

This is the code for add participant which involved onResume() :

public class AddParticipant extends Activity implements OnClickListener{

EditText friendName;
EditText friendNumber;
EditText friendEmail;
EventController controller = new EventController(this);
Button btnadd;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addparticipant);
    friendName = (EditText) findViewById(R.id.friendName);
    friendNumber = (EditText) findViewById(R.id.friendNumber);
    friendEmail = (EditText) findViewById(R.id.friendEmail);
    btnadd = (Button)findViewById(R.id.saveButton);
    btnadd.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    HashMap<String, String> queryValues = new HashMap<String, String>();
    Intent objIntent = getIntent();
    String eventId = objIntent.getStringExtra("eventId");
    queryValues.put("eventId", eventId);
    queryValues.put("friendName", friendName.getText().toString());
    queryValues.put("friendNumber", friendNumber.getText().toString());
    queryValues.put("friendEmail", friendEmail.getText().toString());
    controller.insertFriend(queryValues);
    this.callHomeActivity(v);
    finish();
}

public void callHomeActivity(View view) {
    super.onResume();
}
Was it helpful?

Solution

First, your adapter.notifyDataSetChanged() right before setListAdapter(adapter); in the onCreate() method is useless because the list isn't aware of the data unless you call setListAdapter().

So here is the easy way in you class EventPage :

@Override
protected void onResume()
{
    super.onResume();

    if (getListView() != null)
    {
        updateData();
    }
}

private void updateData()
{
    SimpleAdapter adapter = new SimpleAdapter(EventPage.this, 
                                              controller.getAllFriends(queryValues), 
                                              R.layout.view_friend_entry, 
                                              new String[] {"friendId", "friendName" },
                                              new int[] { R.id.friendId, R.id.friendName });
    setListAdapter(adapter);
}

So here, we basically refresh the data every time onResume() is called.

If you want to do it only after adding a new item, then you must use onActivityResult() instead on onResume():

private static final int ADD_PARTICIPANT = 123;// the unique code to start the add participant activity 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ADD_PARTICIPANT && resultCode == Activity.RESULT_OK)
    {
        updateData();
    }
}

private void updateData()
{
    SimpleAdapter adapter = new SimpleAdapter(EventPage.this, 
                                              controller.getAllFriends(queryValues), 
                                              R.layout.view_friend_entry, 
                                              new String[] {"friendId", "friendName" },
                                              new int[] { R.id.friendId, R.id.friendName });
    setListAdapter(adapter);
}

Now, simply replace in your onCreate() method the startActivity by startActivityForResult(objIndent, ADD_PARTICIPANT);

Lastly, in your AddParticipant activity, add setResult(Activity.RESULT_OK); just before onFinish();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top