gpt4 book ai didi

java - 回收站 View 中的重复项,notifydatasetchanged 不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:37 26 4
gpt4 key购买 nike

我在单击适配器内的布局时运行事务,但是当我单击布局时,事务成功运行。但它在我的 RecyclerView 中创建了另一个项目。我在事务中尝试了 notifydatasetChanged() 但它会产生错误:

CalledFromWrongThreadException: Only the original thread that created a view 
hierarchy can touch its views.

然后我尝试将 notifyDataSetChanged() 放在事务之外,但它不起作用。以下是我正在做的事情:

holder.follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
DatabaseReference mWorkDatabase =
FirebaseDatabase.getInstance().getReference()
.child("WorkforWorkMen").child(work_id).child("follower");
mWorkDatabase.runTransaction(new Transaction.Handler() {
@Override
public Transaction.Result doTransaction(MutableData
mutableData) {
if(holder.followText.getText().equals("Follow")) {
holder.followText.setText("Following");
FirebaseAuth mAuth = FirebaseAuth.getInstance();
String current_user =
mAuth.getCurrentUser().getUid();
DatabaseReference mFollowingDatabase =
FirebaseDatabase.getInstance().getReference()
.child("WorkFollowing").child(current_user).child(work_id);
final Map fol = new HashMap();
fol.put("follower",work_id);
mFollowingDatabase.setValue(fol);
mutableData.setValue
(Integer.parseInt(mutableData.getValue().toString()) + 1);
}

else {
holder.followText.setText("Follow");
FirebaseAuth mAuth = FirebaseAuth.getInstance();
String current_user =
mAuth.getCurrentUser().getUid();
DatabaseReference mFollowingDatabase =
FirebaseDatabase.getInstance().getReference()
.child("WorkFollowing").child(current_user).child(work_id);
mFollowingDatabase.setValue(null);
mutableData.setValue(Integer
.parseInt(mutableData.getValue().toString()) - 1);
}

return Transaction.success(mutableData);
}
@Override
public void onComplete(DatabaseError databaseError,
boolean b, DataSnapshot dataSnapshot) {
Toast.makeText(context,"error on complete"+
String.valueOf(databaseError), Toast.LENGTH_SHORT).show();
Log.i("eror here", "onComplete:
"+String.valueOf(databaseError));
}
});
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "here",
Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}
});

}
});

我想当我将数据添加到我的列表时,关注者值(value)的变化会导致它添加一个新项目,但我不确定

 private void loadMessages(){
if(keys.size() == 0){
messagesList.clear();
Toast.makeText(getContext(), " size zero no results found",
Toast.LENGTH_SHORT).show();
}
else {
messagesList.clear();
for (int i = 0; i < 5; i++) {
if (i>=keys.size()){
Toast.makeText(getContext(), "list is less than 5 item",
Toast.LENGTH_SHORT).show();
}
else {
listPos = i;
String id = keys.get(i);
mWorkDatabase.child(id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String messageKey = dataSnapshot.getKey();
NewWork message = dataSnapshot.getValue(NewWork.class);
itemPos++;
messagesList.add(message);
mAdapter.notifyDataSetChanged();
mRecyclerView2.scrollToPosition(messagesList.size() - 1);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
}
}

最佳答案

您正在从后台线程调用notifyDataSetChanged(),您需要从ui线程调用它,如下所示

new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {

notifyDataSetChanged();
}
});

关于java - 回收站 View 中的重复项,notifydatasetchanged 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50926149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com