当我尝试从Firestore访问数据并将其显示在listview片段类中时,我的应用程序崩溃。任何帮助对我来说都是有益的
availablestudent_fragment.java(主要片段类)
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.firestore.auth.User;
import java.util.ArrayList;
import java.util.List;
public class availablestudent_fragment extends Fragment {
private static final String TAG ="FireLog" ;
private RecyclerView nmainlist;
private FirebaseFirestore mfirestore;
private List<Users> usersList;
private UserListAdapter userListAdapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_availablestudents,null);
nmainlist=(RecyclerView)v.findViewById(R.id.main_list);
nmainlist.setHasFixedSize(true);
nmainlist.setLayoutManager(new LinearLayoutManager(getContext()));
nmainlist.setAdapter(userListAdapter);
mfirestore=FirebaseFirestore.getInstance();
usersList=new ArrayList<>();
userListAdapter=new UserListAdapter(usersList);
mfirestore.collection("users").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.d(TAG, "Error" + e.getMessage());
}
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
Users users = doc.getDocument().toObject(Users.class);
usersList.add(users);
userListAdapter.notifyDataSetChanged();
}
}
}
});
return v;
}
}
users.java(模型类)
package com.kiit.projectmanager;
public class Users {
String Student_Name,Roll_Number;
public Users(){
}
public Users(String student_Name, String roll_Number) {
this.Student_Name = student_Name;
this.Roll_Number = roll_Number;
}
public String getStudent_Name() {
return Student_Name;
}
public void setStudent_Name(String student_Name) {
this.Student_Name = student_Name;
}
public String getRoll_Number() {
return Roll_Number;
}
public void setRoll_Number(String roll_Number) {
this.Roll_Number = roll_Number;
}
}
UsersListAdapter.java
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.google.firebase.firestore.auth.User;
import org.w3c.dom.Text;
import java.util.List;
public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder> {
public List<Users> usersList;
public UserListAdapter(List<Users> usersList){
this.usersList=usersList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.StudentName.setText(usersList.get(position).getStudent_Name());
holder.RollNumber.setText(usersList.get(position).getRoll_Number());
}
@Override
public int getItemCount() {
return usersList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
View mview;
public TextView StudentName;
public TextView RollNumber;
public ViewHolder(@NonNull View itemView) {
super(itemView);
mview=itemView;
StudentName=(TextView)mview.findViewById(R.id.name);
RollNumber=(TextView)mview.findViewById(R.id.rollnumber);
}
}
}
list_item.xml
{ {2}}logcat_Error(当我尝试单击片段时,我的应用程序崩溃了)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:padding="13dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/mirza_semibold"
android:text="NAME"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:textSize="18dp" />
<TextView
android:id="@+id/rollnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/fjalla_one"
android:text="ROLLNUMBER"
android:textColor="@color/common_google_signin_btn_text_dark_focused"
android:textSize="14dp" />
</LinearLayout>
Fragment_availablestudents.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>
从单元格检索错误以使其更加清晰
{{2 }}答案 0 :(得分:2)
要解决此问题,您需要根据Java Naming Conventions将Users
类中的字段和方法的大小写固定。因此,您的模型类应如下所示:
public class Users {
String studentName, rollNumber;
public Users(){}
public Users(String studentName, String rollNumber) {
this.studentName = studentName;
this.rollNumber = rollNumber;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getRollNumber() {
return rollNumber;
}
public void setRollNumber(String rollNumber) {
this.rollNumber = rollNumber;
}
}
删除旧数据,添加新数据,一切正常。