基本上,我一直在尝试从Firestore集合中填充自定义ListView,但是几个小时后却无法这样做,因此我在Stack Overflow中进行了搜索,发现了以下问题:

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

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

ProdAdapter mProductAdapter = new ProdAdapter(this, mProductsList);

错误:不兼容的类型:匿名
OnCompleteListener(QuerySnapshot)无法转换为上下文

在答案中,适配器是在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);
    }

07-24 21:11