我有两个按钮。当点击每个按钮时,它们给出了不同的列表项。我需要在点击时将旧的列表项替换为新的列表项。
代表。 button1点击:
ITEM1 ITEM2 项目3
按钮2点击:
ITEM4 ITEM5 ITEM6
在这里,我无法用新的物品替换旧物品。我怎么能这样做?
我的代码:
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listview);
web1 = (Button) findViewById(R.id.web1);
web2 = (Button) findViewById(R.id.web2);
web1.setOnClickListener(this);
web2.setOnClickListener(this);
}
@Override
public void onClick(android.view.View v) {
switch (v.getId()) {
case R.id.web1:
call(xmlHelp.URL1, xmlHelp.KEY_ITEM);
break;
case R.id.web2:
flag = false;
call(xmlHelp.URL2, xmlHelp.TAG_CONTACTS);
break;
}
}
private void call(final String url, final String keyItem) {
progressDialog = ProgressDialog.show(View.this, "", "Loading...");
new Thread() {
public void run() {
try {
if (flag) {
webCall.apiCall1(url, keyItem);
threadHandler.sendEmptyMessage(0);
} else {
webCall.apiCall2(url, keyItem);
threadHandler.sendEmptyMessage(1);
}
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}.start();
}
private Handler threadHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
// dismiss the progress dialog
if (msg.what == 0) {
for (int i = 0; i < webCall.lm1.size(); i++) {
ListModel1 userObj = webCall.lm1.get(i);
mId.add(userObj.getId());
mName.add(userObj.getName());
mCost.add("Rs." + userObj.getCost());
mDescription.add(userObj.getDescription());
}
lv.setAdapter(null);
adapter = new ListViewAdapter(View.this, R.layout.list_item, mId,
mName, mCost, mDescription);
adapter.notifyDataSetInvalidated();
lv.setAdapter(adapter);
} else {
for (int i = 0; i < webCall.lm2.size(); i++) {
ListModel2 userObj = webCall.lm2.get(i);
mId.add(userObj.getId());
mName.add(userObj.getName());
mEmail.add(userObj.getEmail());
mGender.add(userObj.getGender());
}
lv.setAdapter(null);
adapter = new ListViewAdapter(View.this, R.layout.list_item, mId,
mName, mEmail, mGender);
adapter.notifyDataSetInvalidated();
lv.setAdapter(adapter);
}
progressDialog.dismiss();
}
};