问题
Can anyone explain why this is saying I can not use getRef to get the position that is being clicked? From everything I have looked up, this should work and for some reason it is not.
public class DeleteChoiceListFragment extends Fragment {
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference mRestReference = mRootRef.child("restaurants");
List<String> listofrest = new ArrayList<String>();
ListView restaurantListView;
ListAdapter restaurantListAdapter;
public DeleteChoiceListFragment() {
}
@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_nocheckbox, mRestReference) {
@Override
protected void populateView(View v, Restaurants model, final int position) {
TextView restName = (TextView) v.findViewById(R.id.restname);
restName.setText(model.getName());
listofrest.add(position, model.getName());
}
};
restaurantListView.setAdapter(restaurantListAdapter);
restaurantListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Firebase itemtoremove = restaurantListAdapter.getRef(i);
//I get an error here saying I can't use .getRef()
itemtoremove.removeValue();
}
});
return view;
}
}
回答1:
You've declared restaurantListAdapter as a ListAdapter
. Even when you put a FirebaseListAdapter
object into that field, you can only access method that are defined on ListAdapter
.
The solution is to also declare the field as a FirebaseListAdapter
:
FirebaseListAdapter restaurantListAdapter;
来源:https://stackoverflow.com/questions/41077476/cant-use-getref