Comment mettre à jour ListView après onResume d'autres activités?
Im essayant de créer une application permettant à l'utilisateur de créer un événement, puis inviter des participants. Ainsi, lorsque l'utilisateur aller à "ajouter un participant" de la page, après avoir entré toutes les informations, im essayant de renvoyer à la "liste des participants" à la page en utilisant onResume(), mais comment le mise à jour de la liste? j'ai essayé d'utiliser notifyDataSetChanged (), mais ne fonctionne pas.
Voici le code pour la liste des participants:
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;
}
C'est le code pour ajouter un participant qui a impliqué 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();
}
- vous pouvez régler la carte avec des éléments nouveaux dans onResume()
- La façon correcte de faire quelque chose comme cela est d'utiliser
startActivityForResult
. VotreEventPage
devrait commencer leAddParticipant
activité pour le résultat, puis ensuite de traiter le résultatonActivityResult
et mise à jour de sa liste.. - notifyDataSetChanged() est le bon chemin, mais vous devez modifier les données à recevoir cet événement... developer.android.com/reference/android/widget/...
InformationsquelleAutor AndroidWorld12 | 2013-10-31
Vous devez vous connecter pour publier un commentaire.
Tout d'abord, votre
adapter.notifyDataSetChanged()
juste avantsetListAdapter(adapter);
dans leonCreate()
méthode est inutile parce que la liste n'est pas au courant des données, à moins que vous appelezsetListAdapter()
.Donc, ici, est le moyen le plus facile en vous classe Page :
Donc, ici, nous avons essentiellement actualiser les données à chaque fois
onResume()
est appelé.Si vous voulez le faire seulement après l'ajout d'un nouvel élément, vous devez utiliser
onActivityResult()
plutôt suronResume()
:Maintenant, il suffit de remplacer dans votre
onCreate()
la méthode startActivity parstartActivityForResult(objIndent, ADD_PARTICIPANT);
Enfin, dans votre AddParticipant activité, ajouter
setResult(Activity.RESULT_OK);
juste avantonFinish();