简体   繁体   中英

How do I download various types of files(images, videos , texts) from Firebase and display them

I am trying to make an app where I need to retrieve data from firebase which can be anything(images, videos , texts) and display them. After searching and through guidance I found this link, but it doesnt show me how do I download all the files and even how do I get the getItemViewType() to place it in a recycler view.

This is the adapter

public class MultiViewTypeAdapter extends RecyclerView.Adapter {

private ArrayList<Model>dataSet;
Context mContext;
int total_types;
MediaPlayer mPlayer;
private boolean fabStateVolume = false;

public static class TextTypeViewHolder extends RecyclerView.ViewHolder {

    TextView txtType;
    CardView cardView;

    public TextTypeViewHolder(View itemView) {
        super(itemView);

        this.txtType = (TextView) itemView.findViewById(R.id.type);
        this.cardView = (CardView) itemView.findViewById(R.id.card_view);
    }
}

public static class ImageTypeViewHolder extends RecyclerView.ViewHolder {

    TextView txtType;
    ImageView image;

    public ImageTypeViewHolder(View itemView) {
        super(itemView);

        this.txtType = (TextView) itemView.findViewById(R.id.type);
        this.image = (ImageView) itemView.findViewById(R.id.background);
    }
}

public static class VideoTypeViewHolder extends RecyclerView.ViewHolder {

    TextView txtType;
    VideoView video;

    public VideoTypeViewHolder(View itemView) {
        super(itemView);

        this.txtType = (TextView) itemView.findViewById(R.id.type);
        this.video = (VideoView) itemView.findViewById(R.id.videoView2);
    }
}

public MultiViewTypeAdapter(ArrayList<Model>data, Context context) {
    this.dataSet = data;
    this.mContext = context;
    total_types = dataSet.size();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view;
    switch (viewType) {
        case Model.TEXT_TYPE:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_type, parent, false);
            return new TextTypeViewHolder(view);
        case Model.IMAGE_TYPE:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_type, parent, false);
            return new ImageTypeViewHolder(view);
        case Model.VIDEO_TYPE:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_type, parent, false);
            return new VideoTypeViewHolder(view);
    }
    return null;
}

@Override
public int getItemViewType(int position) {

    switch (dataSet.get(position).type) {
        case 0:
            return Model.TEXT_TYPE;
        case 1:
            return Model.IMAGE_TYPE;
        case 2:
            return Model.VIDEO_TYPE;
        default:
            return -1;
    }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

    Model object = dataSet.get(listPosition);
    if (object != null) {
        switch (object.type) {
            case Model.TEXT_TYPE:
                ((TextTypeViewHolder) holder).txtType.setText(object.text);

                break;
            case Model.IMAGE_TYPE:
                ((ImageTypeViewHolder) holder).txtType.setText(object.text);
                ((ImageTypeViewHolder) holder).image.setImageResource(object.data);
                break;
            case Model.VIDEO_TYPE:


                break;
        }
    }
}

@Override
public int getItemCount() {
    return dataSet.size();
}
}

This is the model class

public class Model {

public static final int TEXT_TYPE=0;
public static final int IMAGE_TYPE=1;
public static final int VIDEO_TYPE=2;

public int type;
public int data;
public String text;

public Model(int type, String text, int data)
{
    this.type=type;
    this.data=data;
    this.text=text;
}
}

They are missing importants information.

Do you use firestore or database real-time ?

If you use firestore and need to get data from document use this code :

DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

I you need to get all the documents from a collection in firestore use this :

db.collection("cities")
        .get()
        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d(TAG, document.getId() + " => " + document.getData());
                    }
                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

if your use realtime your need this :

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // Get Post object and use the values to update the UI
        Post post = dataSnapshot.getValue(Post.class);
        // ...
    }

For the getItemViewType() in your link this a method create for the sample. He define a view holder for odd and even item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM