使用 parseObject 实现 RecyclerView 但出现空白屏幕

Implementing RecyclerView with parseObject but getting a blank screen

我正在尝试使用 ParseObject 实现 RecyclerView,但在我的屏幕上没有显示任何内容!

以下是 activity 实现回收器视图的代码。

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);
            }
        }));








       }
  }

以下是 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);

        }
    }
}

以下是 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) {

    }
}

以下是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;
    }
}

以下是我的 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>

以下是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>

我试图从 How to use ParseObjects with a RecyclerView? 那里寻求一些帮助。

看看您的代码,我想错误是您首先将适配器设置为空列表,然后将项目添加到 FindCallback 中的列表,但您从未告诉列表重新渲染项目。

您在设置适配器时是否检查过列表是否已填充?因为您的查询是 运行 在后台进行的,所以在您的查询完成之前,下面的行有可能会被执行。

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

为了实现你想要做的事情,我想你应该打电话给

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

当您确定列表已填充时,在您的 FindCallback 中。

因此,我对代码进行了以下更改。

AdminShowStudentId.java

As suggested by Thiago Loddi

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);

    recycleerviewDetails=(RecyclerView)findViewById(R.id.studentidrecyclerview);
    if (recycleerviewDetails != null)
    {
        recycleerviewDetails.setHasFixedSize(true);
    }
    layoutManager=new GridLayoutManager(this,1);
    recycleerviewDetails.setLayoutManager(layoutManager);

    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(int i=0;i<objects.size();i++)
                        {
                            String studentidstring = (String) objects.get(i).get("StudUserName");
                            StudentId studentId=new StudentId();
                            studentId.setStudentId(studentidstring);
                            studentidarraylist.add(studentId);
                        }
                        //Now the arraylist is populated!
                        for(int i=0;i<studentidarraylist.size();i++)
                        {
                            Toast.makeText(AdminShowStudentId.this,studentidarraylist.get(i).getStudentId() + "  "+i, Toast.LENGTH_SHORT).show();

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

                        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);
                                startActivity(i);
                            }
                        }));

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

                }
            });

            notComplete=false;

        }



}

}

成功了!!!