我是Android开发的新手,需要帮助将Firebase Realtime数据库中的数据填充到FirebaseRecyclerAdapter
中。
我的应用程序包含一个研讨会列表。用户可以使用GitHub身份验证登录到我的测试应用程序,如果按下了相关按钮,我会收到用户电子邮件以登录或注销研讨会。
因此,在某些情况下,我的工作室包含Firebase中以下示例的用户列表。
问题是我需要在回收者视图中突出显示用户已经登录的研讨会。
我遇到的主要问题是在填充每个研讨会的信息时。以后我可以轻松地让用户分别列出,但是从一开始我就不知道如何在一个不错的Workshop对象中做到这一点。
我迷路了。请帮忙。
我的FirebaseRecyclerAdapter
:
public class WorkshopsFirebaseRecyclerAdapter extends FirebaseRecyclerAdapter<Workshop, WorkshopViewHolder> {
private Context mContext;
private ProgressBar progressBar;
public WorkshopsFirebaseRecyclerAdapter(FirebaseRecyclerOptions<Workshop> options, Context context, ProgressBar view) {
super(options);
mContext = context;
progressBar = view;
}
@Override
public void onDataChanged() {
if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}
}
@Override
protected void onBindViewHolder(@NonNull final WorkshopViewHolder holder, final int position, @NonNull final Workshop model) {
final Workshop workshop = getItem(position);
final String id = getRef(position).getKey();
holder.date.setText(getUserFriendlyDate(workshop.getDate()));
holder.description.setText(workshop.getDescription());
if (workshop.getWorkshopAttendants() != null){
Timber.i("onBindViewHolder workshop is %s", workshop.toString());
}
holder.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TinyDB tinyDB = new TinyDB(mContext);
tinyDB.putObject(mContext.getString(R.string.workshop_tinydb_key), workshop);
Intent intent = new Intent(mContext, WorkshopDetailsActivity.class);
intent.putExtra(mContext.getString(R.string.open_workshop_details_intent_key),
WorkshopDetailsActivity.INTENT_OPEN_UPDATE_WORKSHOP_DETAILS);
intent.putExtra(mContext.getString(R.string.current_workshop_id_key), id);
mContext.startActivity(intent);
}
});
}
@Override
public WorkshopViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.workshop_rv_item, viewGroup, false);
return new WorkshopViewHolder(itemView);
}
private String getUserFriendlyDate(String dateOld){
Date date = new Date();
SimpleDateFormat oldDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.UK);
try {
date = oldDateFormat.parse(dateOld);
} catch (ParseException e) {
e.printStackTrace();
}
SimpleDateFormat newDateFormat = new SimpleDateFormat("dd MMM yyyy", Locale.UK);
return newDateFormat.format(date);
}
}
设置适配器和查询的方法:
private void loadWorkshopsForCity(String city) {
Query query;
if (city.equals(getString(R.string.spinner_cities_all_value))) {
query = FirebaseDatabase.getInstance()
.getReference(getString(R.string.firebase_root_name))
.child(getString(R.string.firebase_workshops_root_name))
.limitToLast(50);
} else {
query = FirebaseDatabase.getInstance()
.getReference(getString(R.string.firebase_root_name))
.child(getString(R.string.firebase_workshops_root_name))
.orderByChild(getString(R.string.firebase_workshop_city_name_key))
.equalTo(city);
}
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot workshopSnapshot : dataSnapshot.getChildren()) {
Workshop workshop = workshopSnapshot.getValue(Workshop.class);
for(DataSnapshot usersSnapshot : workshopSnapshot.child("users").getChildren()){
if (usersSnapshot.exists()){
WorkshopAttendant user = usersSnapshot.getValue(WorkshopAttendant.class);
if (user!=null){
users.add(user);
}
}
}
if (users.size()!=0){
workshop.setWorkshopAttendants(users);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Timber.i(databaseError.toException());
}
});
FirebaseRecyclerOptions<Workshop> options =
new FirebaseRecyclerOptions.Builder<Workshop>()
.setQuery(query, Workshop.class)
.build();
if (adapter != null) {
adapter.stopListening();
}
adapter = new WorkshopsFirebaseRecyclerAdapter(options, this, progressBar);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(adapter);
adapter.startListening();
}
我的包含与会者列表的工作坊:
public class Workshop {
private String date;
private String time;
private String description;
private String name;
private String address;
private String city;
private List<WorkshopAttendant> workshopAttendants;
private boolean isChecked;
// empty constructor for firebase
public Workshop() {
}
public Workshop(String date, String time, String description, String name, String address, String city,
List<WorkshopAttendant> workshopAttendants) {
this.date = date;
this.time = time;
this.description = description;
this.name = name;
this.address = address;
this.city = city;
this.workshopAttendants = workshopAttendants;
} .....
和其他应该在Workshop对象内部的WorkshopAttendant。
public class WorkshopAttendant {
private String email;
private int role; // 1 - coach ; 0 - organiser ; 2 - student
public WorkshopAttendant(String userEmail, int role) {
this.email = userEmail;
this.role = role;
}
// for firebase
public WorkshopAttendant() {
}
答案 0 :(得分:0)
1)将Workshop模型中的Workshop参与者列出到Map用户= new HashMap <>();
2)为我的Recycler选项使用了新的SnapshotParser;
FirebaseRecyclerOptions<Workshop> options =
new FirebaseRecyclerOptions.Builder<Workshop>()
.setQuery(query, new SnapshotParser<Workshop>() {
@NonNull
@Override
public Workshop parseSnapshot(@NonNull DataSnapshot snapshot) {
GenericTypeIndicator<Workshop> t = new GenericTypeIndicator<Workshop>() {};
Workshop workshop = snapshot.getValue(t);
return workshop;
}
})
.build();
3)我在适配器中设置了用户检查功能,因此现在可以标记有用户签名的研讨会。
try {
Map<String, User> users = workshop.getValue();
if (users.size()!=0){holder.description.setBackgroundColor(mContext.getColor(R.color.colorPrimaryDark));
}
} catch (NullPointerException e){
Timber.i(e, "no users for %s", workshop.getDescription());
}
感谢@AlexMamo的耐心