Marcy HS
Marcy HS

Reputation: 43

Firestore collection to custom ListView

Basically I've been trying to populate a custom ListView from a firestore collection, but after a few hours I couldn't so I searched in Stack Overflow and found this question:

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

I compared my adapter and my model with the question but none of them seemed to be wrong so the only option remaining was the main file, I used the same code shown in the answer but an error occurs in this line:

ProdAdapter mProductAdapter = new ProdAdapter(this, mProductsList);

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

In the answer the adapter is created inside the get method, so I tried to create It after that but the error still remains. I don't really know much about Java and Firebase but I would like to know how to get the context or if I should use another method besides 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);
                    }
                }
            });


        }

The adapter:

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;
    }

}

And item class:

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;
    }
}

Upvotes: 4

Views: 1719

Answers (1)

Levi Moreira
Levi Moreira

Reputation: 12005

In this case:

ProdAdapter mProductAdapter = new ProdAdapter(this, mProductsList);

The keyword "this" refers to the anonymous class OnCompleteListener where this code is located. You'll want to change it to this:

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

Which will pass in the context of the main Activity, which is what your adapter is actually expecting as a param:

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

Upvotes: 2

Related Questions