2

我试图创建一个应用程序,使用户能够创建事件,然后邀请参与者。因此,当用户进入“添加参与者”页面时,在输入所有信息后,我试图通过使用 onResume() 直接返回“参与者列表”页面,但是如何更新列表视图?我尝试使用 notifyDataSetChanged() 但不工作。

这是参与者列表的代码:

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

这是涉及 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();
}
4

1 回答 1

5

首先,您在方法中的adapter.notifyDataSetChanged()right before是无用的,因为列表不知道数据,除非您调用.setListAdapter(adapter);onCreate()setListAdapter()

所以这是你类 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);
}

所以在这里,我们基本上每次onResume()调用都会刷新数据。

如果您只想在添加新项目后执行此操作,则必须使用onActivityResult()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);
}

现在,只需将您的onCreate()方法中的 startActivity 替换为startActivityForResult(objIndent, ADD_PARTICIPANT);

最后,在您的 AddParticipant 活动中,添加setResult(Activity.RESULT_OK);之前onFinish();

于 2013-10-31T15:44:06.687 回答