W / ClassMapper:没有类的setter / field [英] W/ClassMapper: No setter/field for class

查看:164
本文介绍了W / ClassMapper:没有类的setter / field的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm simply just trying to populate data from Firebase Database into my recyclerview. My recyclerview shows perfectly, but the adapter won't set the values to the text in recyclerview textview, All it says is " No setter/field for -L57t4-97c3dLZjA_yXC found on class com.example.barry.starcity.StreetClass". Which makes me think that I didn't have my setters made correctly, but there was auto-generated by Android Studio. I don't know what I am missing here. Any help is appreciated.

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>

这是我的数据库结构

< a href =https://i.stack.imgur.com/YBFzQ.png =nofollow noreferrer>

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

这是我的Log cat

This is my Log cat

看到这是我的新数据库

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

Logcat

推荐答案

即使我没有看到你的数据库结构,我也可以从您的代码中说出您要求街道问题下的所有内容,其中包括您之前推送的所有对象。我可以从错误消息中看到它正在尝试找到它在街道问题下找到的推送ID -L57t4-97c3dLZjA_yXC 的setter或字段节点。如果你想获得一个对象,你将不得不深入挖掘街道问题下的推送ID中的对象。您的代码应如下所示:

Even if I don't see your database structure, I can say from your code that your asking for everything under Street Problems, which includes all of the objects you pushed previously. I can see from the error message that it's trying to find a setter or field for the push ID -L57t4-97c3dLZjA_yXC that it found just under Street Problems node. If you want to get a single object, you're going to have to dig into the objects in the push IDs under Street Problems. Your code should look something like this:

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

如果你想要所有 StreetClass 对象,那么简单地循环遍历它的子节点:

If you want all StreetClass objects, then simply loop through its childrens like this:

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()<显示名称的示例/ code>方法。

This is an example on how to display the names using getName() method.

修改:

正如我所见在您的更新问题上,警告是因为您的字段和您的setter之间的大小不匹配。

As i see on your updatedquestion, the warning is because the casing mismatches between your field and you setter.

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

You have a field named and the corresponding setters and getters which are not correct!

private String sname;

public String getName() {
    return sname;
}

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

正确的setter和getter应该是:

The correct setters and getters should be:

public String getSname() {
    return sname;
}

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

同样的问题也与其他字段有关。

The same problems is also with the other fields.

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

This is the correct way of structuring your model class:

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类,如下所示:

You can also use the String class as below:

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

这篇关于W / ClassMapper:没有类的setter / field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆