Java 使用parseObject实现RecyclerView但得到一个空白屏幕
我正在尝试使用ParseObject实现RecyclerView,但在我的屏幕上没有显示任何内容 以下是要实现recycler视图的活动的代码 AdminShowStudentId.javaJava 使用parseObject实现RecyclerView但得到一个空白屏幕,java,android,android-recyclerview,android-parser,parse-android-sdk,Java,Android,Android Recyclerview,Android Parser,Parse Android Sdk,我正在尝试使用ParseObject实现RecyclerView,但在我的屏幕上没有显示任何内容 以下是要实现recycler视图的活动的代码 AdminShowStudentId.java package com.example.Aphexams; import android.content.Intent; import android.os.Bundle; import android.app.Activity; import andro
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);
}
}));
}
}
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;
}
}
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;
}
}
下面是StudentId.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);
}
}));
}
}
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;
}
}
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;
}
}
下面是我的活动_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>
.看看您的代码,我猜错误在于您首先使用空列表设置适配器,然后在FindCallback
中将项目添加到列表中,但从未告诉列表重新呈现项目
您是否检查了设置适配器时列表是否已填充?因为您的查询是在后台运行的,所以有可能在查询完成之前执行以下几行
studentidAdapter=new StudentIdAdapter(studentidarraylist);
recycleerviewDetails.setAdapter(studentidAdapter);
为了实现你想做的事,我想你应该打电话
studentidAdapter=new StudentIdAdapter(studentidarraylist);
recycleerviewDetails.setAdapter(studentidAdapter);
studentidAdapter.notifyDataSetChanged();
在FindCallback中,当您确定列表已填充时。因此,我在代码中做了以下更改
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);
}
}));
}
}
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;
}
}
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;
}
}
Thiago Loddi建议的
公共类AdminShowStudentId扩展活动{
私人RecyclerView RecyclerViewDetails;
private RecyclerView.LayoutManager LayoutManager;
私人学生公寓;
私立ArrayList学生名单;
私有布尔值notComplete=true;
私有字符串studId;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u admin\u show\u student\u id);
RecycleViewDetails=(RecyclerView)findViewById(R.id.StudentidRecycleView);
如果(RecyclerViewDetails!=null)
{
RecyclierViewDetails.setHasFixedSize(true);
}
layoutManager=新的GridLayoutManager(这个,1);
setLayoutManager(layoutManager);
studentidarraylist=new ArrayList();
如果(未完成){
ParseQuery=ParseQuery.getQuery(“studAuth”);
findInBackground(新的FindCallback(){
@凌驾
公共void done(列出对象,parsee异常){
如果(e==null){
对于(inti=0;它在Thiago Loddi上有很多漏洞……你的建议帮助我消除了这个漏洞。