Cant use getRef()?

时光总嘲笑我的痴心妄想 提交于 2021-01-27 18:02:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!