【问题标题】:How to call notifyDataSetChanged() on ArrayList view?如何在 ArrayList 视图上调用 notifyDataSetChanged()?
【发布时间】:2012-08-06 14:13:09
【问题描述】:

这是更新和保存后作为结果页面打开的 ArrayList 页面。我想我需要以某种方式刷新,以便它反映 UI 上的更改。我试图调用 notifyDataSetChanged() 但我的经验水平不走运。有人可以告诉我如何实现它吗?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_requests);

    requestsList = new ArrayList<HashMap<String, String>>();

    new LoadAllRequests().execute();

    ListView list = getListView();

    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            String request_id = ((TextView) view.findViewById(R.id.request_id)).getText().toString();


            Intent in = new Intent(getApplicationContext(),
                    ViewRequestActivity.class);

            in.putExtra(TAG_ID, request_id);


            startActivityForResult(in, 100);
        }
    });

}

// Response from ViewRequestActivity when delete a request reload this page again

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {            
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}


class LoadAllRequests extends AsyncTask<String, String, String> {



    protected String doInBackground(String... args) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_all_requests, "GET", params);


        Log.d("All Requests: ", json.toString());

        try {

            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {


                requests = json.getJSONArray(TAG_REQUESTS);


                for (int i = 0; i < requests.length(); i++) {
                    JSONObject c = requests.getJSONObject(i);


                    String request_id = c.getString(TAG_ID);
                    String request_title = c.getString(TAG_TITLE);


                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, request_id);
                    map.put(TAG_TITLE, request_title);


                    requestsList.add(map);
                }
            } else {


                Intent i = new Intent(getApplicationContext(),
                        NewRequestActivity.class);

                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

【问题讨论】:

  • ListView 的适配器在哪里?

标签: java android android-listview android-arrayadapter


【解决方案1】:

您需要为您的 ListView 创建一个适配器。适配器是将数据提供给它以进行显示。我建议您通读本教程:

http://www.vogella.com/articles/AndroidListView/article.html

因此,一旦您创建了适配器并调用了lv.setAdapter(&lt;adapter&gt;),您就可以调用&lt;adapter&gt;.notifyDataSetChanged()。这将告诉适配器它需要自我刷新。

【讨论】:

    【解决方案2】:

    您可以为您的适配器使用 notifyDataSetChanged() 方法。无论您想在哪里更新您的列表视图,您都可以通过以下方式使用。

     adapter.notifyDataSetChanged(); 
    

    【讨论】:

    • @user1466971 你的适配器叫什么名字?
    • @user1466971 您如何显示您的列表视图?发布该代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多