I have set up my SQLiteOpenHelper class and all working ok to retrieve all data in the sqLite database. I now want tp pass a parameter from mainActivity into my SQLiteOpenHelper class to find certain records. User will enter search criteria into a field and i want to pass that user entry to be searched. Currently i have hard coded to search for category 161 but this is where i want to be able to use the parameter passed by the user. I have put my SQLiteOpenerHelper code and my mainActivty code below. I have tried passing a parameter into the getAllUsers() in mainActivity file but Android will not accept parameters. Can someone please help in showing me what i need to do to pass the parameter i need into the sqLite query
SQLiteOpenHelper class
public ArrayList<HashMap<String, String>> getAllUsers() {
ArrayList<HashMap<String, String>> usersList;
usersList = new ArrayList<HashMap<String, String>>();
String[] columns = {"id", "company", "firstname", "lastname", "category"};
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.query("members", columns, "category = 161", null, null, null, "company");
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", cursor.getString(0));
map.put("company", cursor.getString(1));
map.put("firstname", cursor.getString(2));
map.put("lastname", cursor.getString(3));
map.put("category", cursor.getString(4));
usersList.add(map);
} while (cursor.moveToNext());
}
database.close();
return usersList;
}
mainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get User records from SQLite DB
ArrayList<HashMap<String, String>> userList = controller.getAllUsers();
I don't see a problem here. Just define the method as:
public ArrayList < HashMap< String, String >> getAllUsers(int category){
//
}
Then instead of "category = 161"
, use "category = " + category
as the third parameter in query.
And call it from main activity as:
ArrayList < HashMap< String , String>> userList = controller.getAllUsers(161);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments