我有2种类型的用户。第一个用户可以在回收者视图中查看所有数据。第二个用户只能在“回收者”视图中查看特定数据。
现在我只能显示特定数据,第二个用户单击后,它将显示第一个数据,而不是位置。
如何在正确的显示上修复用户单击并在正确的数据处继续操作?
MainActivity.java。准备根据条件显示回收者视图。
private void prepareMainMenu(final String type) {
MainItem mainItem;
switch (type) {
case "Customer":
mainItem = new MainItem("Customer", imageViewMain[1]);
mainItemList.add(mainItem);
break;
case "Packages":
mainItem = new MainItem("Packages", imageViewMain[2]);
mainItemList.add(mainItem);
break;
case "Live Chat":
mainItem = new MainItem("Live Chat", imageViewMain[3]);
mainItemList.add(mainItem);
break;
case "Transaction":
mainItem = new MainItem("Transaction", imageViewMain[5]);
mainItemList.add(mainItem);
break;
case "Profit Report":
mainItem = new MainItem("Profit Report", imageViewMain[4]);
mainItemList.add(mainItem);
break;
case "Owner":
mainItem = new MainItem("Admin", imageViewMain[0]);
mainItemList.add(mainItem);
mainItem = new MainItem("Customer", imageViewMain[1]);
mainItemList.add(mainItem);
mainItem = new MainItem("Packages", imageViewMain[2]);
mainItemList.add(mainItem);
mainItem = new MainItem("Live Chat", imageViewMain[3]);
mainItemList.add(mainItem);
mainItem = new MainItem("Profit Report", imageViewMain[4]);
mainItemList.add(mainItem);
mainItem = new MainItem("Transaction", imageViewMain[5]);
mainItemList.add(mainItem);
break;
}
//Display
mainViewAdapter.notifyDataSetChanged();
}
MainViewAdapter.java(用户将在其中单击)
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
MainItem mainItem = mainItemList.get(position);
holder.getTextViewTitle().setText(mainItem.getNameMain());
Glide.with(context)
.load(mainItem.getImageMain())
.into(holder.imageViewMain);
holder.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Check the position
switch (position) {
case 0:
context.startActivity(new Intent(context, AdminActivity.class));
break;
case 1:
context.startActivity(new Intent(context, CustomerActivity.class));
break;
case 2:
context.startActivity(new Intent(context, PackagesSelectorActivity.class));
break;
case 3:
context.startActivity(new Intent(context, LiveChatActivity.class));
break;
case 4:
context.startActivity(new Intent(context, ProfitReportActivity.class));
break;
case 5:
context.startActivity(new Intent(context, TransactionActivity.class));
break;
}
}
});
}
已更新: 这是prepareMainMenu方法的去处。
private void retrieveData() {
if (firebaseUser != null) {
databaseReference.child("admin").child(firebaseUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Admin admin = dataSnapshot.getValue(Admin.class);
if (admin != null) {
fullName = admin.getFullName();
email = admin.getEmail();
type = admin.getType();
//Then display
textViewFullName.setText(fullName);
textViewEmail.setText(email);
textViewType.setText(type);
//Check the type of admin
switch (type) {
case "Customer":
prepareMainMenu("Customer");
break;
case "Packages":
prepareMainMenu("Packages");
break;
case "Live Chat":
prepareMainMenu("Live Chat");
break;
case "Transaction":
prepareMainMenu("Transaction");
break;
case "Profit Report":
prepareMainMenu("Profit Report");
break;
case "Owner":
prepareMainMenu("Owner");
break;
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
答案 0 :(得分:1)
使用mainItem名称来区分点击事件,而不是使用onBindViewHolder
内的位置。
holder.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (mainItem.getNameMain().toLowerCase()) {
case "admin":
context.startActivity(new Intent(context, AdminActivity.class));
break;
case "customer":
context.startActivity(new Intent(context, CustomerActivity.class));
break;
case "packages":
context.startActivity(new Intent(context, PackagesSelectorActivity.class));
break;
case "live chat":
context.startActivity(new Intent(context, LiveChatActivity.class));
break;
case "profit report":
context.startActivity(new Intent(context, ProfitReportActivity.class));
break;
case "transaction":
context.startActivity(new Intent(context, TransactionActivity.class));
break;
}
}
});
此外,我使用了小写的字符串比较,这将降低字符串匹配的风险。
答案 1 :(得分:0)
如果从任何用户角色的角度查看此回收器,您将发现根据当前登录角色对数据进行了过滤。 因此,您可以轻松地将用户角色添加到MainItem类中,作为从此类创建的任何对象的属性。 并且在prepareMainMenu函数中,您可以根据当前登录角色轻松地过滤数据
首先,您可以为不同的角色定义一个枚举
public enum Roles {
CustomerRole, AdminRole
}
然后将此角色作为属性传递给MainItem类,例如
MainItem("Customer", imageViewMain[1],Roles.Customer);
并且onBindView方法将像这样
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
MainItem mainItem = mainItemList.get(position);
holder.getTextViewTitle().setText(mainItem.getNameMain());
Glide.with(context)
.load(mainItem.getImageMain())
.into(holder.imageViewMain);
holder.getView().setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
switch (mainItem.role) {
case Roles.CustomerRole:
context.startActivity(new Intent(context, CustomerActivity.class));
break;
case Roles.Admin:
context.startActivity(new Intent(context, AdminActivity.class));
break;
}
});
}
另一个看起来更好的解决方案是像这样将类名传递给dto
class MainItem {
String title;
String image;
Class className;
public MainItem(String title, String image, Class className) {
this.title = title;
this.image = image;
this.className = className;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
public Class getClassName() {
return className;
}
}
,您的数据创建将是
MainItem("Customer", imageViewMain[1],CustomerActivity.class);
和您的onBindView方法将如下所示
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
MainItem mainItem = mainItemList.get(position);
holder.getTextViewTitle().setText(mainItem.getNameMain());
Glide.with(context)
.load(mainItem.getImageMain())
.into(holder.imageViewMain);
holder.getView().setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
context.startActivity(new Intent(context, mainItem.getClassName));
});
}