W / ClassMapper:类

时间:2018-02-12 08:16:44

标签: java android firebase android-fragments firebase-realtime-database

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

NODE OBJECT

  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;
}
}

RecyclerViewAdapter

 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)

    }
  }
 }

MainActivity

  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;
}

项目的布局:

    <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>

这是我的数据库结构

enter image description here

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

这是我的Log cat

enter image description here

see this is my new database

In my App it looks like this

Logcat

1 个答案:

答案 0 :(得分:3)

即使我没有看到您的数据库结构,我也可以从您的代码中说明您要求Street Problems下的所有内容,其中包含您之前推送的所有对象。我可以从错误消息中看到它正在尝试查找它在-L57t4-97c3dLZjA_yXC节点下找到的推送ID Street Problems的setter或字段。如果您想获得单个对象,则必须深入了解Street Problems下的推送ID中的对象。您的代码应如下所示:

dataSnapshot.child("Street Problems/-L57t4-97c3dLZjA_yXC").getValue(StreetClass.class);

如果你想要所有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);

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

修改

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

您有一个已命名的字段以及相应的setter和getter,这些字段不正确!

private String sname;

public String getName() {
    return sname;
}

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

正确的二传手和吸气者应该是:

public String getSname() {
    return sname;
}

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

同样的问题也与其他领域有关。

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

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;
    }
}

您也可以使用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);