gpt4 book ai didi

android - 如何在 RecyclerView 中显示多个项目类型?

转载 作者:行者123 更新时间:2023-11-29 03:16:42 25 4
gpt4 key购买 nike

我正在用 java 创建一个 android 应用程序,我正在使用 Room 数据库。我有三个表:

Users table( user_id and user_name) 
Products table( product_id and product_name)
Orders table ( order_id , user_id and product_id )

我正在使用 ViewModel 查询信息并将其填充到 UI。

我有一个查询从 Orders 表中获取 user_idproduct_id。我有这种情况:user_id 号 1 订购了 3 件产品。

private void setUpViewModel() {

viewModel.getUserProduct(1).observe(this,new Observer<List<OrdersTable>>() {
@Override
public void onChanged(@Nullable List<OrdersTable> order) {
mAdapter.setUserList((ArrayList<OrdersTable>) order);
}
}
);
}

此查询将为用户 1 获取产品 1,2 和 3

我想在我的 recylcerView 中显示用户名和产品名称,但我不知道如何在 recylcerView< 中显示两种不同类型的表(用户表和产品表)/.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView

android:id="@+id/tv_userName_xml"
android:text="user name"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/tv_prodcutName_xml"
android:layout_marginLeft="40dp"
android:text="product"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


</LinearLayout>

enter image description here

 public class MyAdapter extends   RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
ArrayList<OrderTable> order_list;
ArrayList<UserTable> user_list;
ArrayList<ProductTable> product_list;

public MyAdapter(Context context) {
this.mContext = context;
}

@NonNull
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_ts, parent, false);
MyAdapter.MyViewHolder viewHolder = new MyAdapter.MyViewHolder(view);

return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyAdapter.MyViewHolder holder, int position) {

OrderTable orders = order_list.get(position);
UserTable user = user_list.get(position);
ProductTable productTable = product_list.get(position);

holder.tv_userName.setText(user.getUserName());

holder.tv_productName.setText(productTable.getProductName());
}


@Override
public int getItemCount() {
if (order_list == null) {
return 0;
}
return order_list.size();
}


public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_userName, tv_productName;

public MyViewHolder(View itemView) {
super(itemView);
tv_userName= itemView.findViewById(R.id.tv_userName_xml);
tv_productName= itemView.findViewById(R.id.tv_prodcutName_xml);

}

}

public void setTs_list(ArrayList<OrderTable> ts) {
this.order_list = ts;
notifyDataSetChanged();
}


}

最佳答案

您可以加入这些表中的数据并将结果绑定(bind)到一个名为 ResultModel 的自定义模型类中您可以从中获取 user_nameproduct_name那个 ResultModel

用户

enter image description here

产品

enter image description here

t_order

enter image description here

加入查询:

select u.user_name, p.product_name from user u, product p, t_order o where o.user_id = u.user_id and o.product_id = p.product_id

结果

enter image description here

创建一个模型类ResultModel

public class ResultModel {
public String getUser_name() {
return user_name;
}

public String getProduct_name() {
return product_name;
}

private String user_name;
private String product_name;
}

更新观察者

注意:将 OrdersTable 替换为 ResultModel 在您的 Adapter 中也需要

viewModel.getUserProduct(1).observe(this,new Observer<List<ResultModel>>() {
@Override
public void onChanged(@Nullable List<ResultModel> order) {
mAdapter.setUserList((ArrayList<ResultModel>) order);
}
}
);
}

关于android - 如何在 RecyclerView 中显示多个项目类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54710874/

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