I'm using a firebase-database to contain data which I want to be able to search through. I can insert my data from Firebase into an object. I want to be able to search through all elements in all objects, but only want two values shown in the ListView. I have the following code which retrieve data from Firebase, inserts it into an object and returns two values, which I'm able to search through.
My problem is as said that I can easily make it search through all elements by simply returning all in the "getData()", but I only want the Name and Phone Number shown in the ListView. Anyone who have an idea on how to do that?
Object class:
private String car_Value;
private String interest;
private String driverLicenseNr;
private String name;
private String personNr;
private String phone_Number;
public Buyers(Map<String,String> map){
car_Value = map.get("Bil");
interest = map.get("Interesse");
driverLicenseNr = map.get("Kørekortnr");
name = map.get("Navn");
personNr = map.get("Personnr");
phone_Number = map.get("Telefonnr");
}
String getData()
{
return (name + " " + phone_Number);
}
Searching class:
private DatabaseReference mDatabaseReference;
private ListView mUserList;
private SearchView searchModule;
private ArrayList<String> mUsernames = new ArrayList<>();
//private ArrayList<String> filteredUsernames = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_existing_customer);
searchModule = (SearchView) findViewById(R.id.SearchTab);
mDatabaseReference = FirebaseDatabase.getInstance().getReference("Buyers");
mUserList = (ListView) findViewById(R.id.UserListView);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mUsernames);
//final ArrayAdapter<String> filteredArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filteredUsernames);
mUserList.setAdapter(arrayAdapter);
mDatabaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren())
{
Map<String, String> map = (Map<String, String>) childSnapshot.getValue();
Buyers buyerList = new Buyers(map);
mUsernames.add(buyerList.getData());
//filteredUsernames.add(buyerList.getData());
arrayAdapter.notifyDataSetChanged();
}
searchModule.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
arrayAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
arrayAdapter.getFilter().filter(query);
arrayAdapter.notifyDataSetChanged();
return true;
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
You would need to customize ArrayAdapter to use a special Filter object.
It would be easier to store all data outside of the ListView, and only add the filtered data to the ListView. (then there is no need to customize ArrayAdapter)