本文介绍了不能使用getRef()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以解释为什么这是在说我不能使用getRef来获取被点击的位置吗?从我查看过的所有内容来看,这应该有效,并且出于某种原因,它不起作用.
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;
}
}
推荐答案
您已声明 restaurantListAdapter作为 ListAdapter
.即使将 FirebaseListAdapter
对象放入该字段中,您也只能访问在 ListAdapter
上定义的方法.
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
.
解决方案是还声明该字段作为 FirebaseListAdapter
:
The solution is to also declare the field as a FirebaseListAdapter
:
FirebaseListAdapter restaurantListAdapter;
这篇关于不能使用getRef()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!