javaandroidandroid-recyclerviewandroid-parserparse-android-sdk

Implementing RecyclerView with parseObject but getting a blank screen


I am trying to implement RecyclerView using ParseObject but on my screen nothing is showing up!

Following is the code for the activity where recycler view is to implemented.

AdminShowStudentId.java

     package com.example.Aphexams;

     import android.content.Intent;
     import android.os.Bundle;
     import android.app.Activity;
     import android.support.v7.widget.GridLayoutManager;
     import android.support.v7.widget.RecyclerView;
     import android.util.Log;
     import android.view.View;
     import android.widget.Adapter;
     import android.widget.Spinner;
     import android.widget.SpinnerAdapter;
     import android.widget.Toast;

     import com.parse.FindCallback;
     import com.parse.GetCallback;
     import com.parse.ParseException;
     import com.parse.ParseObject;
     import com.parse.ParseQuery;
     import com.parse.ParseQueryAdapter;

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

     //import app.android.project.com.olexam.R;



     public class AdminShowStudentId extends Activity {

    private RecyclerView recycleerviewDetails;
    private RecyclerView.LayoutManager layoutManager;
    private StudentIdAdapter studentidAdapter;
    private ArrayList<StudentId> studentidarraylist;
    private boolean notComplete = true;
    private String studId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_show_student_id);
      final StudentId studentId=new StudentId();
        studentidarraylist=new ArrayList<StudentId>();

            if(notComplete) {


                ParseQuery<ParseObject> query = ParseQuery.getQuery("studAuth");

                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                    public void done(List<ParseObject> objects, ParseException e) {
                        if (e == null) {
                            for (ParseObject object : objects) {
                                String studentid = (String) object.get("StudUserName");
                                studentId.setStudentId(studentid);
                                studentidarraylist.add(studentId);


                            }
                            //is it fetching or not

                        } else {
                            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
                notComplete=false;
            }


        recycleerviewDetails=(RecyclerView)findViewById(R.id.studentidrecyclerview);
        if (recycleerviewDetails != null)
        {
            recycleerviewDetails.setHasFixedSize(true);
        }

        /*fetching data from the database*/
        studentidAdapter=new StudentIdAdapter(studentidarraylist);
        recycleerviewDetails.setAdapter(studentidAdapter);
        studentidAdapter.notifyDataSetChanged();
        layoutManager=new GridLayoutManager(this,1);
        recycleerviewDetails.setLayoutManager(layoutManager);


        recycleerviewDetails.addOnItemTouchListener(new RecyclerViewListener(AdminShowStudentId.this,new RecyclerViewListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                studId=studentidarraylist.get(position).getStudentId();
                Intent i=new Intent(AdminShowStudentId.this,ViewStudent.class);
                i.putExtra("studId",studId);
            }
        }));








       }
  }

following is the code for StudentIdAdapter.java

    package com.example.Aphexams;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

/**
 * Created by HP on 24-05-2018.
 */

public class StudentIdAdapter extends RecyclerView.Adapter<StudentIdAdapter.StudentIdViewHolder>
{
    private List<StudentId> studentIdList;

    public StudentIdAdapter(ArrayList<StudentId> studentidarraylist) {
        this.studentIdList=studentidarraylist;
    }


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

    @Override
    public void onBindViewHolder(StudentIdViewHolder holder, int position) {

        StudentId studentid= studentIdList.get(position);
        holder.tvStudentId.setText(studentid.getStudentId());
      //  holder.tvanswerdate.setText(answer.getAnswerdate());

    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class StudentIdViewHolder extends RecyclerView.ViewHolder
    {
        private TextView tvStudentId;

        public StudentIdViewHolder(View itemView)
        {
            super(itemView);
            tvStudentId=(TextView)itemView.findViewById(R.id.studentidtv);

        }
    }
}

following is the code for RecyclerViewListener.java

    package com.example.Aphexams;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;

/**
 * Created by HP on 24-05-2018.
 */

public class RecyclerViewListener implements RecyclerView.OnItemTouchListener {
    private OnItemClickListener mListener;
    GestureDetector mGestureDetector;
    public interface OnItemClickListener
    {
        public void onItemClick(View view, int position);
    }
    public RecyclerViewListener(Context context, OnItemClickListener listener)
    {
        mListener = listener;
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener()
        {
            @Override public boolean onSingleTapUp(MotionEvent e)
            {
                return true;
            }
        });
    }
    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        View childView = rv.findChildViewUnder(e.getX(),e.getY());
        if(childView != null && mListener != null && mGestureDetector.onTouchEvent(e))
        {
            mListener.onItemClick(childView, rv.getChildAdapterPosition(childView));
            return true;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }
}

Following is the code for StudentId.java

package com.example.Aphexams;

/**
 * Created by HP on 24-05-2018.
 */

public class StudentId {
    private String StudentId;

    public String getStudentId() {
        return StudentId;
    }

    public void setStudentId(String studentId) {
        StudentId = studentId;
    }
}

following is my code for layout of activity_admin_show_student_id.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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="com.example.Aphexams.AdminShowStudentId">

   <android.support.v7.widget.RecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/studentidrecyclerview">


   </android.support.v7.widget.RecyclerView>

</android.support.v4.widget.NestedScrollView>

following is the code for studentid

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:id="@+id/studentidtv"
        android:textSize="16dp"
        android:text="now you see me!"/>

</LinearLayout>

I have tried to take some help from How to use ParseObjects with a RecyclerView?.


Solution

  • Taking a look at you code, I guess the error is that you set you adapter with a empty list at first, then you add items to your list in your FindCallback but you never tell the list to re-render the items.

    Have you checked when you set you adapter if the list is actually populated? Because since your query is running in background, there is a chance the below lines will be execute before your query finishes.

    studentidAdapter=new StudentIdAdapter(studentidarraylist);
    recycleerviewDetails.setAdapter(studentidAdapter);
    

    To achieve what you are trying to do, I guess you should call

    studentidAdapter=new StudentIdAdapter(studentidarraylist);
    recycleerviewDetails.setAdapter(studentidAdapter);
    studentidAdapter.notifyDataSetChanged();
    

    inside your FindCallback, when you are sure your list is populated.