gpt4 book ai didi

java - Recyclerview显示具体数据

转载 作者:行者123 更新时间:2023-12-02 09:43:05 25 4
gpt4 key购买 nike

我有两种类型的用户。第一个用户可以在回收器 View 中看到所有数据。第二个用户只能在回收者 View 中看到特定数据。

现在只能显示具体的数据,第二个用户点击后,就会显示第一个数据,而不是按照位置显示。

如何修复用户在右侧显示上的点击并将继续处理正确的数据?

MainActivity.java。准备根据情况显示回收者 View 。

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) {

}
});
}
}

最佳答案

不要使用 onBindViewHolder 中的 position,而是使用 mainItem 名称来区分点击事件。

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;
}

}
});

此外,我使用了小写字符串比较,这将降低字符串匹配的风险。

关于java - Recyclerview显示具体数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56900463/

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