我在哪里可以放置notifyDataSetChanged();

时间:2017-07-12 03:34:45

标签: android

我想显示始终在MySQL RecycleView中更新的数据。但是,当我从其他设备添加数据时,无法在我的设备上进行更新。这是onCreateView()中的代码。

requestQueue = Volley.newRequestQueue(getContext());

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

    stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("response", response);
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("result");
                for (int a = 0; a < jsonArray.length(); a++) {
                    JSONObject json = jsonArray.getJSONObject(a);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("nama", json.getString("nama"));
                    map.put("isi", json.getString("isi"));
                    map.put("poto", json.getString("poto"));
                    map.put("id", json.getString("id"));
                    list_data.add(map);
                    AdapterChat adapter = new AdapterChat(chat.this, list_data);
                    lvhape.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
    requestQueue.add(stringRequest);

这是我的适配器代码:

Context context;

ArrayList<HashMap<String, String>> list_data;
public AdapterChat(chat chat, ArrayList<HashMap<String, String>> list_data) {
    this.context = chat.getContext();
    this.list_data = list_data;
    notifyDataSetChanged();
}

我可以放notifyDataSetChanged

请帮帮我

2 个答案:

答案 0 :(得分:0)

  • 你可以这样做。

requestQueue = Volley.newRequestQueue(getContext());

list_data = new ArrayList<HashMap<String, String>>();
 AdapterChat adapter = new AdapterChat(chat.this, list_data);
 lvhape.setAdapter(adapter);
stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d("response", response);
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            for (int a = 0; a < jsonArray.length(); a++) {
                JSONObject json = jsonArray.getJSONObject(a);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("nama", json.getString("nama"));
                map.put("isi", json.getString("isi"));
                map.put("poto", json.getString("poto"));
                map.put("id", json.getString("id"));
                list_data.add(map);
                adapter.notifyDataSetChanged();
            }

        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
    }
});
requestQueue.add(stringRequest);

答案 1 :(得分:0)

如果您要将适配器设置为回收站视图,并且数据已从请求中恢复,则无需致电notifyDataSetChanged()。喜欢这个

requestQueue = Volley.newRequestQueue(getContext());

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

stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d("response", response);
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            for (int a = 0; a < jsonArray.length(); a++) {
                JSONObject json = jsonArray.getJSONObject(a);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("nama", json.getString("nama"));
                map.put("isi", json.getString("isi"));
                map.put("poto", json.getString("poto"));
                map.put("id", json.getString("id"));
                list_data.add(map);
            }
            AdapterChat adapter = new AdapterChat(chat.this, list_data);
            lvhape.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
    }
});
requestQueue.add(stringRequest);

但是,每次发出请求时,创建和设置适配器实例都不是一个好习惯。所以更好的解决方案将是这样的

在适配器类中创建更新方法。

public void updateData(List<HashMap<String, String>> list){
    this.listData = list;
    notifyDataSetChanged();    
}

然后,像这样修改上述请求代码。

requestQueue = Volley.newRequestQueue(getContext());

list_data = new ArrayList<HashMap<String, String>>();
AdapterChat adapter = new AdapterChat(chat.this, list_data);
lvhape.setAdapter(adapter);

stringRequest = new StringRequest(Request.Method.GET, url, new 
    Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        Log.d("response", response);
        try {
            JSONObject jsonObject = new JSONObject(response);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            for (int a = 0; a < jsonArray.length(); a++) {
                JSONObject json = jsonArray.getJSONObject(a);
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("nama", json.getString("nama"));
                map.put("isi", json.getString("isi"));
                map.put("poto", json.getString("poto"));
                map.put("id", json.getString("id"));
                list_data.add(map);
             }
             adapter.updateData(list_data);
          } catch (JSONException e) {
              e.printStackTrace();
              Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
       }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(stringRequest);