W/ClassMapper:类没有设置器/字段

Mru*_*nal 4 java android android-fragments firebase firebase-realtime-database

我只是想将 Firebase 数据库中的数据填充到我的 recyclerview 中。我的 recyclerview 显示完美,但适配器不会将值设置为 recyclerview textview 中的文本,它说的是“在类 com.example.barry.starcity.StreetClass 上找不到 -L57t4-97c3dLZjA_yXC 的设置器/字段”。这让我觉得我没有正确制作我的设置器,但是 Android Studio 自动生成了。我不知道我在这里缺少什么。任何帮助表示赞赏。

节点对象

  class StreetClass {

private String id;
private String semail;
private String sname;
private String stype;
private String sdetail;
private String slocation;
private String sdate;
private String imgurl;

public StreetClass(){

}

public String getImgurl() {
    return imgurl;
}

public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
}

public String getId() {return id;}

public void setId(String id) {
    this.id = id;
}

public String getEmail() {
    return semail;
}

public void setEmail(String email) {
    this.semail = email;
}

public String getName() {
    return sname;
}

public void setName(String name) {
    this.sname = name;
}

public String getType() {
    return stype;
}

public void setType(String type) {
    this.stype = type;
}

public String getDetail() {
    return sdetail;
}

public void setDetail(String detail) {
    this.sdetail = detail;
}

public String getLocation() {
    return slocation;
}

public void setLocation(String location) {
    this.slocation = location;
}

public String getDate() {
    return sdate;
}

public void setDate(String date) {
    this.sdate = date;
}
}
Run Code Online (Sandbox Code Playgroud)

回收器视图适配器

 public class RecyclerViewAdapter extends 
 RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

Context context;
List<StreetClass>listdata ;

public RecyclerViewAdapter(Context context, List<StreetClass> list) {

    this.listdata = list;
    this.context = context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {

    StreetClass AllDetails = listdata.get(position);

    holder.NameTextView.setText(AllDetails.getName());
    holder.DetailTextView.setText(AllDetails.getDetail());
    holder.EmailTextView.setText(AllDetails.getEmail());
    holder.DateTextView.setText(AllDetails.getDate());
    holder.LocationTextView.setText(AllDetails.getLocation());
    holder.TypeTextView.setText(AllDetails.getType());
}

@Override
public int getItemCount() {

    return listdata.size();
}

class ViewHolder extends RecyclerView.ViewHolder {

    public TextView NameTextView;
    public TextView DetailTextView;
    public TextView EmailTextView;
    public TextView DateTextView;
    public TextView LocationTextView;
    public TextView TypeTextView;
    /*public ImageView ImageTextView;*/

    public ViewHolder(View itemView) {

        super(itemView);
        NameTextView = itemView.findViewById(R.id.ShowNameTextView);
        DetailTextView = itemView.findViewById(R.id.ShowDetailTextView);
        EmailTextView = itemView.findViewById(R.id.ShowEmailTextView);
        DateTextView = itemView.findViewById(R.id.ShowDateTextView);
        LocationTextView = itemView.findViewById(R.id.ShowLocationTextView);
        TypeTextView = itemView.findViewById(R.id.ShowTypeTextView)

    }
  }
 }
Run Code Online (Sandbox Code Playgroud)

主要活动

  public class StatusFragment extends Fragment {

DatabaseReference databaseStatus;
ProgressDialog progressDialog;
List<StreetClass> list = new ArrayList<StreetClass>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;

public StatusFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_status, container, false);

    recyclerView = rootView.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext().getApplicationContext()));

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Loading Data from Firebase Database");
    progressDialog.show();

    databaseStatus = FirebaseDatabase.getInstance().getReference().child("Street Problems");
    databaseStatus.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {

                StreetClass streetClass = dataSnapshot.getValue(StreetClass.class);

                list.add(streetClass);

            }
            adapter = new RecyclerViewAdapter(getContext().getApplicationContext(), list);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
            progressDialog.dismiss();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });

    return  rootView;
}
Run Code Online (Sandbox Code Playgroud)

和一个项目的布局:

    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardview1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardElevation="5dp"
    card_view:contentPadding="5dp"
    card_view:cardCornerRadius="5dp"
    card_view:cardMaxElevation="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ECEFF1"
        android:padding="10dp">

        <TextView
            android:id="@+id/Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Student Name: "
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/Detail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Detail: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Name"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/Email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Email: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Detail"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Date: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Email"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/Location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Location "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Date"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

      <!--  <TextView
            android:id="@+id/Image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/Type"
            android:layout_toStartOf="@+id/ShowNameTextView"
            android:text="Uploaded image: "
            android:textColor="#000"
            android:textSize="10dp" />-->

        <TextView
            android:id="@+id/Type"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Type: "
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_below="@+id/Location"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />



        <TextView
            android:id="@+id/ShowNameTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Show Student Name"
            android:textColor="#000"
            android:textSize="10dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/Name"
            android:layout_toEndOf="@+id/Name"
            android:layout_marginLeft="19dp"
            android:layout_marginStart="19dp" />


        <TextView
            android:id="@+id/ShowDetailTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowNameTextView"
            android:layout_below="@+id/ShowNameTextView"
            android:gravity="center"
            android:text="Show Detail"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowEmailTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowNameTextView"
            android:layout_below="@+id/ShowDetailTextView"
            android:gravity="center"
            android:text="Show Email"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowDateTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowEmailTextView"
            android:layout_below="@+id/ShowEmailTextView"
            android:gravity="center"
            android:text="Show Date"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowLocationTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowDateTextView"
            android:layout_below="@+id/ShowDateTextView"
            android:gravity="center"
            android:text="Show Location"
            android:textColor="#000"
            android:textSize="10dp" />

        <TextView
            android:id="@+id/ShowTypeTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/ShowDateTextView"
            android:layout_below="@+id/ShowLocationTextView"
            android:gravity="center"
            android:text="Show Type"
            android:textColor="#000"
            android:textSize="10dp" />

      <!--  <android.support.v7.widget.AppCompatImageView
            android:id="@+id/ShowImageView"
            android:layout_width="90dp"
            android:layout_height="50dp"
            android:layout_alignStart="@+id/ShowTypeTextView"
            android:layout_below="@+id/ShowTypeTextView" />-->



    </RelativeLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud)

这是我的数据库结构

在此处输入图片说明

    java.lang.NullPointerException: Attempt to invoke virtual method 'void 
    android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a 
    null object reference
Run Code Online (Sandbox Code Playgroud)

这是我的日志猫

在此处输入图片说明

看这是我的新数据库

在我的应用程序中它看起来像这样

逻辑猫

Ale*_*amo 5

即使我没有看到您的数据库结构,我也可以从您的代码中说您要求 下的所有内容Street Problems,其中包括您之前推送的所有对象。我可以从错误信息看,它的试图找到一个设置或场的推ID -L57t4-97c3dLZjA_yXC,它发现只有不到Street Problems节点。如果您想获取单个对象,您将不得不深入研究 下推送 ID 中的对象Street Problems。您的代码应如下所示:

dataSnapshot.child("Street Problems/-L57t4-97c3dLZjA_yXC").getValue(StreetClass.class);
Run Code Online (Sandbox Code Playgroud)

如果你想要所有StreetClass对象,那么只需像这样循环遍历它的子对象:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Street Problems");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                StreetClass streetClass = dSnapshot.getValue(StreetClass.class);
                Log.d("TAG", streetClass.getName());
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
Run Code Online (Sandbox Code Playgroud)

这是一个关于如何使用getName()方法显示名称的示例。

编辑:

正如我在您更新的问题中看到的那样,警告是因为您的字段和您的设置者之间的大小写不匹配。

您有一个名为的字段以及相应的不正确的 setter 和 getter!

private String sname;

public String getName() {
    return sname;
}

public void setName(String name) {
    this.sname = name;
}
Run Code Online (Sandbox Code Playgroud)

正确的 setter 和 getter 应该是:

public String getSname() {
    return sname;
}

public void setSname(String name) {
    this.sname = name;
}
Run Code Online (Sandbox Code Playgroud)

其他领域也存在同样的问题。

这是构建模型类的正确方法:

public class StreetClass {
    private String id;
    private String semail;
    private String sname;
    private String stype;
    private String sdetail;
    private String slocation;
    private String sdate;
    private String imgurl;

    public StreetClass(){}

    public StreetClass(String id, String semail, String sname, String stype, String sdetail, String slocation, String sdate, String imgurl) {
        this.id = id;
        this.semail = semail;
        this.sname = sname;
        this.stype = stype;
        this.sdetail = sdetail;
        this.slocation = slocation;
        this.sdate = sdate;
        this.imgurl = imgurl;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSemail() {
        return semail;
    }

    public void setSemail(String semail) {
        this.semail = semail;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getStype() {
        return stype;
    }

    public void setStype(String stype) {
        this.stype = stype;
    }

    public String getSdetail() {
        return sdetail;
    }

    public void setSdetail(String sdetail) {
        this.sdetail = sdetail;
    }

    public String getSlocation() {
        return slocation;
    }

    public void setSlocation(String slocation) {
        this.slocation = slocation;
    }

    public String getSdate() {
        return sdate;
    }

    public void setSdate(String sdate) {
        this.sdate = sdate;
    }

    public String getImgurl() {
        return imgurl;
    }

    public void setImgurl(String imgurl) {
        this.imgurl = imgurl;
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用 String 类,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference yourRef = rootRef.child("Street Problems");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            for(DataSnapshot dSnapshot : ds.getChildren()) {
                String sname = dSnapshot.child("sname").getValue(String.class);
                Log.d("TAG", sname);
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
yourRef.addListenerForSingleValueEvent(eventListener);
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

6113 次

最近记录:

7 年,8 月 前