使用onClick从firebase中的列表中删除特定项

时间:2016-12-10 09:28:41

标签: java android firebase

我试过阅读它上面的文档,我知道我需要使用removeValue()来实现这一点,我只是不知道如何获得点击的位置。我从firebase中删除列表并尝试使用OnItemClick()来表示单击listview中的某些内容时,它将被删除。我觉得这很简单,我只是缺少一些东西。

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) {


        }
    });
    return view;

}

}

现在36小时没有睡觉,所以我的大脑已100%死亡。如有必要,我可以提供更多细节

1 个答案:

答案 0 :(得分:0)

像这样实现你的setOnItemClickListener

restaurantListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    Firebase itemtoRemove = restaurantListAdapter.getRef(i);
    itemtoRemove.removeValue();

    }
});

添加你的gradle

compile 'com.google.firebase:firebase-core:9.0.1'

compile 'com.google.firebase:firebase-database:9.0.1'