【问题标题】:notifyDataSetChanged is not working for ThreadnotifyDataSetChanged 不适用于线程
【发布时间】:2016-12-27 16:37:55
【问题描述】:

我正在使用服务从服务器获取消息并存储在本地数据库中。如果我不这样做,我需要为凌空响应创建新的线程,UI 滞后太多,但列表视图正在更新。现在创建新线程后,用户界面不再滞后,但它没有更新列表视图。下面是我的服务类代码。

ChatService.java

StringRequest stringRequest=new StringRequest(Request.Method.GET, url.replaceAll(" ","%20"), new Response.Listener<String>() {
        @Override
        public void onResponse(final String response) {
            networkThread=new Thread(new Runnable() {
                @Override
                public void run() {
                    Gson gson=new Gson();
                    chatResponse=gson.fromJson(response,GetAllChatResponse.class);
                    for (Object obj:chatResponse.getData()) {
                        Log.d("conversation response",""+((ChatDataResponse) obj).getMessage());
                        sqLiteDataProvider.insertMessage(true,(ChatDataResponse) obj);
                    }
                    Log.d("conver","ok");
                    sendWaResult();
                }
            });
            networkThread.start();

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("convo error",""+error.getMessage());
        }
    });
    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

public void sendWaResult(){
    final Intent intent=new Intent(KYOWA_RESULT);
    try{
        Looper.prepare();
    } catch (Exception e) {}
    Handler handler = new Handler();
    handler.post(new Runnable() {
        @Override
        public void run() {
            waBroadcastManager.sendBroadcast(intent);
            stopSelf();
        }
    });
}`

另一方面,在片段中,我想通知我的列表视图数据库已通过以下代码更新。

ChatFragment.java

   receiver=new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ChatFragment.this.getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getChat();
                    }
                });

            }
        };
private void getChat() {
    new AsyncTask<Void,Void,ArrayList<ChatListDataResponse>>(){

        @Override
        protected ArrayList<ChatListDataResponse> doInBackground(Void... voids) {
            return sqLiteDataProvider.getChatList();
        }

        protected void onPostExecute(ArrayList<ChatListDataResponse> list)
        {
            listDataResponses = list;
            if (chatListAdapter==null){
                chatListAdapter=new ChatListAdapter(getContext(),listDataResponses);
                mListView.setAdapter(chatListAdapter);
            }else {
                chatListAdapter.updateChatList(listDataResponses);
            }
        }
    }.execute();

我正在通过自定义 BaseAdapter 通知我的列表视图。

ChatListAdapter.java

public void updateChatList(ArrayList<ChatListDataResponse> numbers){
    listDataResponses.clear();
    listDataResponses.addAll(numbers);
    this.notifyDataSetChanged();
    this.notifyDataSetInvalidated();
}
  • 我得到的结果:第一次将 api 响应存储到本地数据库后,列表视图保持空白。每次调用 ChatService 时,列表视图都不会更新。
  • 我想要的结果:我想在调用 ChatService 时通知列表视图。

【问题讨论】:

  • 您的问题解决了吗?

标签: android multithreading listview custom-adapter notifydatasetchanged


【解决方案1】:

尝试 Handler 更新 UI,因为线程不会确认 UI 相关工作的工作:

new Handler().post(new Runnable() {
      public void run() {
        //UI updation code
      }
 });

或尝试:

runOnUiThread(new Runnable() {
      public void run() {
        //UI updation code
      }
 });

【讨论】:

  • 我已经在使用runOnUiThread 调用getChat 来更新UI。
【解决方案2】:

我建议您使用AsyncTask&lt;Void, Void, Void&gt;(),用您的代码覆盖doInBackground(),然后在onPostExecute() 中执行 adapter.notifyDataSetChanged()

【讨论】:

  • updateChatList 方法仅从 onPostExecute() 调用。
  • 你还打电话给setInvalidated(),为什么?
  • 没有任何效果,所以只是尝试不同的东西。
猜你喜欢
  • 2014-09-04
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多