0

我正在尝试使用 ParseObject 实现 RecyclerView 但在我的屏幕上什么都没有出现!

以下是要实现回收器视图的活动的代码。

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 中获得一些帮助?.

4

2 回答 2

0

看看你的代码,我猜错误是你首先为你的适配器设置了一个空列表,然后你将项目添加到你的列表中,FindCallback但你从来没有告诉列表重新渲染这些项目。

如果实际填充了列表,您是否在设置适配器时检查过?因为由于您的查询在后台运行,所以有可能在您的查询完成之前执行以下行。

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

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

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

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

于 2018-05-24T18:49:52.190 回答
0

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

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;

        }



}

}

它奏效了!!!

于 2018-05-25T07:22:16.040 回答