如何使用微调框过滤Firebase数据库中的Recyclerview?我已经尝试做过,但是当我从微调器中选择时什么也没有发生。我想使用微调器作为类别从Firebase过滤数据。我希望有一个人可以帮助我!这是我的示例代码。
public class HistoricalSiteMenuFragment extends Fragment {
private static FirebaseDatabase firebaseDatabase;
private DatabaseReference dbChild1, dbChild2;
private RecyclerView historicalList;
private Query query;
Double latitude, longitude;
Spinner sp1;
ArrayAdapter<CharSequence> adapter1;
String selected;
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_historical_site_menu, container, false);
/*Spinner Category*/
sp1 = (Spinner) view.findViewById(R.id.spinner);
adapter1 = ArrayAdapter.createFromResource(getContext(), R.array.categories, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter1);
String selected = sp1.getSelectedItem().toString();
Bundle bundle = getArguments();
String name = bundle.getString("name");
String nameLower = name.toLowerCase();
nameLower = nameLower.replace(" city", "");
firebaseDatabase = FirebaseDatabase.getInstance();
if (selected.equals("Historical Site")){
dbChild1 = firebaseDatabase.getReference().child("Attractions");
dbChild2 = dbChild1.child(nameLower);
query = dbChild2.orderByChild("category").equalTo("historical");
query.keepSynced(true);
historicalList = (RecyclerView) view.findViewById(R.id.historical_list);
historicalList.setHasFixedSize(true);
historicalList.setLayoutManager(new LinearLayoutManager(getContext()));
}else if (selected.equals("Ecological Site")){
dbChild1 = firebaseDatabase.getReference().child("Attractions");
dbChild2 = dbChild1.child(nameLower);
query = dbChild2.orderByChild("category").equalTo("ecological");
query.keepSynced(true);
historicalList = (RecyclerView) view.findViewById(R.id.historical_list);
historicalList.setHasFixedSize(true);
historicalList.setLayoutManager(new LinearLayoutManager(getContext()));
}else{
dbChild1 = firebaseDatabase.getReference().child("Attractions");
dbChild2 = dbChild1.child(nameLower);
query = dbChild2;
query.keepSynced(true);
historicalList = (RecyclerView) view.findViewById(R.id.historical_list);
historicalList.setHasFixedSize(true);
historicalList.setLayoutManager(new LinearLayoutManager(getContext()));
}
return view;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<HistoricalList,ButtonViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<HistoricalList,ButtonViewHolder>
(HistoricalList.class,R.layout.cardview_historical,ButtonViewHolder.class,query) {
@Override
protected void populateViewHolder(ButtonViewHolder viewHolder, HistoricalList model, int position) {
viewHolder.sethName(model.gethName());
viewHolder.sethAddress(model.getAddress());
viewHolder.sethImage(getActivity().getApplicationContext(),model.gethImage());
}
@Override
public ButtonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ButtonViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new ButtonViewHolder.ClickListener() {
@Override
public void onItemClick(View view, int position) {
//get data from firebase at the position clicked
String h_name = getItem(position).gethName();
String h_image = getItem(position).gethImage();
latitude = getItem(position).getLatitude();
longitude = getItem(position).getLongitude();
String info = getItem(position).getInfo();
String address = getItem(position).getAddress();
//pass this data to new Activity
final HistoricalSiteFragment hfrag = new HistoricalSiteFragment();
Bundle bundle = new Bundle();
bundle.putString("historical_name",h_name);
bundle.putString("historical_image",h_image);
bundle.putString("latitude",Double.toString(latitude));
bundle.putString("longitude",Double.toString(longitude));
bundle.putString("info",info);
bundle.putString("address",address);
hfrag.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.main_fragment_container,hfrag).addToBackStack(null).commit();
}
});
return viewHolder;
}
};
historicalList.setAdapter(firebaseRecyclerAdapter);
}
答案 0 :(得分:0)
您必须使用项目选择的侦听器,如下所示-
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
在这里,选定的项目只是处于选定位置,并过滤出您的数据。