i am trying to build an application that save data in sqlite database.i have a class that search in the database searching by VendorName and Date. The problem is i want it to display the MeterNumbers that was saved on the same Date . to what i have tried it only gives me the first result it get in the database, lets say i search for Tom that was saved on 01/01/2013, it supposed to display both metereNumbers of Tom that was saved on 01/01/2013
here is an example of my table in sqlite
VendorName Date MeterNumber
…Tom…. 01/01/2013 ……2098957438902
…Tom… 01/01/2013……..4786909876785
…Steven…18/01/2013………8978978906542
This is the Code in Sqlite database
public String getMeterNUmber(String MeterNumber) throws SQLException
{
String[] whereArgs = new String[]{MeterNumber};
Cursor mCursor = db.rawQuery("SELECT MeterNumber FROM " + SCAN_TABLE + " WHERE Date = ?",whereArgs);
mCursor.moveToFirst();
if(mCursor.getCount() > 0){
MeterNumber = mCursor.getString(0);
}
return MeterNumber;
}
this is the code in the class that calls for the query in the sqlite database and take the result to another class by intent
Intent updateCustomerIntent = new Intent(Searching.this, Result.class);
updateCustomerIntent.putExtra("product", dbUser.getMeterNUmber(ss));
startActivity(updateCustomerIntent);
First of all, your getMeterNumber method takes a meter number as an argument, as already pointed out in ggenglish’s comment, this seems odd, it should be a date no? Also, your method only calls mCursor.getString(0) once, and then returns that, which means you will only get the meter number from the first row of your result set.
If you want all the results, you will need to iterate over the full recordset. When you receive the cursor, it will be positioned just before the first row, so, you can loop through it with the method moveToNext(). Then, you will also have to store your result in a appropriate data structs, such as a ArrayList
So, you will end up with something like this
And then finally, use putStringArrayListExtra instead of putExtra when adding the data to the intent.