I am trying to populate my expandable list from a SQLite DB, I got the expandable list tutorial from androidhive but it was for static data.
I got it working about 80% but now my problem is that the data gets populated under all the headers and not their unique headers. I think my problem is because all header and child data is using the same arrays and need to be unique to the headers?
What I was thinking was creating arrays dynamically with variable names but I have no idea how to do that and can't think of another way of doing this since this is my first time working with expandable lists.
Here is my code. I also have a feeling the way I am doing the nested cursors isn't the right way
String sqlQ = "SELECT Distinct "
+ "strftime('%d-%m-%Y', "+ SQLdb.DB_TIMEOPEN +") header " +
" FROM " + SQLdb.DICTIONARY_TABLE_NAME
+ " WHERE " + SQLdb.DB_PACKAGE + " = '"+ appPackage +"'"
+ " ORDER BY " + SQLdb.DB_TIMEOPEN + " ASC";
Cursor mCursor = dataBase.rawQuery(sqlQ, null);
Log.i("SQL",sqlQ);
if (mCursor.moveToFirst()) {
do {
listDataHeader.add(mCursor.getString(mCursor
.getColumnIndex("header")));
String sqlQ2 = "SELECT " + SQLdb.DB_NAME + ", "
+ SQLdb.DB_PACKAGE + ", "
+ SQLdb.DB_TIMEOPEN + ", "
+ SQLdb.DB_TIMECLOSED + ", "
+ SQLdb.DB_DURATION + ", "
+ "strftime('%d-%m-%Y', "+ SQLdb.DB_TIMEOPEN +") header " +
" FROM " + SQLdb.DICTIONARY_TABLE_NAME
+ " WHERE " + SQLdb.DB_PACKAGE + " = '"+ appPackage +"'"
+ " AND strftime('%d-%m-%Y', "+ SQLdb.DB_TIMEOPEN +") = '"+ mCursor.getString(mCursor
.getColumnIndex("header")) +"'"
+ " ORDER BY " + SQLdb.DB_TIMEOPEN + " ASC";
Cursor mCursor2 = dataBase.rawQuery(sqlQ2, null);
if (mCursor2.moveToFirst()) {
do {
listDataChildInner.add(mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_DURATION)));
listDataChildInnerO.add(mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_TIMEOPEN)));
listDataChildInnerC.add(mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_TIMECLOSED)));
Log.i("ACT CHILDT",mCursor.getString(mCursor
.getColumnIndex("header")) + " -> " + mCursor2.getString(mCursor2
.getColumnIndex(SQLdb.DB_TIMEOPEN)));
} while (mCursor2.moveToNext());
}
listDataChild.put(mCursor.getString(mCursor
.getColumnIndex("header")).toString(), listDataChildInner);
listDataChildO.put(mCursor.getString(mCursor
.getColumnIndex("header")).toString(), listDataChildInnerO);
listDataChildC.put(mCursor.getString(mCursor
.getColumnIndex("header")).toString(), listDataChildInnerC);
} while (mCursor.moveToNext());
}
Related
I'm building an Android app that contain SQLite DB and I was having a problem with translating SQLite saved data then I find a solution which is a method to get resource id of strings, and it worked just fine, the app was working fine then suddenly this error showed I don't know why, please help
The method, I used to get resource for string:
public static int getResId(String resName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
The code in the adapter
textViewname.setText(context.getString(R.string.product_Name)+" " + repo.getName());
textViewquantity.setText(context.getString(R.string.last_added_qty)+" " + repo.getQuantity() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textViewdate.setText(context.getString(R.string.entry_date)+" " + repo.getDate()+ " /"+context.getResources().getString(mdatabase.getResId(repo.getInOrOut(),R.string.class)));
textViewTotal.setText(context.getString(R.string.total_qty)+" " + repo.getTotal() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textviewlastUpdated.setText(context.getString(R.string.last_updated_date)+" " + repo.getUpdateDate() +" /"+context.getResources().getString(mdatabase.getResId(repo.getInOrOut(),R.string.class)));
textViewid.setText(context.getString(R.string.id)+" " + repo.getId());
textViewOutputqty.setText(context.getString(R.string.last_out_qty)+" " + repo.getOutputqty() + " " + context.getResources().getString(mdatabase.getResId(repo.getItemsType(),R.string.class)));
textViewpriceofsingleitem.setText(context.getString(R.string.retail_price)+" " + repo.getPrice() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewpriceofMultipleitem.setText(context.getString(R.string.wholesale_price)+" " + repo.getPriceofmultipleInput() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewProfitSIngle.setText(context.getString(R.string.retail_profit)+" " + repo.getProfitSingle() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewProfitMultiple.setText(context.getString(R.string.wholesale_profit)+" " + repo.getProfitMultiple() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewPurchasingPrice.setText(context.getString(R.string.purchasing_price)+" " + repo.getPurchasingPricePeranItem() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalPurchasingPrice.setText(context.getString(R.string.total_purchase_price)+" " + repo.getTotalPurchasingPrice() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalProfitSingle.setText(context.getString(R.string.total_retail_profit)+" " + repo.getTotalProfitperSingle() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewTotalProfitMultiple.setText(context.getString(R.string.total_wholesale_profit)+" "+ repo.getTotalProfitPerMultiple() + " " + context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewitemsIBox.setText(context.getString(R.string.num_unitsInBox)+" "+ repo.getItemsInBox());
textViewSinglePriceOfiteminBox.setText(context.getString(R.string.retail_price_perUnit)+ " " + repo.getSinglePriceofItemInBox()+" " +context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewMultipriceOfItemInBox.setText(context.getString(R.string.wholesale_price_perUnit)+" "+repo.getMultiplePriceofitemInBox()+" "+context.getResources().getString(mdatabase.getResId(repo.getCurrency(),R.string.class)));
textViewtotalItemsInBoexs.setText(context.getString(R.string.total_units)+" "+ repo.getTotalitemsInboxes() +" "+ context.getResources().getString(R.string.Pieces));
This error happens because you are trying to get resource that does not exist. It is because you store memory address which allocate to your string resource and possible it will change next time. I do not know what is your scenario but why you store it already in your strings it will never lost. I can help you to build another logic only when you tell what are you trying to achieve.
I want to achieve AutoSuggest ComboBox 2 problems I am facing
1st getting an array of id and name both want the name only.
Array i am receiving
2nd losing focus on typing the text.
Please help
database Query
public SentenceList getAutoProductList() {
return new StaticSentence(s, "SELECT "
+ "P.ID, "
+ "P.REFERENCE, "
+ "P.CODE, "
+ "P.CODETYPE, "
+ "P.NAME, "
+ "P.PRICEBUY, "
+ "P.PRICESELL, "
+ "P.CATEGORY, "
+ "P.TAXCAT, "
+ "P.ATTRIBUTESET_ID, "
+ "P.STOCKCOST, "
+ "P.STOCKVOLUME, "
+ "P.IMAGE, "
+ "P.ISCOM, "
+ "P.ISSCALE, "
+ "P.ISKITCHEN, "
+ "P.PRINTKB, "
+ "P.SENDSTATUS, "
+ "P.ISSERVICE, "
+ "P.ATTRIBUTES, "
+ "P.DISPLAY, "
+ "P.ISVPRICE, "
+ "P.ISVERPATRIB, "
+ "P.TEXTTIP, "
+ "P.WARRANTY, "
+ "P.STOCKUNITS "
+ "FROM PRODUCTS P "
+ "ORDER BY NAME", null, ProductInfoExt.getSerializerRead());
}
Auto Complete JcomboBox Code
SentenceList sentProductNames = dlSales.getAutoProductList();
m_JComboProductName.setEditable(true);
ComboBoxValModel m_jProductNameModel = new ComboBoxValModel();
editor = (JTextComponent) m_JComboProductName.getEditor().getEditorComponent();
List productList = null;
try {
productList = sentProductNames.list();
} catch (Exception e) {
}
productList.add(0, null);
m_jProductNameModel = new ComboBoxValModel(productList);
m_JComboProductName.setModel(m_jProductNameModel);
AutoCompleteDecorator.decorate(m_JComboProductName);
System.out.println("product List "+ productList);
if(m_JComboProductName.getItemCount()>0){
m_JComboProductName.setSelectedIndex(0);
}
I am building a web portal through android and a query i am running through JDBC drivers is returning 0 where data should not be zero.
This is the query:
ResultSet set = statement.executeQuery("select it.itcod, it.itnam, it.packn, it.tradp, " +
"sum(nvl(itd.slbox,0) - nvl(itd.srbox,0) - nvl(itd.brbox,0) - nvl(itd.gsbox,0)) as sbox, " +
"sum(nvl(itd.slbbx,0) - nvl(itd.srbbx,0) - nvl(itd.brbbx,0) - nvl(itd.gsbbx,0)) as sbbx, " +
"SUM(NVL(itd.PRBOX,0) - NVL(itd.RPBOX,0) - NVL(itd.TRBOX,0)) as pbox, " +
"SUM(NVL(itd.PRBBX,0) - NVL(itd.RPBBX,0) - NVL(itd.TRBBX,0)) as pbbx " +
"from items it " +
"LEFT join item_daily itd " +
"on (it.cocod = itd.cocod " +
"and it.itcod = itd.itcod " +
"and ITD.ddate between " + fdate + " and " + tdate + ")" +
"WHERE IT.COCOD = " + COCOD +
"AND IT.DCODE = " + DCODE +
"AND NVL(IT.FREZE,'N')!='Y' " +
"group by it.cocod, it.itcod, it.itnam, it.packn, " +
" it.tradp, it.pkqty, it.dcode, it.freze, " +
" it.ishow, it.sltax, it.dcont, it.mcode, " +
" it.nwcod " +
"order by itnam ");
I have tried using resultsetmetadata but that does not work either.
you should be careful to this line
+ fdate + " and " + tdate
because you should use like this:
" .. to_date('"+fdate+"','***your_date_format') and to_date('"+fdate+"','***your_date_format')"
I am busy using a SQLite database with a java application and after updating the database successfully I get the following error
org.sqlite.jdbc4.JDBC4PreparedStatement#4e1c6f
Below is my update code
PreparedStatement update = con.prepareStatement("UPDATE highLeaker SET " + cyc + " = "
+ dataArray[3] + " , "
+ "gasSurveyOperator" + " = "
+ dataArray[1] + " , "
+ "gasSurveySerial" + " = "
+ dataArray[2] + " , "
+ "loss" + " = "
+ dataArray[4] + " , "
+ "comment" + " = "
+ dataArray[5] + " , "
+ "cycle" + " = '"
+ cycleT + "' , "
+ "date" + " = "
+ dataArray[6]
+ " WHERE leakerID = " + dataArray[0]+";");
System.out.println(update);
update.executeUpdate();
dataRow = CSVFile.readLine(); // Read next line of data.
You printed out the object. Also, I would highly recommend using place holders instead when executing SQL queries.
okay i have source like this
public List<SearchRecord> getResult() {
List<SearchRecord> searchResult = new ArrayList<SearchRecord>();
String query = "SELECT " + A + ", " + B+ ", " + C
+ " FROM " + TABLE_A+ " UNION ALL SELECT " + A+ ", "
+ B+ ", " + C+ " FROM " + TABLE_B + " UNION ALL SELECT " + A+ ", " + B+ ", "
+ C+ " FROM " + TABLE_C + " UNION ALL SELECT "
+ A+ ", " + B+ ", " + C+ " FROM "
+ TABLE_D;
Cursor cursor = mDatabase.rawQuery(query, null);
if (cursor != null) {
cursor.moveToFirst();
do {
SearchRecord sr = new SearchRecord();
sr.setRecordA(cursor.getString(0));
sr.setRecordB(cursor.getString(1));
sr.setRecordC(cursor.getString(2));
searchResult.add(sr);
} while (cursor.moveToNext());
return searchResult;
}
return null;
}
with this code i can get some record from multiple table who have same column name, and store the result in a List object. but how to i find out which table is this record belongs ? is it from TABLE_A, TABLE_B, TABLE_C, or the other
As per SQL, no, you are joining 3 sets of rows and operating them the same way, each record cannot be distinguished by its origin.
Of course, you could do something like
String query = "SELECT 'TABLE_A', " + A + ", " + B+ ", " + C
+ " FROM " + TABLE_A+ " UNION ALL SELECT 'TABLE_B' " + A+ ", "
+ B+ ", " + C+ " FROM " + TABLE_B
+ " UNION ALL SELECT 'TABLE_C', " + A+ ", " + B+ ", "
+ C+ " FROM " + TABLE_C + " UNION ALL SELECT 'TABLE_D', "
+ A+ ", " + B+ ", " + C+ " FROM "
+ TABLE_D;
:-)