不能使用getRef()?

时间:2016-12-10 16:07:47

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

任何人都可以解释为什么这说我不能使用getRef获取被点击的位置?从我查找的所有内容来看,这应该有效,并且由于某种原因它不是。

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 个答案:

答案 0 :(得分:2)

宣布 restaurantListAdapter为Name。即使您将ListAdapter对象放入该字段,也只能访问FirebaseListAdapter上定义的方法。

解决方案是声明字段为ListAdapter

FirebaseListAdapter