Poison Sniper
Poison Sniper

Reputation: 148

Android how to print sqlite data inside normal string array?

hello every one i face this problem and iam too weak in DB i wrote this code to fill the database and now i want to copy the data inside an array to print it out inside arrayadapter coz iam using tabactivity and this is the best solution i found

public void insertIntoTable(){
        try{
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
            mydb.execSQL("INSERT INTO " + TABLE + "(ID, NAME) VALUES("+Phno.getText()+",'"+PName.getText()+"')"); 

            mydb.close();
        }catch(Exception e){
            Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG);
        }
    }

and this is how i want to print it out

 mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null);
    String[] values = new String[] { "Iwant to put the cursor data here" };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
    }

Upvotes: 2

Views: 2009

Answers (1)

vnshetty
vnshetty

Reputation: 20132

I hope this will work for you

ArrayList<String> values = new ArrayList<String>();
mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null);
allrows.moveFirst()
while(allrows.moveToNext()){ 
      String name= allrows.getString(allrows.getColumnIndex("NAME"));
      values.add(name); 
}
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

Upvotes: 3

Related Questions