我正在使用recyclerview显示cardview元素,并为其制作了一个适配器。当我使用Arraylist存储值时,recyclerview按预期工作,元素显示出来。然而,我想使用链表,因为项目会不断添加和删除。当我尝试使用链表时,recyclerview不起作用,元素也不显示。我怎样才能解决这个问题?
我的班级代码:
public class AdminViewAlertsActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference database;
AlertAdapter alertAdapter;
ArrayList<AlertModel> list;
LinkedList<AlertModel> list2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_view_alerts);
recyclerView = findViewById(R.id.alerts_list);
database = FirebaseDatabase.getInstance().getReference("Alerts");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
list2 = new LinkedList<>();
alertAdapter = new AlertAdapter(this,list2);
recyclerView.setAdapter(alertAdapter);
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot:snapshot.getChildren()){
AlertModel alertModel = dataSnapshot.getValue(AlertModel.class);
list.add(alertModel);
alertAdapter.notifyDataSetChanged();
}
alertAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
我的适配器代码:
public class AlertAdapter extends RecyclerView.Adapter<AlertAdapter.AlertViewHolder> {
Context context;
LinkedList<AlertModel> list;
AlertAdapter adapter;
public AlertAdapter(Context context, LinkedList<AlertModel> list) {
this.context = context;
this.list = list;
this.adapter = this;
}
@NonNull
@Override
public AlertViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.alrtitem,parent,false);
return new AlertViewHolder(v);
}
interface Interactions{
public void refreshActivity();
}
@Override
public void onBindViewHolder(@NonNull AlertViewHolder holder, int position) {
AlertModel alertModel = list.get(position);
holder.date_and_time.setText(alertModel.getDate_time());
holder.alert.setText(alertModel.getNotification());
holder.viewOptions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(context, holder.viewOptions);
popup.inflate(R.menu.alertmenu);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.alertmenu1:
DatabaseReference dataRef = FirebaseDatabase.getInstance().getReference();
dataRef.child("Alerts").child(alertModel.getDate_time()).removeValue().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(context, "Removed", Toast.LENGTH_SHORT).show();
list.remove(holder.getBindingAdapterPosition());
adapter.notifyDataSetChanged();
}
});
default:
return false;
}
}
});
popup.show();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public static class AlertViewHolder extends RecyclerView.ViewHolder{
TextView date_and_time, alert, viewOptions;
public AlertViewHolder(@NonNull View itemView) {
super(itemView);
date_and_time = itemView.findViewById(R.id.date_and_time);
alert = itemView.findViewById(R.id.Alert);
viewOptions = itemView.findViewById(R.id.viewOptions);
}
}
}
感谢您的帮助!