【发布时间】:2019-01-14 07:42:49
【问题描述】:
recycerViewOrderNewItem 和offlineOrderProductListProductList 是两个recyclerviews,它们是在onCreate() 方法中初始化的。
recycerViewOrderNewItem = findViewById(R.id.recycerViewOrderNewItem);
recycerViewOrderNewItem.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
offlineOrderProductListProductList = findViewById(R.id.offlineOrderProductListProductList);
offlineOrderProductListProductList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
下面是我以List<>检索我的数据的地方
List<NewOrderEntryModel> allItemsOfOrder = new InitializeDatabase(OrderEntryActivity.this).myAppDatabaseInit.myDao().getAllNewOrderEntryModelByRefID(SalesID);
我正在为他们两个设置这样的适配器......
offlineOrderProductListProductList.setAdapter(new NewOrderEntryAdapter(OrderEntryActivity.this, (ArrayList<NewOrderEntryModel>) allItemsOfOrder));
recycerViewOrderNewItem.setAdapter(new NewOrderEntryAdapter(OrderEntryActivity.this, (ArrayList<NewOrderEntryModel>) allItemsOfOrder));
对于offlineOrderProductListProductList recyclerview 正在工作,但对于recycerViewOrderNewItem recyclerview 不工作
我已经调试了代码。 ArrayList 包含数据。
下面是我的适配器代码...
public class NewOrderEntryAdapter extends RecyclerView.Adapter<NewOrderEntryAdapter.NewOrderEntryAdapterViewHolder>{
private Context context;
private ArrayList<NewOrderEntryModel> newOrderEntryModels;
public NewOrderEntryAdapter(Context context, ArrayList<NewOrderEntryModel> newOrderEntryModels) {
this.context = context;
this.newOrderEntryModels = newOrderEntryModels;
}
@NonNull
@Override
public NewOrderEntryAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.list_item_order_entry_detail,parent,false);
return new NewOrderEntryAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull NewOrderEntryAdapterViewHolder holder, final int position) {
NewOrderEntryModel orderEntryModel = newOrderEntryModels.get(position);
//Data
final String name = orderEntryModel.getProductName();
final String totalPrice = String.valueOf(orderEntryModel.getPBSalesTotal());
final String code = String.valueOf(orderEntryModel.getPCode());
final String quantity = String.valueOf(orderEntryModel.getPBInQty());
final String price = String.valueOf(orderEntryModel.getPBSalesPrice());
final String productID = String.valueOf(orderEntryModel.getPBProductID());
// Binding
holder.tvProductNameOrderEntry.setText(name);
holder.tvProductTotalPriceOrderEntry.setText(totalPrice);
holder.tvProductCodeOrderEntry.setText(code);
holder.tvProductQuantityOrderEntry.setText(quantity);
holder.tvProductPriceOrderEntry.setText(price);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(context, "Reference id: "+orderEntryModel.getPBRefID()+" Table ID: "+orderEntryModel.getID(), Toast.LENGTH_SHORT).show();
if(orderEntryModel.getPBRefID()==null){
//Toast.makeText(context, "Reference id: "+orderEntryModel.getPBRefID()+" Table ID: "+orderEntryModel.getID(), Toast.LENGTH_SHORT).show();
openDetailActivity(String.valueOf(position),"","",name,totalPrice,code,quantity,price,productID);
}else {
Toast.makeText(context, "Reference id: "+orderEntryModel.getPBRefID()+" Table ID: "+orderEntryModel.getID(), Toast.LENGTH_SHORT).show();
openDetailActivity(String.valueOf(position),Integer.toString(orderEntryModel.getID()),orderEntryModel.getPBRefID(),name,totalPrice,code,quantity,price,productID);
}
//Toast.makeText(context, context.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return newOrderEntryModels.size();
}
public class NewOrderEntryAdapterViewHolder extends RecyclerView.ViewHolder{
public TextView tvProductNameOrderEntry
,tvProductTotalPriceOrderEntry
,tvProductCodeOrderEntry
,tvProductQuantityOrderEntry
,tvProductPriceOrderEntry;
public NewOrderEntryAdapterViewHolder(View itemView) {
super(itemView);
tvProductNameOrderEntry = itemView.findViewById(R.id.tvProductNameOrderEntry);
tvProductTotalPriceOrderEntry = itemView.findViewById(R.id.tvProductTotalPriceOrderEntry);
tvProductCodeOrderEntry = itemView.findViewById(R.id.tvProductCodeOrderEntry);
tvProductQuantityOrderEntry = itemView.findViewById(R.id.tvProductQuantityOrderEntry);
tvProductPriceOrderEntry = itemView.findViewById(R.id.tvProductPriceOrderEntry);
}
}
public void openDetailActivity(String position,
String id,
String pbRef,
String productName,
String totalPrice,
String productCode,
String quantity,
String productPrice,
String productID){
Intent intent = new Intent(context, NewItemDetailActivity.class);
intent.putExtra("position",position);
intent.putExtra("id",id);
intent.putExtra("pbRef",pbRef);
intent.putExtra("productName",productName);
intent.putExtra("totalPrice",totalPrice);
intent.putExtra("productCode",productCode);
intent.putExtra("quantity",quantity);
intent.putExtra("productPrice",productPrice);
intent.putExtra("productID",productID);
context.startActivity(intent);
}
}
请帮我解决这个问题...
【问题讨论】:
-
请提供适配器代码。
-
1.您是否检查了 recyclerview 的 xml 以确保您没有使用 match-parent 作为 recyclerview 项目的高度? 2. 从你的 recyclerview 适配器类调试代码,以确保 recyclerview 确实看到了数组
-
通过评论每一个来检查recycleViews是否单独工作。如果这很可能是 XML
height=match_parent的问题。 -
我总是将 wrap_content 用于 recyclerView。
-
两个recyclerView是同一个页面?
标签: java android arraylist android-recyclerview