mardi 7 avril 2015

Query SQLite with Editext value then display results in a ListView

So I am currently able to view SQLite data in a ListView but it will bring back all records due to the set statement in my DB controller. But I want the user to be able to add a value into a EditText and query the database with that value then display the results into the ListView. The following are the two current pieces of code I use just incase it helps. Any help will be appreciated thanks in advance!


In my MainActivity I call this in the oncreate to display the listview:



public void listRecs(){

ArrayList<HashMap<String, String>> scanList = this.controller.getAllRecsExpDESC();

myAdapter = new SimpleAdapter(this, scanList, R.layout.list_view_layout, new String[]{"regNo","streetName","recExpDate","tvPosition"},
new int[]{R.id.reg_No,R.id.street_Name,R.id.date_Time,R.id.tv_Position});

Recs.setAdapter(myAdapter);
}


And this is the code from my DB Controller:



public ArrayList<HashMap<String, String>> getAllRecsExpDESC() {
ArrayList<HashMap<String, String>> wordList;
wordList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM recs ORDER BY recExpDate DESC";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("regNo", cursor.getString(0));
map.put("tvPosition", cursor.getString(1));
map.put("streetName", cursor.getString(2));
map.put("recExpDate", cursor.getString(3));
wordList.add(map);
} while (cursor.moveToNext());
}
database.close();
return wordList;
}

Aucun commentaire:

Enregistrer un commentaire