I am currently trying to create a search function for a ListView for an SQLite database. But to do that I need to convert my database values that are in an array list "userList" to string so it can be used in the menu? I need userList to be an array of strings so I can use them in the search function.
Edit: Instead of doing this could i put only the "Id entry" variable in an arraylist? How could I do this?
Here is my code:
public class ViewListContents extends AppCompatActivity {
DatabaseHelper deviceDB;
ArrayList<User> userList;
//ArrayList<String> listItem;
ListView listView;
User user;
ArrayAdapter adapter1;
ArrayList<String> list;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcontents_layout); //sets layout
deviceDB = new DatabaseHelper(this); //initialises DB for database class
userList = new ArrayList<>();
Cursor data = deviceDB.getListContents();
list = new ArrayList<>();
//FiveColumn_ListAdapter adapter1 = new FiveColumn_ListAdapter(this,R.layout.list_adapter_view,list);
int numRows = data.getCount();
if (numRows == 0 ){
Toast.makeText(ViewListContents.this,"The database was empty",Toast.LENGTH_LONG).show();
}
else {
while (data.moveToNext()){
user = new User(data.getString(0),data.getString(1),data.getString(2),data.getString(3),data.getString(4),data.getString(5)); //retrieving columns from data
userList.add(user); //userlist array takes items of type user
}
}
FiveColumn_ListAdapter adapter = new FiveColumn_ListAdapter(this,R.layout.list_adapter_view,userList);
listView = (ListView) findViewById(R.id.ListView);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
MenuItem searchItem = menu.findItem(R.id.item_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
//ArrayList<String> List = new ArrayList<>();
for (User user : userList){
if (user.
}
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
}
User is a self defined class I made to get the variables here:
public User (String fID_Entry, String fName, String fAddress, String fLatitude, String fLongtitude, String fTime){
ID_Entry = fID_Entry;
Name = fName;
Address = fAddress;
Latitude = fLatitude;
Longtitude = fLongtitude;
Time = fTime;
}
public String getID_Entry() {
return ID_Entry;
}
public String getName() {
return Name;
}
public String getAddress() {
return Address;
}
public String getLatitude() {
return Latitude;
}
public String getLongtitude() {
return Longtitude;
}
public String getTime() {
return Time;
}
}