使用picasso,图像不会从firebase数据库加载到recyclerview上

时间:2017-12-12 06:48:47

标签: android https firebase-realtime-database picasso

我一直在尝试使用回收站视图创建一个壁纸应用程序。我想从我的firebase firestore数据库加载图像。我设法做到了。当我使用LOG检查它时,我得到了正确的图像网址。我试图使用PICASSO将图像链接转换为图像。但不幸的是,图像不会加载到recyclerview上。请帮忙 这是我的适配器和持有人类

package com.example.rimapps.charpapers;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Protocol;
import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Rimshad on 28/11/2017.
 */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder>{

    List<String> Mylist2= new ArrayList<>();
    Context context;

    public ImageAdapter(List<String>Mylist2,Context context) {
        this.Mylist2 = Mylist2;
        this.context = context;
    }

    @Override
    public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.items2, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ImageAdapter.ViewHolder holder, final int position) {
        OkHttpClient client = new OkHttpClient();
        client.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
        Picasso picasso = new Picasso.Builder(context)
                .downloader(new OkHttpDownloader(client))
                .build();

       picasso.with(context)
                .load(Mylist2.get(position))
                .placeholder(R.drawable.superman)
                .error(R.drawable.error)
                .into(holder.image2);

        holder.image2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,Mylist2.get(position),Toast.LENGTH_SHORT).show();
            }
        });
    }
    @Override
    public int getItemCount() {
        return Mylist2.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {


                public ImageView image2;

                ViewHolder(View view) {

                    super(view);

                    image2 = (ImageView) view.findViewById(R.id.wallid);
                }
            }
        }

这是我的MainActivity类

package com.example.rimapps.charpapers;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity {

    private static final String TAG ="tAAAAAAAAAAg" ;
    private ArrayList<String> imagelist;
    RecyclerView recyclerView2;
    FirebaseFirestore db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        db = FirebaseFirestore.getInstance();

        recyclerView2 = (RecyclerView) findViewById(R.id.recview2);
        recyclerView2.setLayoutManager(new GridLayoutManager(Main2Activity.this,2));
        imagelist= new ArrayList<>();
        DocumentReference docRef = db.collection("Wallpapers").document("Aquaman");
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document != null) {
                        Log.d(TAG, "DocumentSnapshot data: " + task.getResult().getData());
                         for(int i=1;i<document.getData().size();i++){
                            document.get(i+"");
                             imagelist.add(document.get(i+"").toString());
                             Log.e(TAG,document.get(i+"").toString());
                        }
                            ImageAdapter imageAdapter = new ImageAdapter(imagelist,Main2Activity.this);
                            recyclerView2.setAdapter(imageAdapter);
                    } else {
                        Log.d(TAG, "No such document");
                    }
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    }
            }

我想将数据从db加载到我的回收站视图

我得到的当前结果是来自.error(R.drawable.error)的{​​{1}}。请帮忙。这是https:

的问题

1 个答案:

答案 0 :(得分:0)

这个答案是因为,我无法在评论中明确指出:

无需编写此语句代码:

Picasso.with(context)
        .load(Mylist2.get(position))
        .placeholder(R.drawable.superman)
        .error(R.drawable.error)
        .into(holder.image2);

您可以通过以下方式直接调用:

import pandas as pd
from datetime import timedelta, date
import os

def daterange(start_date, end_date):
    for n in range(int ((end_date - start_date).days)):
        yield start_date + timedelta(n)

#Define what date range to consolidate
start_date = date(2017, 8, 1)
end_date = date.today()

#Create a starting dataframe with the first csv file and change the "hash_id" to string to prepare dataframe for insertions using this as the unique identifier for each listing

master_df = pd.read_csv("real_estate(" + start_date.strftime("%d-%m-%Y") + ").csv")
master_df = master_df.assign(last_seen=master_df['date'])
master_df['hash_id'] = master_df['hash_id'].astype(str)

#Loop through all CSV files for the time period listed, if listing is already in dataframe then update the "last seen" with the newer date, otherwise add the listing as a new row to the master dataframe

for single_date in daterange(start_date, end_date):
    file_path = "real_estate(" + single_date.strftime("%d-%m-%Y") + ").csv"
    print(single_date.strftime("%d-%m-%Y"))
    if os.path.isfile(file_path) is True and os.path.getsize(file_path) > 1:
        temp_df = pd.read_csv(file_path)
        temp_df = temp_df.assign(last_seen=temp_df['date'])
        for index, row in temp_df.iterrows():
            hash_id = str(row['hash_id'])
            if master_df['hash_id'].str.contains(hash_id).any():
                master_df.loc[master_df['hash_id'] == hash_id, 'last_seen'] = row['date']
            else:
                master_df.loc[len(master_df)] = row

#write the consolidated dataframe to CSV
master_df.to_csv("master_file.csv")