onbackpressed()

时间:2017-01-02 20:11:54

标签: java android android-recyclerview notifydatasetchanged

  • 后退按钮后,我的recyclerview没有正确更新 按压。
  • 在按下后退按钮之前,recyclerview正常工作
  • 数据已正确更新(在日志中显示),但Recyclerview未反映更改
  • 处理程序的目的是轮询数据库以获取通知(正常工作)
  • 每次都会显示通知toast
  • 我没有收到任何错误

如果我能提供任何其他信息,请不要犹豫。

主要

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_room);
        recView = (RecyclerView) findViewById(R.id.recyclerViewMessages);
        linearLayoutManager = new LinearLayoutManager(this) {};
        linearLayoutManager.setReverseLayout(true);
        recView.setLayoutManager(linearLayoutManager);
        listData = (ArrayList) MessagingData.getMessageListData();
        adapter = new RecyclerViewAdapterMessaging(listData, this);
        recView.setAdapter(adapter);
        adapter.setItemClickCallback(this);
        final Handler h = new Handler();
        final int delay = 2000; //milliseconds

        h.postDelayed(new Runnable(){
          public void run(){

            Notify_Message_Async notify_message_async = new Notify_Message_Async(ctx);
            notify_message_async.execute(NOTIFICATION, message_id);

            System.out.println(global.getNotification());
            if(global.getNotification()==1){
                Toast.makeText(ctx, "Notified",
                        Toast.LENGTH_LONG).show();
                try {
                    refresh_receive();
                } catch (ExecutionException e) {
                    Toast.makeText(ctx, "catch",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    Toast.makeText(ctx, "catch",
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
            h.postDelayed(this, delay);
        }
    }, delay);
}

 public void refresh_receive() throws ExecutionException, InterruptedException {
        String method = "receive_message";
        Receive_Live_Message_Async receive_live_message_async = new Receive_Live_Message_Async(this);
        receive_live_message_async.execute(method, message_id).get();// Setup the message
        adapter.setListData((ArrayList)MessagingData.getMessageListData());
        adapter.notifyDataSetChanged();
        global.setNotification(0);//reset notification
 }

适配器:

public class RecyclerViewAdapterMessaging extends RecyclerView.Adapter<RecyclerViewAdapterMessaging.Holder> {
    private View v;
    private List<List_Item_Messaging> listData;
    private LayoutInflater inflater;
    Global global = new Global();
    private ItemClickCallback itemClickCallback;
    Context context;

    public interface ItemClickCallback {
        void onItemClick(View v, int p);

        void onSecondaryIconClick(int p);
    }

    public void setItemClickCallback(final ItemClickCallback itemClickCallback) {
        this.itemClickCallback = itemClickCallback;
    }

    public RecyclerViewAdapterMessaging(List<List_Item_Messaging> listData, Context c) {
        inflater = LayoutInflater.from(c);
        context = c;
        this.listData = listData;
    }

    @Override
    public int getItemViewType(int position) {//0 for self... /1 for Other
        List_Item_Messaging item = listData.get(position);
        //ENSURE GLOBAL USERNAME NOT NULL
        String other_username = item.getMessage_username();
        if (other_username == null) {
            ((Activity) context).finish();
        }
        if (item.getMessage_username().trim().equals(global.getUserName())) {
            System.out.println("The usernames are the same");
            return 0;
        } else {
            System.out.println("The usernames are the NOT same");
            return 1;
        }

    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case 0:
                View view = inflater.inflate(R.layout.chat_thread, parent, false);// Self
                v = view;
                break;
            case 1:
                View view2 = inflater.inflate(R.layout.chat_thread_other, parent, false);// Not self
                int width2 = global.getScreenWidth();
                v = view2;
                break;
        }
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        List_Item_Messaging item = listData.get(position);
        holder.conversation.setText(item.getMessage_conversation());
    }

    public void setListData(ArrayList<List_Item_Messaging> exerciseList) {
        this.listData.clear();
        this.listData.addAll(exerciseList);
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    class Holder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView thumbnail;
        //ImageView secondaryIcon;
        TextView conversation;
        View message_container;

        public Holder(View itemView) {
            super(itemView);

            conversation = (TextView) itemView.findViewById(R.id.conversation_textview);
            message_container = itemView.findViewById(R.id.message_container);
            message_container.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.message_container) {
                itemClickCallback.onItemClick(v, getAdapterPosition());
            } else {
                itemClickCallback.onSecondaryIconClick(getAdapterPosition());
            }
        }
    }

    public void clearItems() {
        listData.clear();
        this.notifyDataSetChanged();
    }
}

我已将以下内容引用为无解决方案:

1 个答案:

答案 0 :(得分:0)

在代码中稍作修改

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
    recView = (RecyclerView) findViewById(R.id.recyclerViewMessages);
    linearLayoutManager = new LinearLayoutManager(this) {};
    linearLayoutManager.setReverseLayout(true);
    recView.setLayoutManager(linearLayoutManager);
    // change here
    if (listData != null)
        listData.clear();
    else listData = new <> ArrayList();
    listData.addAdd((ArrayList)MessagingData.getMessageListData());
    adapter = new RecyclerViewAdapterMessaging(listData, this);
    recView.setAdapter(adapter);
    adapter.setItemClickCallback(this);
    final Handler h = new Handler();
    final int delay = 2000; //milliseconds

然后在这里做一个小改动

public void refresh_receive() throws ExecutionException, InterruptedException {
        String method = "receive_message";
        Receive_Live_Message_Async receive_live_message_async = new Receive_Live_Message_Async(this);
        receive_live_message_async.execute(method, message_id).get();// Setup the message
        // changing here
        dataList.clear();
        dataList.addAdd((ArrayList)MessagingData.getMessageListData())
        adapter.setListData(dataList);
        adapter.notifyDataSetChanged();
        global.setNotification(0);//reset notification
 }

您的代码中的另一个问题是,您正在使用receive_live_message_async AsyncTask 将更新代码放在onPostExecute

public class receive_live_message_async extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] objects) {
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Object o) {
            // call your refresh_receive(); here
            super.onPostExecute(o);
        }
    }

同样在你调用receive_live_message_async.execute()时;在onPostExecute中更新您的recyclerView

    @Override
    protected void onPostExecute(Object o) {
        dataList.clear();
        dataList.addAll((ArrayList)MessagingData.getMessageListData());
        adapter.notifyDataSetChanged();
        super.onPostExecute(o);
    }