在RecyclerView Firebase中强制转换arraylist [英] Cast arraylist in recyclerview firebase
问题描述
我有一组要从Firebase检索的数据.我正在使用recyclerview来显示数据,但是我的适配器无法正常工作.我尝试在适配器中添加arraylist,但这无法正常工作.这表示适配器未连接,并且我的活动为空.
I have an array of data which I am retrieving from firebase. I am using a recyclerview to display the data but my adapter is not working correctly.I tried adding the arraylist in the adapter but this is not working. It is saying the adapter is not attached and I am having a blank activity.
对此有任何帮助吗?这是我的详细信息.
Any help on this ? Here are my details.
模式类
public class Order {
private String ProductId;
private String ProductName;
private String Quantity;
public Order() {
}
public String getProductId() {
return ProductId;
}
public void setProductId(String productId) {
ProductId = productId;
}
public String getProductName() {
return ProductName;
}
public void setProductName(String productName) {
ProductName = productName;
}
public String getQuantity() {
return Quantity;
}
public void setQuantity(String quantity) {
Quantity = quantity;
}
public Order(String productId, String productName, String quantity) {
ProductId = productId;
ProductName = productName;
Quantity = quantity;
}
}
适配器
public class AllOrdersAdapter extends RecyclerView.Adapter<AllOrdersViewHolder> {
List<Order> myfoods;
public AllOrdersAdapter(List<Order> myfoods) {
this.myfoods = myfoods;
}
@NonNull
@Override
public AllOrdersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.allorders_layout,parent,false);
return new AllOrdersViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull AllOrdersViewHolder holder, int position) {
holder.foodname.setText(myfoods.get(position).getProductName());
holder.foodquantity.setText(myfoods.get(position).getQuantity());
holder.foodId.setText(myfoods.get(position).getProductId());
}
@Override
public int getItemCount() {
return myfoods.size();
}
}
测试类
public class Test extends AppCompatActivity {
FirebaseDatabase db;
DatabaseReference requests;
RecyclerView lstFoods;
RecyclerView.LayoutManager layoutManager;
TextView food_id,food_quan,food_name;
// List foods = new ArrayList<>();
// RecyclerView.Adapter<AllOrder> adapter;
// List<String> myOrders = new ArrayList<String>();
// ArrayList<String> foods=new ArrayList<>();
List<String> myfoods = new ArrayList<String>();
AllOrdersAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
//firebase
db = FirebaseDatabase.getInstance();
requests= db.getReference().child("Requests");
lstFoods = (RecyclerView)findViewById(R.id.lstAllFoods);
lstFoods.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
lstFoods.setLayoutManager(layoutManager);
loadOrderss();
}
private void loadOrderss() {
requests.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (postSnapshot.getValue() != null) {
// List ingredients = new ArrayList<>();
for (DataSnapshot ing : postSnapshot.child("foods").getChildren()) {
// String data = String.valueOf(postSnapshot.getValue(Order.class));
myfoods.add(ing.child("quantity").getValue(String.class));
myfoods.add(ing.child("productName").getValue(String.class));
myfoods.add(ing.child("productId").getValue(String.class));
// myfoods.add(String.valueOf(Order.class));
System.out.println("Gained data: " + ing.child("productName").getValue(String.class));
}
}
}
adapter = new AllOrdersAdapter((ArrayList<String>) myfoods);
lstFoods.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
推荐答案
代码似乎有几处错误.发布后,如果编译,我会感到惊讶.
There seems to be a couple things wrong with the code. As it is posted I would be surprised if it compiles.
在您的 Adapter
中,您拥有:
List<Order> myfoods;
和
public AllOrdersAdapter(List<Order> myfoods) {
this.myfoods = myfoods;
}
但是您在活动代码中通过了:
but in your activity code you pass:
adapter = new AllOrdersAdapter((ArrayList<String>) myfoods);
一个是 String
的 ArrayList
,另一个是 Order
的另一个!
one is a ArrayList
of String
the other of Order
!
您还需要将适配器类更改为:
You also need to change your adapter class to something like:
public class AllOrdersAdapter extends RecyclerView.Adapter<AllOrdersAdapter.AllOrdersViewHolder> {
private static final String TAG = AllOrdersAdapter.class.getSimpleName();
private ArrayList<Order> mData;
public class AllOrdersViewHolder extends RecyclerView.ViewHolder {
public TextView mTvFoodname;
public TextView mTvFoodQuantity;
public TextView mTvFoodId;
public AllOrdersViewHolder(View v){
super(v);
// TODO: You need to assign the appropriate View Id's instead of the placeholders ????
mTvFoodQuantity = v.findViewById(R.id.????);
mTvFoodname = v.findViewById(R.id.????);
mTvFoodId = v.findViewById(R.id.????);
}
}
public AllOrdersAdapter(ArrayList<Order> data){
this.mData = data;
}
@Override
public AllOrdersViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.business_list_card_view, parent, false);
return new AllOrdersViewHolder(itemView);
}
@Override
public void onBindViewHolder(final AllOrdersViewHolder holder, final int position){
//TODO: You need to decide whether you want to pass a string or order object
Order data = mData.get(position);
final String name = data.getProductName();
final String quantity = data.getQuantity();
final String id = data.getProductId();
holder.mTvFoodname.setText(name);
holder.mTvFoodQuantity.setText(quantity );
holder.mTvFoodId.setText(id)
}
@Override
public int getItemCount(){
return mData.size();
}
}
注意:由于我不知道,应该使用 String
的 ArrayList
还是 Order
的 ArrayList
code>活动或适配器
将需要更改.同样,在 onBindViewHolder
方法中,将数据分配给 RecyclerView
的方式也会受到影响.
Note: That since I can not know, whether an ArrayList
of String
or of Order
should be used the parameters in either the Activity
or Adapter
will need to be changed. Also how you assign the data to the RecyclerView
will be affected in the onBindViewHolder
method.
您还应该遵循弗兰克的建议.
You should also follow the advice given by Frank.
编辑
将您的 onDataChange()
方法更改为此:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
if (postSnapshot.getValue() != null) {
List ingredients = new ArrayList<>();
for (DataSnapshot ing : postSnapshot.child("foods").getChildren()) {
String name = ing.child("productName").getValue(String.class);
String quantity = ing.child("quantity").getValue(String.class);
String productId = ing.child("productId").getValue(String.class);
// Using your overloaded class constructor to populate the Order data
Order order = new Order(productId, name, quantity);
// here we are adding the order to the ArrayList
myfoods.add(order);
Log.e(TAG, "Gained data: " + name)
}
}
}
adapter.notifyDataSetChanged();
}
在您的 Activity
中,您需要将 ArrayList
类变量"myfoods"更改为此:
In your Activity
you will need to change the ArrayList
class variable "myfoods" to this:
ArrayList(Order)myfoods = new ArrayList<>();
,现在您可以在 onCreate()
方法中进行更改:
and in your onCreate()
method you can now change:
adapter = new AllOrdersAdapter((ArrayList<String>) myfoods);
简单地说:
adapter = new AllOrdersAdapter(myfoods);
还要注意,我在上面的原始代码中做了一些更改.
Also notice that I have made some changes in my original code above.
这篇关于在RecyclerView Firebase中强制转换arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!