当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Firebase MultiSelect Delete from ListView

问题描述:

I am currently pulling data from Firebase and putting it in a list view and all that is working fine.

The app is a deciding wheel and I need to let the users add and delete some of the choices based on their personal preference.

They will be able to select multiple items in the list view and then delete those all at once. I have not found any way to do this online that made any sense to me at all.

public class RestaurantSelectionList extends Fragment {

DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();

DatabaseReference mRestReference = mRootRef.child("restaurants");

List<String>listofrest = new ArrayList<String>();

ListView restaurantListView;

ListAdapter restaurantListAdapter;

public RestaurantSelectionList(){}

@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,mRestReference) {

@Override

protected void populateView(View v, Restaurants model, int position) {

TextView restName = (TextView) v.findViewById(R.id.restname);

restName.setText(model.getName());

listofrest.add(position,model.getName());

}

};

restaurantListView.setAdapter(restaurantListAdapter);

restaurantListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

restaurantListView.setItemsCanFocus(false);

restaurantListView.setOnItemClickListener(new AdapterView.OnItemClickListener()

{

@Override

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

Toast.makeText(getActivity(), "clicked", Toast.LENGTH_SHORT).show();

}

});

return view;

}

}

This is where I have my list view and I was wondering if this could even be done with Firebase? This is the first time I have ever worked with Firebase and it has caused me a lot of problems so far. This is the list that is shown:

网友答案:

I would setOnClickListener to the check box.
Initialize a global hashMap(). When a item is checked, put that item ID in the hashMap(), if unchecked remove the item ID from the hashMap(). Then take the reference of the node u want to update, execute updateChildren() method, setting the correspond Item ID's value to null.

分享给朋友:
您可能感兴趣的文章:
随机阅读: