我在RecyclerView中遇到重复项目时遇到问题。每当我上传到我的Firebase并再次打开main活动时,recyclerView中的重复主题便会重复之前的加载。如何从recyclerView中删除重复的物品。
我的主要活动的代码是
private RecyclerView mRecyclerView;
private RecyclerViewAdapter mAdapter;
private DatabaseReference mDatabaseReference;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posts);
setupBottomNavigation();
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseReference = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new RecyclerViewAdapter(MainActivity.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Button btn_post = (Button) findViewById(R.id.post_Main);
btn_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_posts = new Intent(MainActivity.this, Posts.class);
startActivity(intent_posts);
}
});
}
我的Adapter类中的代码是
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.rViewHolder> {
private Context mContext;
private List<Upload> mUploads;
public RecyclerViewAdapter(Context context, List<Upload> uploads){
mContext = context;
mUploads = uploads;
}
@Override
public rViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.card_view_recycler_view, parent, false);
return new rViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull rViewHolder viewHolder, int position) {
Upload uploadCurrent = mUploads.get(position);
viewHolder.textViewDescription.setText(uploadCurrent.getDescription());
Picasso.get().load(uploadCurrent.getImageUrl()).into(viewHolder.imageView);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class rViewHolder extends RecyclerView.ViewHolder{
public TextView textViewDescription;
public ImageView imageView;
public rViewHolder(View itemView){
super(itemView);
textViewDescription = itemView.findViewById(R.id.r_description);
imageView = itemView.findViewById(R.id.r_image_view);
}
}
答案 0 :(得分:0)
我认为发生这种情况是因为Firebase离线存储数据,请尝试以下方式初始化实例
ps -ef | grep process_name
if [ $? -eq 0 ]; then
echo "Process is running."
else
echo "Process is not running."
fi
应该工作。
答案 1 :(得分:0)
请尝试清除列表,然后再向其中添加数据,如下所示。
private RecyclerView mRecyclerView;
private RecyclerViewAdapter mAdapter;
private DatabaseReference mDatabaseReference;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_posts);
setupBottomNavigation();
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseReference = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.clear();
mUploads.add(upload);
}
mAdapter = new RecyclerViewAdapter(MainActivity.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Button btn_post = (Button) findViewById(R.id.post_Main);
btn_post.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent_posts = new Intent(MainActivity.this, Posts.class);
startActivity(intent_posts);
}
});
}
答案 2 :(得分:0)
在这种情况下,请尝试清除您的ArrayList mUploads
,然后再向其中添加项目,
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// Clear ArrayList
mUploads.clear();
for(DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new RecyclerViewAdapter(MainActivity.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
这对我而言效果很好,希望对您有所帮助。 Reference here.
答案 3 :(得分:0)
如果您卸载应用程序并重新安装,并且安装后首次运行该应用程序时列表中有重复项,则后端Firebase中将存在重复项。如果仅在您第二次运行该应用程序时存在重复项,则您的代码负责。
您显示的代码对我来说看起来不错。可能还有一些您没有显示的错误,例如您有重复的addValueEventListener
或您用List<Upload> mUploads
再次喂入适配器
许多人在这里建议您不必执行mUploads.clear();
,因为您是从onCreate()
填充数组的,女巫在活动生命周期中仅运行一次
如果您不在其他地方使用
,我会将private List<Upload> mUploads;
内的onCreate()
作为局部变量移动