如何使用RecyclerView适配器显示Firebase存储中的图片?

时间:2019-02-07 15:58:45

标签: android firebase-storage android-adapter android-glide

我想使用Glide(或毕加索)在RecyclerView中显示Firebase存储中的图片。我的问题是:当我尝试使用当前上下文填充with参数时,出现以下错误:

Cannot resolve method with "this" 

这是我的适配器的代码。

public class MedAdapterMatin extends RecyclerView.Adapter<MedAdapterMatin.MyViewHolder> {

ArrayList<Medicament> malist;

public MedAdapterMatin(ArrayList<Medicament> malist) {
    this.malist = malist;

}

public static class MyViewHolder extends RecyclerView.ViewHolder {

    ImageView boiteView, pilsView;
    TextView momentPrise, nomMed, nbPrise;
    Context context;
    //Firebase Database
    FirebaseDatabase database;
    DatabaseReference myRef;


    public MyViewHolder(View medView) {
        super(medView);

        boiteView = medView.findViewById(R.id.boite);
        pilsView = medView.findViewById(R.id.cachet);
        momentPrise = medView.findViewById(R.id.moment);
        nomMed = medView.findViewById(R.id.medi);
        nbPrise = medView.findViewById(R.id.nbPrise);
    }

    public void display(Medicament m) {

        //Firebase database
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference().child("PIC_Firebase");
        momentPrise.setText(m.getMomentPrise());
        nomMed.setText(m.getNom());
        //boiteView.setImageURI(Uri.parse(m.getImageAdress()));
      StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("Medicament/" + m.getNom() + ".jpg");
        profileImageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
               // Picasso.get().load(uri).into(boiteView);
                Glide.with(MedAdapterMatin.getContext()).load(uri).into(boiteView);
            }

        });
        String form = m.getFormePharma();
        /*int resID = context.getResources().getIdentifier(form, "drawable", context.getPackageName());*/
        pilsView.setImageResource(R.drawable.goutte);
        nbPrise.setText("X" + m.getCompriméMatin());


    }

}


@NonNull
@Override
public MedAdapterMatin.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext());
    View view = layoutInflater.inflate(R.layout.affichage_medicament, viewGroup, false);
    return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    holder.display(malist.get(position));

}


public int getItemCount() {
    return malist.size();
}

}

这是我的MainActivity:

//RecyclerView Matin
private RecyclerView rvMatin;
private MedAdapterMatin medAdapterMatin;
private ArrayList<Medicament> medListMatin;
//RecyclerView Midi
private RecyclerView rvMidi;
private MedAdapterMidi medAdapterMidi;
private ArrayList<Medicament> medListMidi;
//RecyclerView Soir
private RecyclerView rvSoir;
private MedAdapterSoir medAdapterSoir;
private ArrayList<Medicament> medListSoir;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_accueil_p);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //Firebase database
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("PIC_Firebase");

    Globals g = (Globals) getApplicationContext();

    //Call ordonnance bdd
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Ordonnance ord = new Ordonnance();
            Globals g = (Globals) getApplicationContext();

            String ordo = g.getRef_database() + "Ordonnance";
            DataSnapshot dt_ord = (DataSnapshot) dataSnapshot.child(ordo);
            ord = dt_ord.getValue(Ordonnance.class);
            //String date_ord = ord.getDateOrdonnace();
            //ArrayList<Medicament> listmed = ord.getListMed();
            medList = new ArrayList<>();
            medListMatin = new ArrayList<>();
            medListMidi = new ArrayList<>();
            medListSoir = new ArrayList<>();

            medList = ord.getListMed();

            if (medList != null) {
                //Affectation dans les listes de medicaments Matin/Midi/Soir
                for (Medicament ls : medList) {
                    if (!ls.getCompriméMatin().equals(""))
                        medListMatin.add(ls);
                    if (!ls.getCompriméMidi().equals(""))
                        medListMidi.add(ls);
                    if (!ls.getCompriméSoir().equals(""))
                        medListSoir.add(ls);
                }
            }
            //RECYCLER VIEW MATIN
            rvMatin = findViewById(R.id.list_matin);
            medAdapterMatin = new MedAdapterMatin(medListMatin);
            rvMatin.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

            //RECYCLER VIEW Midi
            rvMidi = findViewById(R.id.list_midi);
            medAdapterMidi = new MedAdapterMidi(medListMidi);
            rvMidi.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));

            //RECYCLER VIEW Soir
            rvSoir = findViewById(R.id.list_soir);
            medAdapterSoir = new MedAdapterSoir(medListSoir);
            rvSoir.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
            //Set adapters
            rvMatin.setAdapter(medAdapterMatin);
            rvMidi.setAdapter(medAdapterMidi);
            rvSoir.setAdapter(medAdapterSoir);
        }

1 个答案:

答案 0 :(得分:0)

因为“ this”不在此处提供上下文。如果要在Activity类中尝试此代码,则传递“ ActivityName.this”,或者如果使用片段,则编写“ getContext”而不是“ this”。

Glide.with(ActivityName.this).load(url)."tinto(boiteView); //if you're trying code in Activity class

Glide.with(getContext ()).load(url)."tinto(boiteView); //if you're trying code in Fragment class