我看到其他人问过这个问题,但在我看来,我已经完成了所有需要做的事情,但我仍然无法让它发挥作用。我收到在类错误中找不到名称的 setter /字段。 Database 我附上了我的数据库的图片。这非常简单。
public class Restaurants {
private String name;
public Restaurants(){}
public Restaurants(String name)
{
this.name = name;
}
public String getRestaurant()
{
return name;
}
public void setRestaurant(String name)
{
this.name = name;
}
@Override
public String toString() {
return "Restaurants" +
"name" + name;
}
}
我这里有我的 getter 和 setter,所以理论上它应该可以工作。
public class RestaurantSelectionList extends Fragment {
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mRestReference = mRootRef.child("restaurants");
List<String>listofrest = new ArrayList<String>();
ListView restaurantListView;
ListAdapter restaurantListAdapter;
public RestaurantSelectionList(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.restaurant_selection_list_frag,container,false);
restaurantListView = (ListView) view.findViewById(R.id.restaurantListView);
restaurantListAdapter = new FirebaseListAdapter<Restaurants>(getActivity(),Restaurants.class,R.layout.individual_restaurant_name,mRestReference) {
@Override
protected void populateView(View v, Restaurants model, int position) {
TextView restName = (TextView) v.findViewById(R.id.restname);
restName.setText(model.getRestaurant());
listofrest.add(position,model.getRestaurant());
}
};
restaurantListView.setAdapter(restaurantListAdapter);
restaurantListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_SHORT).show();
}
});
return view;
}
这是调用它的代码。请告诉我我错过了什么。这将持续 4 个小时。
最佳答案
请将您的模型类更改为:
public class Restaurants {
private String name;
public Restaurants(){}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
@Override
public String toString() {
return "Restaurants" + "name" + name;
}
}
关于java - 在 Firebase 类中找不到名称的 setter /字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047421/