-1

I am new to android development. So sorry if this is useless question. I have RecyclerView like I want to display image and video from Firebase in RecyclerView.

I have two ViewHolder. What I want is, adapter to check the post "type". If the
1)"type"=photo. I want to inflate PhotoViewHolder.
2) If "type"= video I want to inflate VideoHolder.

How can I do this?. Please help me!.

take a look at this pic

I have googled for hours and I got this code it only inflate VideoHolder

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //findViewById...
    mContext = parent.getContext();
    switch (viewType) {
        case VIDEO_TYPE:
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_type, parent, false);
            return new VideoHolder(v);
        case IMAGE_TYPE:
            View i = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_list, parent, false);
            return new PhotoHolder(i);

    }
    return null;
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
    mHolder = holder;
    photo = moviesList.get(position);
    //  final VideoHolder viewHolder2 = (VideoHolder)holder;
    int viewType = getItemViewType(position);
    switch ( viewType ) {
        case IMAGE_TYPE:
            PhotoHolder photoview = (PhotoHolder) holder;
            final ImageLoader imageLoader =  ImageLoader.getInstance();
            imageLoader.displayImage(getItem(position).getImage_path(),photoview.image);
            break;
        case VIDEO_TYPE:
            final VideoHolder viewHolder2 = (VideoHolder)holder;
            //viewHolder2.users = new StringBuilder();
            viewHolder2.bind(Uri.parse(photo.getImage_path()),moviesList);
            break;
    }


}

@Override
public int getItemCount() {
    return moviesList.size();
}
public Photo getItem(int position) {
    return moviesList.get(position);
}
@Override
public int getItemViewType ( int position ) {
    int viewType;
    if if (moviesList.get(position).getType().equals("Photo")) {
        viewType = IMAGE_TYPE;
    } else{
        viewType = VIDEO_TYPE;
    }
    return viewType;
}
  • so what's the problem? – denvercoder9 Jun 03 '18 at 06:39
  • I want check the "type " the firebase and inflate ViewHolder according to it –  Jun 03 '18 at 06:41
  • I tried if else commend it does not work it only inflate VideoHolder..please help me bro –  Jun 03 '18 at 06:41
  • Use the `equals()` method, not `==`. That is, `if (moviesList.get(position).getType().equals("Video"))`. – Mike M. Jun 03 '18 at 06:43
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – denvercoder9 Jun 03 '18 at 06:43
  • it shows it is null .. –  Jun 03 '18 at 06:45
  • Then you need to provide more information than just those `Adapter` methods. We don't know how you're constructing the `moviesList`. – Mike M. Jun 03 '18 at 06:46
  • ok ill edit this..please wait –  Jun 03 '18 at 06:47
  • 1
    This seems less like a recyclerview problem and more like a problem with comparison. I'd suggest you pause on the recyclerview for now and just loop through your dataset and figure out how to differentiate between 2 item, once you have that displaying should be trivial as the recyclerview code you provided above seems correct. – Jude Fernandes Jun 03 '18 at 07:08
  • so what should I do now –  Jun 03 '18 at 07:11
  • am in comparing in wrong way –  Jun 03 '18 at 07:12

1 Answers1

0

change to .equals() to .contains()

 @Override
public int getItemViewType ( int position ) {
    int viewType;
    if (moviesList.get(position).getType_post().contains("Photo")) {
        viewType = IMAGE_TYPE;
    } else{
        viewType = VIDEO_TYPE;
    }
    return viewType;
}