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);
}
}
请帮助我解决这个问题...
答案 0 :(得分:1)
我认为您应该清楚地初始化适配器和recyclerview。
allItemsOfOrder可以是全局的
List<NewOrderEntryModel> allItemsOfOrder = new ArrayList<>();
以下面的代码为例:
recyclerView = (RecyclerView) findViewById(R.id.recycerViewOrderNewItem);
mAdapter = new NewOrderEntryAdapter(this,allItemsOfOrder);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
然后您应该将订单添加到列表
allItemsOfOrder可以是全局的
allItemsOfOrder.add(/*Something*/);
然后,您应按如下所示通知适配器...
mAdapter.notifyDataSetChanged();
您可以将此link用作参考。