I am new to implementing sql in android I need to show a list after storing the data into my localdb below is my code snippet
private void getAllData() {
data.clear();
Cursor mCursr_all = SFMApp.localDBS.fetchMyPurchaseAllGroupBy(activity, SFMTableCreate.TABLE_SFM_STORE_MYPURCHASE, null);
Log.e("mCursor","::::::"+mCursr_all.getPosition()+":::::"+mCursr_all.getColumnNames());
data.addAll(new SFMCursorFactory().fetchMyPurchaseMaster(mCursr_all, SFMTableCreate.getColoumnArrayCallMYPURCHASE()));
if (mCursr_all == null) {
//Toast.makeText(activity,"Cursor value is null",Toast.LENGTH_SHORT).show();
} else {
for (int i = 0; i <data.size() ; i++) {
String data1 = data.get(i).getPurchaseNo();
Log.e("PURCHASE Key:::::","::::::::====="+data1);
}
purchaseAdapter = new SFMMyPurchaseAdapter(activity, data,SFMMyPurchase.this);
callloglist.setAdapter(purchaseAdapter);
}
dismissProgress();
}
here in the above code data is my arraylist of my data model fetchMyPurchaseAllGroupBy function is below
public Cursor fetchMyPurchaseAllGroupBy(Context context, String table, String[] columns) {
SQLiteDatabase db = context.openOrCreateDatabase(
SFMAppConfig.DB_NAME, SQLiteDatabase.OPEN_READWRITE, null);
String query;
query = "Select * from " + table + " GROUP BY "+SFMTableCreate.COLOUMNS_SFM_CALL_MYPURCHASE.PurchaseKey.name();
Log.e("Query", query);
Cursor mCursor = db.rawQuery(query, null);
/*Cursor mCursor = db.query(table, columns,
SFMTableCreate.COLOUMNS_SFM_ASSET_TAG_LOG_MASTER.vcCode.name(), null, null, null, SFMTableCreate.COLOUMNS_SFM_ASSET_TAG_LOG_MASTER.vcCode.name() + " DESC");*/
if (mCursor != null && mCursor.getCount() != 0 && mCursor.moveToFirst()) {
Log.e("fetchAll", "notNull");
Log.e("row count", "" + mCursor.getCount());
} else {
Log.e("fetchAll", "Null");
mCursor = null;
}
db.close();
return mCursor;
}
and fetchMyPurchaseMaster func is below
public ArrayList<MyPurchaseData> fetchMyPurchaseMaster(Cursor mCursor, String[] columns) {
ArrayList<MyPurchaseData> aData = new ArrayList<MyPurchaseData>();
if (mCursor == null)
return aData;
int[] columnIndex = new int[columns.length];
for (int i = 0; i < columnIndex.length; i++)
columnIndex[i] = mCursor.getColumnIndex(columns[i]);
Log.e("ColumnIndex",":::::"+columnIndex);
do {
MyPurchaseData data = new MyPurchaseData();
int j = 0;
data.setPurchaseKey(mCursor.getInt(columnIndex[j++]));
data.setPurchaseDate(mCursor.getString(columnIndex[j++]));
data.setPurchaseNo(mCursor.getString(columnIndex[j++]));
data.setNetAmount(mCursor.getInt(columnIndex[j++]));
data.setVendorName(mCursor.getString(columnIndex[j++]));
data.setItemName(mCursor.getString(columnIndex[j++]));
data.setUnit(mCursor.getString(columnIndex[j++]));
data.setQTY(mCursor.getInt(columnIndex[j++]));
data.setRATE(mCursor.getInt(columnIndex[j++]));
j = 0;
aData.add(data);
} while (mCursor.moveToNext());
Log.e("cursor size", String.valueOf(aData.size()));
return aData;
}
below is how my screen should look like
my json response link http://mobile.spectrafm.com/SPFMWEBAPI2/api/Stores/GetPurchaseList/03/2018/SPFM002
here please help me how to use GROUPBY query and make the screen appear like the image above given
Note currently am getting as