我正在尝试使用 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>