Java 如何在没有列表位置的情况下重新加载recyclerview行?

Java 如何在没有列表位置的情况下重新加载recyclerview行?,java,android,arraylist,android-recyclerview,Java,Android,Arraylist,Android Recyclerview,我有一个自定义对象的ArrayList。我将此ArrayList传递给我的RecyclerView适配器。当事件发生时,我需要从ArrayList中检索包含具有唯一值的变量的对象,对该对象进行更改,然后重新加载与该对象对应的ViewHolder。我如何做到这一点 问题是我没有ArrayList中对象的位置,因为位置一直在动态变化。我只是有一个唯一的值对应于一个对象,我想在这个对象的基础上进行更改 我尝试执行list.clear()操作,然后使用更新的值将对象重新分配给ArrayList,然后调用

我有一个自定义对象的ArrayList。我将此ArrayList传递给我的RecyclerView适配器。当事件发生时,我需要从ArrayList中检索包含具有唯一值的变量的对象,对该对象进行更改,然后重新加载与该对象对应的ViewHolder。我如何做到这一点

问题是我没有ArrayList中对象的位置,因为位置一直在动态变化。我只是有一个唯一的值对应于一个对象,我想在这个对象的基础上进行更改

我尝试执行list.clear()操作,然后使用更新的值将对象重新分配给ArrayList,然后调用adapter.notifyDataSetChanged(),但没有成功

编辑: 我已经在下面提供了包含recyclerview的活动的代码。实际的代码要大得多,所以我决定编辑它,只显示关于这个问题的必要细节。请让我知道,如果我需要添加更多的代码

因此,这里基本上发生的是,IMActivity绑定到一个服务,并在绑定时将其上下文传递给该服务。当服务中发生事件时,将更改数据库值,并在服务中调用updateActivity()

我想要updateActivity()做的是检查chatMsgList中是否有一个包含具有唯一值的变量的ChatMsg对象,并重新加载与该ChatMsg对象对应的viewholder

如果我需要进一步澄清,请告诉我

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;
    }
}
public类IMActivity扩展appcompativity实现Sertivity{
私人回收站;
专用iAdapter iAdapter;
私有列表chatMsgList=newarraylist();
编辑文本;
图像按钮发送;
TalkSeeService.TalkSeeBinder TalkSeeBinder;
字符串用户名[]=新字符串[10];
字符串otherUserName;
列出msgList;
DBAct数据库;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
bindService(新意图(com.abdralabs.talksee.IMActivity.this,TalkSeeService.class),新服务连接(){
@凌驾
服务连接上的公共无效(组件名称,IBinder服务){
talkSeeBinder=(TalkSeeService.talkSeeBinder)服务;
talkSeeBinder.setCertivity(com.abdralabs.talksee.IMActivity.this);
}
@凌驾
ServiceDisconnected上的公共无效(组件名称){
talkSeeBinder.SetCertivity(空);
talkSeeBinder=null;
}
},绑定(自动创建);
setContentView(R.layout.activity_im);
recyclerView=(recyclerView)findViewById(R.id.rv_im);
imAdapter=新的imAdapter(chatMsgList);
LinearLayoutManager布局管理器=新的LinearLayoutManager(getApplicationContext());
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(iAdapter);
ibSend=(ImageButton)findViewById(R.id.ib_send);
etIM=(EditText)findViewById(R.id.et_im);
ibSend.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//从EDITTEXT中获取文本,然后将其添加到数据库中
updateChatData();
}
});
db=新的DBAct(com.abdralabs.talksee.IMActivity.this,otherUserName);
msgList=db.getAllMessages();
db.close();
prepareChatData();
}
私有void prepareChatData(){

对于(int i=0;i到您的iAdapter添加方法

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

请将您的代码放在此处。发布您到目前为止所做的工作,以查看您的位置。我已在编辑中提供了代码。请检查。您是否尝试在适配器中创建一个方法,从其项目列表中检索所需的对象,根据需要进行更新,然后调用
notifyDataSetChanged()
?可能存在重复的
public void setChatMsgList(List<ChatMsg> chatMsgList ) {
    this.chatMsgList = chatMsgList;
}
{
    ...
    chatMsgList.add(chatMsg); 
}
imAdapter.setChatMsgList(chatMsgList);
imAdapter.notifyDataSetChanged();