代码之家  ›  专栏  ›  技术社区  ›  glokul

Android截击列表视图闪烁,项目剩余

  •  0
  • glokul  · 技术社区  · 7 年前

    ListView CustomAdapter 设置为该值后,将显示我通过截取请求调用的json对象数组:

    final ListView listView = (ListView) findViewById(R.id.list);
    
    adapter = new CustomListAdapter(this,personList);
    listView.setAdapter(adapter);
    
    // Creating volley request obj
         final JsonArrayRequest personReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
    
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
    
                            JSONObject obj = response.getJSONObject(i);
                            person.setfirstName(obj.getString("first_name"));
                            person.setlastName(obj.getString("last_name"));
    
                            //Add person to list
                            personList.add(person);
                            adapter.notifyDataSetChanged();
    
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
    
                    }
    
                    Log.i(TAG, "personList items: " + personList.size());
    
    
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();
    
        }
    });
    
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(personReq);
    

    使用可运行的and处理程序,我每5秒发出一次请求:

        //Run volley request every 5 seconds
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
    
                if (orderList != null) {
                    orderList.clear();
                }
    
                //Placing here TOO removes the last item but causes blinking
                //adapter.notifyDataSetChanged();
    
                AppController.getInstance().addToRequestQueue(personReq);
    
                handler.postDelayed(this, 5000);
    
            }
        };
    
        handler.postDelayed(runnable, 5000);
    

    问题是,一个项目总是保留下来,即使它不应该在那里(由于通过一个单独的截击请求删除它)。点击该项目会导致应用程序崩溃,并出现logcat中的以下内容:

    java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes.
    

    正在添加 adapter.notifyDataSetChanged();

    有没有可能阻止这种闪烁?

    3 回复  |  直到 7 年前
        1
  •  0
  •   KeLiuyue    7 年前

    试试这个。

    public void addAll(List<T> datas) {
        if (datas != null) {
            mData.addAll(datas);
            notifyDataSetChanged();
        }
    }
    

    然后在你的活动中。

    personList.add(person);
    adapter.addAll(personList);
    adapter.notifyDataSetChanged();
    

    notifyDataSetChanged() 当其内容更改时

        2
  •  0
  •   dominicoder    7 年前

    添加person对象后。

    那是浪费。呼叫 notifyDataSetChanged ,在列表处于要显示的最终状态后。

    最后一项会导致整个视图每5秒闪烁一次。

    我只能猜测这种“闪烁”是因为您正在清除列表并刷新适配器(从而使 ListView 空)在发出网络请求之前,当然会根据网络条件延迟。

    之后

    public void onResponse(JSONArray response) {
        Log.d(TAG, response.toString());
    
        personList.clear(); // Wipe the list before adding new data
    
        // Parsing json
        // The rest of your parsing code here...
    
        // Now that the list is fully updated to the new state, call this ONCE
        adapter.notifyDataSetChanged();
    }
    
        3
  •  0
  •   glokul    7 年前

    好的,我想出了一种方法来停止闪烁并清除最后剩下的项目。

    除了 onResponse adapter.notifyDataSetChanged(); 也必须出现在 onErrorResponse