No Data In Recycler View

95 Views Asked by At

I am completely beginner to android so there might be some studpidness in my case.

I am trying to make an Instagram clone app. But I am stuck in displaying users feed. I am fetching my data from Parse Server Then its stored in Two ArrayList 1 stores all Bitmaps and other all strings. Through my log i can see that my data is fetching properly but it is not displayed any where on the activity.

I am providing the activity code and all the necessary information any help would be greatly appreciated.

userTimeLine.class :

package com.example.avail.instagramclone;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

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

public class userTimeLine extends AppCompatActivity {
    TextView userName;
    String currLoggedInUser;
    ArrayList<String> postsByUser;
    ArrayList<Bitmap> imagesByUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_time_line);

        postsByUser = new ArrayList<String>();
        imagesByUser = new ArrayList<Bitmap>();
        String selectedUser = getIntent().getStringExtra("SelectedUser");
        Log.i("Selected User Is " ,selectedUser);
        userName = findViewById(R.id.username);
        userName.setText(selectedUser.toUpperCase());
        final RecyclerView recyclerView = findViewById(R.id.recView);
        recyclerView.setHasFixedSize(true);


        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        final RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(postsByUser,imagesByUser);
        recyclerView.setAdapter(recyclerViewAdapter);



        ParseQuery<ParseObject> query = ParseQuery.getQuery("Image");
        query.whereEqualTo("username",selectedUser);
        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e==null){
                    Log.i("Loading Posts","True");
                    Log.i("No Of Posts", String.valueOf(objects.size()));
                    if (objects.size()>0){


                        for(ParseObject object : objects){
                            ParseFile file = (ParseFile) object.get("image");
                            file.getDataInBackground(new GetDataCallback() {
                                @Override
                                public void done(byte[] data, ParseException e) {
                                    if(e==null){
                                        Bitmap bitmapImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                                        imagesByUser.add(bitmapImage);
                                        Log.i("Status","Bitmap Fetched!");
                                        Log.i("Content In Images", String.valueOf(imagesByUser.size()));
                                        recyclerViewAdapter.notifyDataSetChanged();

                                    }
                                    else{
                                        Log.i("info", e.getMessage());
                                    }

                                }
                            });

                            String status = (String) object.get("poststatus");


                            postsByUser.add(status);


                        }
                        Log.i("Content In Status", String.valueOf(postsByUser.size()));
                        Log.i("Content In Images", String.valueOf(imagesByUser.size()));












                    }else {
                        Toast.makeText(getApplicationContext(),"NO POSTS BY USER",Toast.LENGTH_SHORT).show();


                    }
                }else{
                    Toast.makeText(getApplicationContext(),"NETWORK ERROR!",Toast.LENGTH_SHORT).show();
                    Log.i("Network Error ",e.getMessage());


                }
            }
        });







    }
}

Adapter Class for the recycler view named as: RecyclerViewAdapter :

**

package com.example.avail.instagramclone;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
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.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private ArrayList<String> postStatuses ;
    private ArrayList<Bitmap> postImages;
    ImageView postImage;
    TextView postStatus;
    public RecyclerViewAdapter(ArrayList<String> postStatuses, ArrayList<Bitmap> postImages){
        this.postStatuses = postStatuses;
        this.postImages = postImages;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View customViewForDisplayingFeed = inflater.inflate(R.layout.recycler_list_users_feed,parent,false);
        return new ViewHolder(customViewForDisplayingFeed);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String status = postStatuses.get(position);
        Bitmap image = postImages.get(position);
        postImage.setImageBitmap(image);
        postStatus.setText(status);
    }
    @Override
    public int getItemCount() {
        return postStatuses.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        public ViewHolder(View itemView) {
            super(itemView);
            postImage = itemView.findViewById(R.id.userFeedImageListItem);
            postStatus = itemView.findViewById(R.id.userFeedPostListItem);
        }
    }
}

**

Layout file for RecyclerView item named as recycler_list_users_feed.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/userFeedImageListItem"
        android:layout_width="match_parent"
        android:layout_height="261dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <TextView
        android:id="@+id/userFeedPostListItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Here comes the post status"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/userFeedImageListItem" />
</LinearLayout>

Also, my activity file named as activity_user_time_line.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".userTimeLine">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recView"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="15dp"
        android:layout_marginStart="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toBottomOf="@+id/username" />

    <View
        android:id="@+id/view4"
        style="@style/Divider"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toTopOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/usersList" />

    <View
        android:id="@+id/view3"
        style="@style/Divider"
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:background="#f0f1f1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view3"
        app:srcCompat="@drawable/usericon" />

    <TextView
        android:id="@+id/TIMELINE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="TIMELINE"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="@+id/view3"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view3"
        app:layout_constraintTop_toTopOf="@+id/view3" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="USERNAME"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/view4"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

</android.support.constraint.ConstraintLayout>

Last but not the least my log cat :). NOTE : MY APP ISN'T CRASHING BTW.

09-15 03:35:31.559 21397-21397/com.example.avail.instagramclone I/Selected User Is: abuzar
09-15 03:35:31.583 21397-21726/com.example.avail.instagramclone I/QCNEA   : |NIMS|: getaddrinfo: hostname 18.222.87.194 servname NULL numeric 4 appname /system/bin/app_process
09-15 03:35:31.586 21397-21726/com.example.avail.instagramclone I/QCNEA   : |NIMS|: getaddrinfo: hostname 18.222.87.194 servname NULL numeric 4 appname /system/bin/app_process
09-15 03:35:32.065 21397-21397/com.example.avail.instagramclone I/Loading Posts: True
09-15 03:35:32.065 21397-21397/com.example.avail.instagramclone I/No Of Posts: 1
09-15 03:35:32.070 21397-21397/com.example.avail.instagramclone I/Content In Status: 1
09-15 03:35:32.070 21397-21397/com.example.avail.instagramclone I/Content In Images: 0
09-15 03:35:32.110 21397-21731/com.example.avail.instagramclone D/dalvikvm: GC_FOR_ALLOC freed 568K, 13% free 26178K/30084K, paused 14ms, total 14ms
09-15 03:35:32.130 21397-21397/com.example.avail.instagramclone D/dalvikvm: GC_FOR_ALLOC freed 1531K, 13% free 26212K/30084K, paused 14ms, total 14ms
09-15 03:35:32.177 21397-21397/com.example.avail.instagramclone I/dalvikvm-heap: Grow heap (frag case) to 43.627MB for 16777232-byte allocation
09-15 03:35:32.298 21397-21397/com.example.avail.instagramclone I/Status: Bitmap Fetched!
09-15 03:35:32.298 21397-21397/com.example.avail.instagramclone I/Content In Images: 1
4

There are 4 best solutions below

0
On BEST ANSWER

I was able to solve my problem via making changes in my main activity's home page

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".userTimeLine">

    <View
        android:id="@+id/view3"
        style="@style/Divider"
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:background="#f0f1f1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view3"
        app:srcCompat="@drawable/usericon" />

    <TextView
        android:id="@+id/TIMELINE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="TIMELINE"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="@+id/view3"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view3"
        app:layout_constraintTop_toTopOf="@+id/view3" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="USERNAME"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="170dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/username" />

</android.support.constraint.ConstraintLayout>
1
On

maybe your problem happened in your Adapter's ViewHolder. you can exchange your Adapter code like this and then try to compile your app again.

package com.example.avail.instagramclone;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
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.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<String> postStatuses ;
private ArrayList<Bitmap> postImages;

public RecyclerViewAdapter(ArrayList<String> postStatuses, ArrayList<Bitmap> postImages){
    this.postStatuses = postStatuses;
    this.postImages = postImages;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View customViewForDisplayingFeed = inflater.inflate(R.layout.recycler_list_users_feed,parent,false);
    return new ViewHolder(customViewForDisplayingFeed);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String status = postStatuses.get(position);
    Bitmap image = postImages.get(position);
    viewHolder.postImage.setImageBitmap(image);
    viewHolder.postStatus.setText(status);
}
@Override
public int getItemCount() {
    return postStatuses.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
    ImageView postImage;
    TextView postStatus;
    public ViewHolder(View itemView) {
        super(itemView);
        postImage = itemView.findViewById(R.id.userFeedImageListItem);
        postStatus = itemView.findViewById(R.id.userFeedPostListItem);
    }
}
}
1
On

I think what you have wrong is postImage and postStatus in the Adapter.

postImage and postStatus should be attributes of the ViewHolder class.

You would initialize them after inflating the ViewHolder's layout. And give them the value to display in onBindViewholder().

A side note: Keeping an ArrayList of Bitmap is perhaps not a good idea because they can fill up your available memory.

Perhaps it is better to cache the Bitmas in some folder and keep an ArrayList of the paths.
And then read them from that folder when you need to display them.

2
On

Let's try another thing:

We change your RecyclerViewAdapter with this

import java.util.ArrayList;
public class RecyclerViewAdapter extends 
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<String> postStatuses ;
private ArrayList<Bitmap> postImages;

public RecyclerViewAdapter(ArrayList<String> postStatuses, ArrayList<Bitmap> postImages){
    this.postStatuses = postStatuses;
    this.postImages = postImages;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View customViewForDisplayingFeed = inflater.inflate(R.layout.recycler_list_users_feed,parent,false);
    return new ViewHolder(customViewForDisplayingFeed);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    String status = postStatuses.get(position);
    Bitmap image = postImages.get(position);
    holder.postImage.setImageBitmap(image);
    holder.postStatus.setText(status);
}
@Override
public int getItemCount() {
    return postStatuses.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder{

ImageView postImage; TextView postStatus public ViewHolder(View itemView) { super(itemView); postImage = itemView.findViewById(R.id.userFeedImageListItem); postStatus = itemView.findViewById(R.id.userFeedPostListItem); } } }

Sorry for the ident, i edit this in a phone