如何onResume其他活动后更新的ListView? [英] How to update ListView after onResume from other activity?

查看:240
本文介绍了如何onResume其他活动后更新的ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建一个应用程序,使用户能够创建活动,然后邀请参与者。因此,当用户进入添加参与者页面中,输入的所有信息,我尝试使用onResume(),但如何更新列表视图直接返回到参与者列表页面后?我试图用notifyDataSetChanged(),但不工作。

下面是code为参加者名单:

 公共类赛事页面扩展ListActivity实现OnClickListener {
意向意图;
TextView的friendId;
EventController控制器=新EventController(本);
TabHost日;
按钮addparticipant;
字符串事件ID;
ListView的LV;

@覆盖
保护无效的onCreate(包savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.eventpage);
    addparticipant =(按钮)findViewById(R.id.addpart);
    addparticipant.setOnClickListener(本);
    //创建标签
    TH =(TabHost)findViewById(R.id.tabhost);

    th.setup();
    则tabspec规格= th.newTabSpec(标签1); //设置在一则tabspec
                                            // tabhost(次)称之为TAG1
    specs.setContent(R.id.tab1)的tab1的XML //设置内容
    specs.setIndicator(参与者);
    th.addTab(规格);
    规格= th.newTabSpec(标签2); //设置在tabhost(日)一则tabspec
                                    //调用它TAG1
    specs.setContent(R.id.tab2)的tab1的XML //设置内容
    specs.setIndicator(总支出);
    th.addTab(规格);
    规格= th.newTabSpec(TAG3); //设置在tabhost(日)一则tabspec
    HashMap的<字符串,字符串> queryValues​​ =新的HashMap<字符串,字符串>();
    意图objIntent = getIntent();
    EVENTID = objIntent.getStringExtra(EVENTID);
    queryValues​​.put(事件ID,事件ID);
    ArrayList的< HashMap的<字符串,字符串>>好友列表=控制器
            .getAllFriends(queryValues​​);
    如果(friendList.size()!= 0){
        LV = getListView();
        lv.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,
                    INT位置,长的id){
                friendId =(TextView中)view.findViewById(R.id.friendId);
                。字符串valFriendId = friendId.getText()的toString();
                意图objIndent =新的意图(getApplicationContext()
                        EventPage.class);
                objIndent.putExtra(friendId,valFriendId);
                startActivity(objIndent);
            }
        });
        SimpleAdapter适配器=新SimpleAdapter(EventPage.this,
                好友列表,R.layout.view_friend_entry,新的String [] {
                        friendId,friendName},新的INT [] {
                        R.id.friendId,R.id.friendName});
        adapter.notifyDataSetChanged();
        setListAdapter(适配器);
        registerForContextMenu(LV);
    }
}

@覆盖
公共无效的onClick(视图v){
    // TODO自动生成方法存根
    意图objIntent =新的意图(getApplicationContext()
            AddParticipant.class);
    objIntent.putExtra(事件ID,事件ID);
    startActivity(objIntent);
}

@覆盖
公共无效onCreateContextMenu(文本菜单菜单,视图V,
        ContextMenuInfo menuInfo){
    的String []菜单项= getResources()getStringArray(R.array.menu)。
    的for(int i = 0; I< menuItems.length;我++){
        menu.add(Menu.NONE,我,我,菜单项[I]);
    }
}

@覆盖
公共布尔onContextItemSelected(菜单项项){
    INT menuItemIndex = item.getItemId();
    开关(menu​​ItemIndex){
    情况下0:
        friendId =(TextView中)findViewById(R.id.friendId);
        。字符串valFriendId = friendId.getText()的toString();
        意图objIndent =新的意图(getApplicationContext()
                EditParticipant.class);
        objIndent.putExtra(friendId,valFriendId);
        startActivity(objIndent);
        打破;
    情况1:
        friendId =(TextView中)findViewById(R.id.friendId);
        字符串valFriendId2 = friendId.getText()的toString()。
        controller.deleteFriend(valFriendId2);
        onResume();
    }

    返回true;
}
 

这是在$ C $下添加与会者这涉及到onResume():

 公共类AddParticipant扩展活动实现OnClickListener {

的EditText friendName;
的EditText friendNumber;
的EditText friendEmail;
EventController控制器=新EventController(本);
按钮btnadd;

公共无效的onCreate(包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 =(按钮)findViewById(R.id.saveButton);
    btnadd.setOnClickListener(本);
}

@覆盖
公共无效的onClick(视图v){
    // TODO自动生成方法存根
    HashMap的<字符串,字符串> queryValues​​ =新的HashMap<字符串,字符串>();
    意图objIntent = getIntent();
    串事件ID = objIntent.getStringExtra(EVENTID);
    queryValues​​.put(事件ID,事件ID);
    queryValues​​.put(friendName,friendName.getText()的toString());
    queryValues​​.put(friendNumber,friendNumber.getText()的toString());
    queryValues​​.put(friendEmail,friendEmail.getText()的toString());
    controller.insertFriend(queryValues​​);
    this.callHomeActivity(五);
    完();
}

公共无效callHomeActivity(查看视图){
    super.onResume();
}
 

解决方案

首先,你的 adapter.notifyDataSetChanged()前右 setListAdapter(适配器) ; 的onCreate()方法是无用的,因为除非你调用列表是不知道数据的 setListAdapter( )

因此​​,这里是你的类赛事页面最简单的方式:

  @覆盖
保护无效onResume()
{
    super.onResume();

    如果(getListView()!= NULL)
    {
        了updateData();
    }
}

私人无效了updateData()
{
    SimpleAdapter适配器=新SimpleAdapter(EventPage.this,
                                              controller.getAllFriends(queryValues​​)
                                              R.layout.view_friend_entry,
                                              新的String [] {friendId,friendName},
                                              新的INT [] {R.id.friendId,R.id.friendName});
    setListAdapter(适配器);
}
 

所以在这里,我们基本上每次刷新数据 onResume()之称。

如果你想后,才加入新的项目做到这一点,则必须使用 onActivityResult()而不是在 onResume()

 私有静态最终诠释ADD_PARTICIPANT = 123; //独特的code以启动添加参加活动

@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据)
{
    super.onActivityResult(要求code,因此code,数据);

    如果(要求code == ADD_PARTICIPANT和放大器;&安培;结果code == Activity.RESULT_OK)
    {
        了updateData();
    }
}

私人无效了updateData()
{
    SimpleAdapter适配器=新SimpleAdapter(EventPage.this,
                                              controller.getAllFriends(queryValues​​)
                                              R.layout.view_friend_entry,
                                              新的String [] {friendId,friendName},
                                              新的INT [] {R.id.friendId,R.id.friendName});
    setListAdapter(适配器);
}
 

现在,只需更换你的的onCreate()方法startActivity通过        startActivityForResult(objIndent,ADD_PARTICIPANT);

最后,在你的AddParticipant活动,增加的setResult(Activity.RESULT_OK); 之前 onFinish();

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

解决方案

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

这篇关于如何onResume其他活动后更新的ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆