No image showing on recyclerview with parse server

Firman Jamal Source

I'm very new to Android programming but with a little bit knowledge in java programming and I have an issue with connecting my Parse database to my RecyclerView. Nothing appears on the screen when the activity is launch. I have Log all the output query from Parse server and manage to get every rows of "image_url" but it seems to have problem when i try to output the image. I don't understand where the problem comes in and I really need explanation with this.

MainActivity

package sg.com.test.parseandroid;

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

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private GridLayoutManager gridLayoutManager;
    private static CustomAdapter adapter;
    private List<MyData> data_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        data_list = new ArrayList<>();
        gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);

        ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> postList, ParseException e) {
                if (e == null) {
                    for (ParseObject obj : postList) {
                        MyData data = new MyData(obj.getObjectId(), obj.getString("image_url"));
                        data_list.add(data);
                        Log.d("MAINACTIVITY", String.format("objectid- %s", data_list.get(postList.indexOf(obj)).getObjectId()));
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Something went wrong...", Toast.LENGTH_SHORT).show();
                }
            }

        });
        adapter = new CustomAdapter(this, data_list);
        recyclerView.setAdapter(adapter);
    }

}

CustomAdapter.java

package sg.com.test.parseandroid;

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 com.bumptech.glide.Glide;
import java.util.List;


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

    private Context context;
    private List<MyData> my_data;

    public CustomAdapter(Context context, List<MyData> my_data) {
        this.context = context;
        this.my_data = my_data;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Glide.with(context).load(my_data.get(position).getImage_url()).into(holder.imageView);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById((R.id.image));
        }

    }
}

MyData.java

package sg.com.test.parseandroid;

public class MyData {

    private String objectId, image_url;

    MyData(String objectId, String image_url) {
        this.objectId = objectId;
        this.image_url = image_url;
    }

    public String getObjectId() {
        return objectId;
    }

    public void setObjectId(String objectId) {
        this.objectId = objectId;
    }

    public String getImage_url() {
        return image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }
}

Logcat

02-14 19:22:29.157 1728-1728/? I/zygote: Not late-enabling -Xcheck:jni (already on)
02-14 19:22:29.208 1728-1728/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
02-14 19:22:29.523 1728-1728/sg.com.test.parseandroid I/InstantRun: starting instant run server: is main process
02-14 19:22:29.622 1728-1766/sg.com.test.parseandroid D/NetworkSecurityConfig: No Network Security Config specified, using platform default
02-14 19:22:29.933 1728-1773/sg.com.test.parseandroid D/OpenGLRenderer: HWUI GL Pipeline
02-14 19:22:29.984 1728-1773/sg.com.test.parseandroid I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
02-14 19:22:29.985 1728-1773/sg.com.test.parseandroid I/OpenGLRenderer: Initialized EGL, version 1.4
02-14 19:22:29.985 1728-1773/sg.com.test.parseandroid D/OpenGLRenderer: Swap behavior 1
02-14 19:22:29.985 1728-1773/sg.com.test.parseandroid W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
02-14 19:22:29.985 1728-1773/sg.com.test.parseandroid D/OpenGLRenderer: Swap behavior 0
02-14 19:22:30.001 1728-1773/sg.com.test.parseandroid D/EGL_emulation: eglCreateContext: 0x9ec85420: maj 2 min 0 rcv 2
02-14 19:22:30.021 1728-1773/sg.com.test.parseandroid D/EGL_emulation: eglMakeCurrent: 0x9ec85420: ver 2 0 (tinfo 0x9ec83310)
02-14 19:22:30.078 1728-1773/sg.com.test.parseandroid D/EGL_emulation: eglMakeCurrent: 0x9ec85420: ver 2 0 (tinfo 0x9ec83310)
02-14 19:22:31.231 1728-1728/sg.com.test.parseandroid D/MAINACTIVITY: objectId- caNtpHNOZB
02-14 19:22:31.231 1728-1728/sg.com.test.parseandroid D/MAINACTIVITY: objectId- G5eDwzYS8p
02-14 19:22:31.231 1728-1728/sg.com.test.parseandroid D/MAINACTIVITY: objectId- aap5bR7Xb5

Updates: Can't get any LOG in CustomAdapter.java

card_view.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"/>
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="sg.com.test.parseandroid.MainActivity"
    android:id="@+id/recycler_view"/>

I will get an error if i Log the data_list

ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
        query.findInBackground(new FindCallback<ParseObject>() {
            public void done(List<ParseObject> postList, ParseException e) {
                if (e == null) {
                    for (ParseObject obj : postList) {
                        MyData data = new MyData(obj.getObjectId(), obj.getString("image_url"));
                        data_list.add(data);
                        Log.d("MAINACTIVITY", String.format("objectid- %s", data_list.get(postList.indexOf(obj)).getImage_url()));
                    }
                } else {
                    Toast.makeText(MainActivity.this, "Something went wrong...", Toast.LENGTH_SHORT).show();
                }
            }
        });

        MyData data = data_list.get(0);
        Log.d("MainActivity.onCreate", data.getObjectId());
        adapter = new CustomAdapter(this, data_list);
        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);

Logcat

02-15 05:26:43.820 16722-16722/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: sg.com.creertech.parseandroid, PID: 16722
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{sg.com.creertech.parseandroid/sg.com.creertech.parseandroid.MainActivity}: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                       at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                       at android.os.Handler.dispatchMessage(Handler.java:106)
                                                       at android.os.Looper.loop(Looper.java:164)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                    Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
                                                       at java.util.ArrayList.get(ArrayList.java:437)
                                                       at sg.com.creertech.parseandroid.MainActivity.onCreate(MainActivity.java:52)
                                                       at android.app.Activity.performCreate(Activity.java:6999)
                                                       at android.app.Activity.performCreate(Activity.java:6990)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
javaandroidandroid-layoutparsingandroid-recyclerview

Answers

answered 6 months ago dazed'n'confused #1

At first, insert log inside every fuctions in CustomAdapter and see if the data is reaching here or not. eg.

Log.d("CustomAdapter", "size of data==> "+my_data.size());

If all good then try this code inside onCreateViewHolder(). Instead of parent, false try null. This may help

View view = LayoutInflater.from(context.inflate(R.layout.card, null); 

Also can you show the card.xml layout??

comments powered by Disqus