0

I have an ArrayList of a custom object. This ArrayList I pass to my RecyclerView Adapter. When an event occurs I need to retrieve the object from the ArrayList that contains a variable with a unique value, make changes to that object and then reload the ViewHolder corresponding to that object. How do I achieve this?

The problem is that I don't have the position of the object within the ArrayList because the positions keep changing dynamically. I just have this unique value corresponding to a object on the basis of which I want to make changes.

I tried doing list.clear() then reassigning objects to the ArrayList with the updated values and then calling adapter.notifyDataSetChanged() but it didn't work.

EDIT: I have provided the code below of the Activity which contains the recyclerview. The actual code is much larger so I decided to edit it to only show the necessary details regarding this question. Please let me know if I need to add more code.

So, basically what happens here is that IMActivity is bound to a service and passes its context to the service while binding. When an event occurs in the service a database value is changed and updateActivity() is called in the service.

What I want updateActivity() to do is to check the chatMsgList for a ChatMsg object that contains a variable that has a unique value and reload the viewholder corresponding to that ChatMsg object.

Please let me know if I need to clarify further.

public class IMActivity extends AppCompatActivity implements Sertivity {

    private RecyclerView recyclerView;
    private ImAdapter imAdapter;
    private List<ChatMsg> chatMsgList = new ArrayList<>();
    EditText etIM;
    ImageButton ibSend;
    TalkSeeService.TalkSeeBinder talkSeeBinder;
    String usernames[]= new String[10];
    String otherUserName;
    List<Msg> msgList;
    DBAct db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bindService(new Intent(com.abdralabs.talksee.IMActivity.this, TalkSeeService.class), new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                talkSeeBinder = (TalkSeeService.TalkSeeBinder)service;
                talkSeeBinder.setSertivity(com.abdralabs.talksee.IMActivity.this);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                talkSeeBinder.setSertivity(null);
                talkSeeBinder = null;
            }
        },BIND_AUTO_CREATE);

        setContentView(R.layout.activity_im);
        recyclerView = (RecyclerView)findViewById(R.id.rv_im);
        imAdapter = new ImAdapter(chatMsgList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(imAdapter);

        ibSend = (ImageButton)findViewById(R.id.ib_send);
        etIM = (EditText)findViewById(R.id.et_im);

        ibSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //THE TEXT FROM THE EDITTEXT IS TAKEN AND THEN ADDED TO THE DATABASE
                updateChatData();
            }
        });

        db = new DBAct(com.abdralabs.talksee.IMActivity.this,otherUserName);
        msgList = db.getAllMessages();
        db.close();
        prepareChatData();
    }


    private void prepareChatData() {
        for (int i=0; i<msgList.size(); i++) {
            ChatMsg chatMsg = new ChatMsg(msgList.get(i).getMessage(),
                    convertToDayDateTime(Long.valueOf(msgList.get(i).getTime())),
                    msgList.get(i).getOther(),
                    getReceiptFromDelivered(i));
            chatMsgList.add(chatMsg);
        }
        imAdapter.notifyDataSetChanged();
    }

    private void updateChatData(){
        msgList = db.getAllMessages();
        db.close();
        int i = msgList.size() - 1;
        ChatMsg chatMsg = new ChatMsg(msgList.get(i).getMessage(),
                convertToDayDateTime(Long.valueOf(msgList.get(i).getTime())),
                msgList.get(i).getOther(),
                getReceiptFromDelivered(i));
        chatMsgList.add(chatMsg);
        imAdapter.notifyDataSetChanged();
        recyclerView.smoothScrollToPosition(imAdapter.getItemCount());
    }

    @Override
    public void updateActivity() {

        chatMsgList.clear();
        prepareChatData();
        recyclerView.smoothScrollToPosition(imAdapter.getItemCount());
    }



    @Override
    public void callActivityMethod() {
        updateChatData();
    }


    private String convertToDayDateTime(long timestamp){
        Calendar cal = Calendar.getInstance();
        TimeZone tz = cal.getTimeZone();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm");
        sdf.setTimeZone(tz);
        String localTime = sdf.format(new Date(timestamp));

        return localTime;
    }

    private String getReceiptFromDelivered(int position){
        String receipt;
        if (msgList.get(position).getDelivered().equals("0")){
            receipt = "...";
        }else {
            receipt = "S";
        }
        return receipt;
    }
}

IMAdapter.java

        public class ImAdapter extends RecyclerView.Adapter <RecyclerView.ViewHolder> {
        private List<ChatMsg> chatMsgList = new ArrayList<ChatMsg>();
        final int VIEW_TYPE_USER = 1;
        final int VIEW_TYPE_OTHER = 0;
        public ImAdapter(List<ChatMsg> chatMsgList){
        this.chatMsgList = chatMsgList;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==VIEW_TYPE_USER){
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_msg_user,parent,false);
        return new ImUserViewHolder(itemView);
        }
    else {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_msg_other,parent,false);
        return new ImOtherViewHolder(itemView);
        }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType()==VIEW_TYPE_USER){
        ChatMsg chatMsg = chatMsgList.get(position);
        ImUserViewHolder imUserViewHolder = (ImUserViewHolder)holder;
        imUserViewHolder.msg.setText(chatMsg.getMsg());
        imUserViewHolder.timeStamp.setText(chatMsg.getTime());
        }
    else {
        ChatMsg chatMsg = chatMsgList.get(position);
        ImOtherViewHolder imOtherViewHolder = (ImOtherViewHolder) holder;
        imOtherViewHolder.msg.setText(chatMsg.getMsg());
        imOtherViewHolder.timeStamp.setText(chatMsg.getTime());
        }
        }


        @Override
        public int getItemViewType(int position) {
        ChatMsg chatMsg = chatMsgList.get(position);
        if (chatMsg.getOther().equals("true")){
        return VIEW_TYPE_OTHER;
        }
    else {
        return VIEW_TYPE_USER;
        }
        }

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

        public class ImOtherViewHolder extends RecyclerView.ViewHolder{
        public TextView msg;
        public TextView timeStamp;
        public ImOtherViewHolder(View itemView) {
        super(itemView);
        msg = (TextView)itemView.findViewById(R.id.tv_chat_msg_other);
        timeStamp = (TextView)itemView.findViewById(R.id.tv_time_chat_msg_other);
        }
        }

        public class ImUserViewHolder extends RecyclerView.ViewHolder{
        public TextView msg;
        public TextView timeStamp;
        public ImUserViewHolder(View itemView) {
        super(itemView);
        msg = (TextView)itemView.findViewById(R.id.tv_chat_msg_user);
        timeStamp = (TextView)itemView.findViewById(R.id.tv_time_chat_msg_user);
        }
        }


}
wolfrevo kcats
  • 110
  • 1
  • 8

1 Answers1

0

To you IMAdapter add method

public void setChatMsgList(List<ChatMsg> chatMsgList ) {
    this.chatMsgList = chatMsgList;
}

and in your IMActivity in method prepareChatData call this new setChatMsgList method

{
    ...
    chatMsgList.add(chatMsg); 
}
imAdapter.setChatMsgList(chatMsgList);
imAdapter.notifyDataSetChanged();
Honza Musil
  • 320
  • 2
  • 10