【问题标题】:Where to place adapter.notifyDataSetChanged(); when using Volley在哪里放置 adapter.notifyDataSetChanged();使用 Volley 时
【发布时间】:2023-12-25 18:36:01
【问题描述】:

此代码运行良好,将我的 JSON 响应显示到 Arraylist 中。问题是,每次我单击按钮再次显示列表时,它都会简单地复制之前的 JSON 响应,从而导致重复结果列表。

如何在每次单击按钮时刷新数组列表,删除所有以前的结果?我需要放置另一个 notifyDataSetChanged 吗?如果有,在哪里?

Details.java 代码:

public class Details extends AppCompatActivity {

    private static final String TAG = Details.class.getSimpleName();
    String url="removed";
    private Button ShowDetailsButton;
    private Button AddDetails;
    private CustomListAdapter adapter;
    private ProgressDialog pDialog;
    private List<LoadUsers> mList = new ArrayList<LoadUsers>();
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.studio_student_view);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        listView = (ListView) findViewById(R.id.lv);
        adapter = new CustomListAdapter(this, mList);
        listView.setAdapter(adapter);

        ShowDetailsButton = (Button) findViewById(R.id.show_details);
        AddDetails = (Button) findViewById(R.id.add_details);

        // Progress dialog
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);

        ShowDetailsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Creating volley request obj
                JsonArrayRequest studentReq = new JsonArrayRequest(url,
                        new Response.Listener<JSONArray>() {
                            @Override
                            public void onResponse(JSONArray response) {
                                Log.d(TAG, response.toString());
                                hidePDialog();

                                // Parsing json
                                for (int i = 0; i < response.length(); i++) {
                                    try {

                                        JSONObject obj = response.getJSONObject(i);
                                        LoadUsers details = new LoadUsers();
                                        details.setTitle(obj.getString("name"));
                                        details.setThumbnailUrl(obj.getString("image"));
                                        details.setEmail(obj.getString("email"));
                                        details.setPhone(obj.getString("phone"));

                                        // adding 
                                        mList.add(details);

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }

                                // notifying list adapter about data changes
                                // so that it renders the list view with updated data
                                adapter.notifyDataSetChanged();
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hidePDialog();

                    }

                });

                // Adding request to request queue
                AppController.getInstance().addToRequestQueue(studentReq);
            }

        });

    }
    public void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }
}

CustomListAdapter.java 代码:

public class CustomListAdapter extends BaseAdapter {
    private Activity activity;
    private LayoutInflater inflater;
    private List<LoadUsers> usersItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public CustomListAdapter(Activity activity, List<LoadUsers> usersItems) {
        this.activity = activity;
        this.usersItems = usersItems;
    }

    @Override
    public int getCount() {
        return usersItems.size();
    }

    @Override
    public Object getItem(int location) {
        return usersItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();
            NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
            TextView title = (TextView) convertView.findViewById(R.id.title);
            TextView email = (TextView) convertView.findViewById(R.id.lvemail);
            TextView phone = (TextView) convertView.findViewById(R.id.lvphone);


            // getting user data for the row
            LoadUsers m = usersItems.get(position);

            // thumbnail image
            thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

            // title
            title.setText(m.getTitle());

            // email
            email.setText("Email: " + String.valueOf(m.getEmail()));

            // phone
            phone.setText("Phone: " + String.valueOf(m.getPhone()));

            return convertView;

    }

}

【问题讨论】:

  • 添加mList.clear(); 正上方评论行// Creating volley request obj 或正上方// Adding request to request queue :)
  • 谢谢,成功了。

标签: android json arraylist adapter android-volley


【解决方案1】:

错误:

数据正在重复,因为您没有清除具有先前数据集的 ArrayList。

解决方案: 在进行 API 调用之前清除 ArrayList。在您的情况下,这应该是按钮单击侦听器代码中的第一行!

// clear collection 
mList.clear();

【讨论】:

    【解决方案2】:

    您可以在public void onResponse(JSONArray response) 之后添加mList.clear() 您也可以使用mList.addAll() 方法在适配器中添加所有元素并通知一次,否则如果您使用mList.add(),您将在每次添加单个项目时通知。

    请注意,通知是在内部完成的。

    【讨论】:

      【解决方案3】:

      谢谢。成功了!

      解决方案:

      public class Details extends AppCompatActivity {
      
          private static final String TAG = Details.class.getSimpleName();
          String url="removed";
          private Button ShowDetailsButton;
          private Button AddDetails;
          private CustomListAdapter adapter;
          private ProgressDialog pDialog;
          private List<LoadUsers> mList = new ArrayList<LoadUsers>();
          private ListView listView;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.studio_student_view);
              Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
              setSupportActionBar(toolbar);
      
              listView = (ListView) findViewById(R.id.lv);
              adapter = new CustomListAdapter(this, mList);
              listView.setAdapter(adapter);
      
              ShowDetailsButton = (Button) findViewById(R.id.show_details);
              AddDetails = (Button) findViewById(R.id.add_details);
      
              // Progress dialog
              pDialog = new ProgressDialog(this);
              pDialog.setCancelable(false);
      
              ShowDetailsButton.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                
                mList.clear();
      
                      // Creating volley request obj
                      JsonArrayRequest studentReq = new JsonArrayRequest(url,
                              new Response.Listener<JSONArray>() {
                                  @Override
                                  public void onResponse(JSONArray response) {
                                      Log.d(TAG, response.toString());
                                      hidePDialog();
      
                                      // Parsing json
                                      for (int i = 0; i < response.length(); i++) {
                                          try {
      
                                              JSONObject obj = response.getJSONObject(i);
                                              LoadUsers details = new LoadUsers();
                                              details.setTitle(obj.getString("name"));
                                              details.setThumbnailUrl(obj.getString("image"));
                                              details.setEmail(obj.getString("email"));
                                              details.setPhone(obj.getString("phone"));
      
                                              // adding 
                                              mList.add(details);
      
                                          } catch (JSONException e) {
                                              e.printStackTrace();
                                          }
      
                                      }
      
                                      // notifying list adapter about data changes
                                      // so that it renders the list view with updated data
                                      adapter.notifyDataSetChanged();
                                  }
                              }, new Response.ErrorListener() {
                          @Override
                          public void onErrorResponse(VolleyError error) {
                              VolleyLog.d(TAG, "Error: " + error.getMessage());
                              hidePDialog();
      
                          }
      
                      });
      
                      // Adding request to request queue
                      AppController.getInstance().addToRequestQueue(studentReq);
                  }
      
              });
      
          }
          public void hidePDialog() {
              if (pDialog != null) {
                  pDialog.dismiss();
                  pDialog = null;
              }
          }
          public void onDestroy() {
              super.onDestroy();
              hidePDialog();
          }
      }

      【讨论】:

        【解决方案4】:

        我们这里有两种方法,

        1. 您想清除所有数据并完全更新列表。
        2. 您想添加一些新数据以列出并显示旧数据 + 新数据

        方法一:

         @Override
         public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            hidePDialog();
            // CLEAR YOUR OLD DATA HERE
            mList.clear();
            // Parsing json
            for (int i = 0; i < response.length(); i++) {
            try {
                JSONObject obj = response.getJSONObject(i);
                LoadUsers details = new LoadUsers();
                details.setTitle(obj.getString("name"));
                details.setThumbnailUrl(obj.getString("image"));
                details.setEmail(obj.getString("email"));
                details.setPhone(obj.getString("phone"));
                // adding 
                mList.add(details);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        
          }
          // notifying list adapter about data changes
          // so that it renders the list view with updated data
          adapter.notifyDataSetChanged();
        

        按照这种方法,您的列表将再次更新。

        方法二:

        您只想添加新数据并且想要为新添加的项目显示动画。

         @Override
         public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            hidePDialog();
            // MAKE A NEW LIST HERE AND ADD NEW DATA TO IT
            private List<LoadUsers> mListNewItems = new ArrayList<LoadUsers>();
            // Parsing json
            for (int i = 0; i < response.length(); i++) {
            try {
                JSONObject obj = response.getJSONObject(i);
                LoadUsers details = new LoadUsers();
                details.setTitle(obj.getString("name"));
                details.setThumbnailUrl(obj.getString("image"));
                details.setEmail(obj.getString("email"));
                details.setPhone(obj.getString("phone"));
                // ADD NEW DATA TO NEW LIST 
                mListNewItems.add(details);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            // IMPORTANT TASK SHOULD DONE HERE
            // Get Old Size Of the List before adding new data to notify new inserted items
            int oldSize=mList.getSize();
            // here we check whether we have such a data in our list. if we had, we dnt add that, otherwise add new data to our list
            for (int i=0;i<mListNewItems.size();i++){
            if(!mList.contain(mListNewItems.get(i))){
                mList.add(mListNewItems.get(i));
            }
            }
        
            //check how many new items added
            int numberOfNewItems=mList.getSize()-oldSize;
        
            // Here you should use notifyItemRangeInserted instead of notifyDataSetChange as it is less time consuming than notifyDataSetChange and also it let you to show animation for new added items.
            adapter.notifyItemRangeInserted(oldSize,numberOfNewItems);
          }
        

        【讨论】: