dimanche 31 mai 2015

Android SQLite Display all records in Listview

I have been on this all weekend and Im at my wits end as to how to return all records in a query into a listview

the following code works perfectly

public void DisplayContact(String sitename)
{
final DBAdapter db = new DBAdapter(this);
db.open();  

Cursor c = db.getAsset6(sitename);
int mx=0;
String[] lvdata = new String[c.getCount()-1];
while (c.moveToNext()) {
lvdata[mx]=c.getString(2) + "-" + c.getString(1) + "-" + c.getString(6);
mx++;

}
getActionBar().setTitle("Number of Records = " + mx); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        R.layout.rowlayout, R.id.label, lvdata);
setListAdapter(adapter);

}

public Cursor getAsset6(String strname) throws SQLException 
{ 
    Cursor mCursor =
            db.query(true, DATABASE_TABLE,
                     new String[] {KEY_ROWID,KEY_FAULTREF,KEY_WORKNUM,KEY_ORIGNUM,KEY_REPBY,KEY_REPDATTIM,KEY_DESCRIPTION},
                     KEY_REPBY + " LIKE ?",
                     new String[] { "%" + strname + "%" },
                     null, null, null, null); if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;                           
}

The problem is it doesnt add the first item in the query (ie there are 151 records in the query but it only puts 150 in the listview missing the first entry)

I am sure its something to do with c.moveToFirst(); and a Do Statement but i have read every article I can find on here and whatever combination I try it crashes when I run the query in the app. I am running the app using an external database so I cant see whats causing the crash in eclipse

Can one of you SQL gurus help me out its driving me crazy

Your help appreciated

Mark

Aucun commentaire:

Enregistrer un commentaire