0

我有两个按钮。单击每个按钮时都给出了不同的列表项。单击时我需要将旧列表项替换为新列表项。

例如。button1 点击:

项目 1 项目 2 项目 3

按钮2点击:

第 4 项 第 5 项 第 6 项

在这里我不能用新的替换旧项目。我该怎么做?

我的代码:

...

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

    }
};
4

0 回答 0