gpt4 book ai didi

java - Firestore 集合到自定义 ListView

转载 作者:太空狗 更新时间:2023-10-29 13:03:59 24 4
gpt4 key购买 nike

基本上我一直在尝试从 firestore 集合中填充自定义 ListView,但几个小时后我做不到,所以我在 Stack Overflow 中搜索并发现了这个问题:

How to show a firestore Collection in an Android ListView using an Adapter

我将我的适配器和我的模型与问题进行了比较,但它们似乎都没有错,所以剩下的唯一选项是主文件,我使用了答案中显示的相同代码,但此行出现错误:

ProdAdapter mProductAdapter = new ProdAdapter(this, mProductsList);

error: incompatible types: anonymous OnCompleteListener(QuerySnapshot) cannot be converted to Context

在答案中,适配器是在 get 方法中创建的,所以我尝试在那之后创建它,但错误仍然存​​在。我不太了解 Java 和 Firebase,但我想知道如何获取上下文,或者我是否应该使用除 addOnCompleteListener 之外的其他方法。

MainActivity.java:

 public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView scView;
private ListView lista;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final FirebaseFirestore db = FirebaseFirestore.getInstance();
lista = findViewById(R.id.mylistView);

db.collection("tasks").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
List<Product> mProductsList = new ArrayList<>();
if(task.isSuccessful()){
for(QueryDocumentSnapshot document : task.getResult()) {
Product miss = document.toObject(Product.class);
mProductsList.add(miss);
}

ProdAdapter mProductAdapter = new ProdAdapter(this, mProductsList);

mProductAdapter.notifyDataSetChanged();
lista.setAdapter(mProductAdapter);
}
}
});


}

适配器:

public class ProdAdapter extends ArrayAdapter<Product>{

Context context;
ArrayList<Product> object;

public ProdAdapter(Context context, ArrayList<Product> object){
super(context,0, object);
}


@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.product_view_model,parent,false);
}

Product dir = getItem(position);

TextView prod = (TextView) convertView.findViewById(R.id.lblName);
TextView description = (TextView) convertView.findViewById(R.id.lblDesc);
TextView code = (TextView) convertView.findViewById(R.id.lblCode);
TextView qty = (TextView) convertView.findViewById(R.id.lblQty);
TextView price = (TextView) convertView.findViewById(R.id.lblPrice);

prod.setText(dir.getProduct());
description.setText(dir.getDescription());
code.setText(dir.getId());
qty.setText(dir.getQty());
price.setText(dir.getPrice());

return convertView;
}

}

和项目类:

package com.shop.shai.management_test.model;

public class Product {

public String id;
public String product;
public String price;
public String qty;
public String description;

public Product() {}

public Product(String id, String product, String price, String qty, String description) {
this.id = id;
this.product = product;
this.price = price;
this.qty = qty;
this.description = description;
}

public String getId(){
return id;
}
public String getProduct(){
return product;
}
public String getPrice(){
return price;
}
public String getDescription(){
return description;
}
public String getQty(){
return qty;
}

public void setId(String id) {
this.id = id;
}
public void setProduct(String product) {
this.product = product;
}
public void setPrice(String price) {
this.price = price;
}
public void setQty(String qty) {
this.qty = qty;
}
public void setDescription(String description) {
this.description = description;
}
}

最佳答案

在这种情况下:

ProdAdapter mProductAdapter = new ProdAdapter(this, mProductsList);

关键字“this”指的是这段代码所在的匿名类OnCompleteListener。您需要将其更改为:

ProdAdapter mProductAdapter = new ProdAdapter(MainActivity.this, mProductsList);

它将在主 Activity 的上下文中传递,这是您的适配器实际期望的参数:

public ProdAdapter(Context context, ArrayList<Product> object){
super(context,0, object);
}

关于java - Firestore 集合到自定义 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51082933/

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