0

i'm populating a list view to view my record, i've the following code...

            super.onCreate(savedInstanceState);
            setContentView(R.layout.total);
            ArrayList<Object> results = new ArrayList<Object>();
            // -- SQLiteOpenHelper dbHelper = new DatabaseHelper(this, SAMPLE_DB_NAME, null, 1);

            SQLiteDatabase myDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, SQLiteDatabase.OPEN_READONLY, null);
            try { 
                 /* Create the Database (no Errors if it already exists) */
                           myDB.execSQL("PRAGMA foreign_keys = ON;");

                            // -- openOrCreateDatabase(name, mode, factory)
                            // myDB = dbHelper.getReadableDatabase();
                    Cursor c = myDB.query(DatabaseHelper.SAMPLE_TABLE_NAME, null, null, null, null, null, null);
                    Cursor d = myDB.query(DatabaseHelper.SAMPLE_TABLE_NAMES, null, null, null, null, null, null);

                    /* Check if our result was valid. */ 
                if (c != null && d != null) {

                    c.moveToFirst(); // it's very important to do this action otherwise your Cursor object did not get work
                    d.moveToFirst();
                    char cust_name = (char) c.getColumnIndex("cust_name");
                    char pro_name = (char) d.getColumnIndex("pro_name");
                    int pro_price = (int) d.getColumnIndex("pro_price");

                     /* Check if at least one Result was returned. */ 
                     if (c.isFirst() && d.isFirst()) { 
                          int i = 0; 
                          /* Loop through all Results */ 
                          do { 
                               i++; 
                               String cust_nameColumnIndex = c.getString(cust_name);
                               String pro_nameColumnIndex = c.getString(pro_name);
                               int pro_priceColumnIndex = c.getInt(pro_price);


                               /* Add current Entry to results. */ 
                               results.add("" + i + ": " + cust_name + " (" + pro_name + ": " + pro_price + ")"); 
                          } while (c.moveToNext()&& d.moveToNext());

                     } 
                }

            } catch (SQLiteException e) { 
            } finally { 
                 if (myDB != null) 
                      myDB.close(); 
            }

            // -- android.R.layout.simple_list_item_1 is object which belong to ListActivity itself
            // -- you only need to add list object in your main layout file
            this.setListAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, results)); 
        }

total.xml

 <ListView
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="380dp"
            android:cacheColorHint="#00000000" >
        </ListView>

the data is successfully inserted to sqlite (confirmed from adb shell)...it gives me garbage value...can any one please figure out the issue....Thanks in advance

enter image description here

Numair
  • 1,062
  • 1
  • 20
  • 41

2 Answers2

3

That is not garbage values references(memory) addresses, use below code it will work.

 do { 
                               i++; 
                               String cust_nameColumnIndex = c.getString(cust_name);
                               String pro_nameColumnIndex = c.getString(pro_name);
                               int pro_priceColumnIndex = c.getInt(pro_price);


                               /* Add current Entry to results. */ 
                               results.add("" + i + ": " + cust_nameColumnIndex + " (" + pro_nameColumnIndex + ": " + pro_priceColumnIndex + ")"); 
                          } while (c.moveToNext()&& d.moveToNext());



this.setListAdapter(new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, (String[]) results.toArray(new String[0])));
Bhaskar Reddy
  • 480
  • 5
  • 13
  • can you explain it, please? I'm curious to know why that code works – Finuka May 02 '12 at 08:41
  • observe the result.add() what I send – Bhaskar Reddy May 02 '12 at 08:51
  • @BhaskarReddy thanks for your response.. actually the data are from two tables.. it is showing me the first(cust_name) now..instead of garbage...so this worked well...but now giving 0 in 3rd column... there should be value of pro_price..which are not 0 – Numair May 02 '12 at 10:13
  • @BhaskarReddy i change the ans according to my case from ... pro_nameColumnIndex = c.getString.... and pro_priceColumnIndex = c.getInt... to **d.getString**... as all three values are from different cursors...but will accept your ans for helping me.. :) – Numair May 02 '12 at 11:33
0

Try changing the way you read your cursors. Something like that might be better:

//Get the indexes
        int cust_name =  cursor.getColumnIndex("cust_name");
        int pro_name =  cursor.getColumnIndex("pro_name");
        int pro_price =  cursor.getColumnIndex("pro_price");

        try {
            if(cursor.moveToFirst()){
                while (!cursor.isAfterLast()) {

                    //Get the data
                    String cust_nameColumnIndex = cursor.getString(cust_name);
                    String pro_nameColumnIndex = cursor.getString(pro_name);
                    int pro_priceColumnIndex = cursor.getInt(pro_price);

                    //Move to next cursor item
                    cursor.moveToNext();
                }
            }
            else {
                Log.i(TAG, "Empty cursor");
                //Do whatever
            }
        } catch (Exception e) {
            Log.i(TAG, "Exception while reading cursor: " + e.getMessage());
            //Do whatever
        }
        finally {
            cursor.close();
        }    
Ika
  • 1,608
  • 14
  • 15
  • data is from multiple table..i.e. cust_name is from tbl_cust and pro_name/price is from tbl_prod... – Numair May 02 '12 at 08:43