0

I'm using SQLiteStudio(v2.1.5) for fetching the data inside my database (dbName) and i have 1 table (tbl_Names) and put in android. Now In my database I have a two column id and name. In my database (dbName) I have input three name consist of (Abbygail ,Andie, and Alysa etc..). I can only display it in TextView.

This is my WHOLE CODE:

String DB_PATH="";
SQLiteDatabase db;
File dbfile;
TextView txtBabyName1, txtBabyName2, txtBabyName3;
SQLiteDatabase writedb;

@Override         protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
              }

@Override
protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (android.os.Build.VERSION.SDK_INT >= 17) {
                DB_PATH = getApplicationContext().getApplicationInfo().dataDir + "/databases/";
        } else {
                DB_PATH = "/data/data/" + getApplicationContext().getPackageName() + "/databases/";
        }
        File dbpath = new File(DB_PATH);
        if (!dbpath.exists()) {
                dbpath.mkdirs();
        }
        dbfile = new File(DB_PATH+"dbpath");
        if (!dbfile.exists()) {
                try {
                        InputStream is = getApplicationContext().getAssets().open("dbName");
                        int size = is.available();
                        byte[] buffer = new byte[size];
                        is.read(buffer);
                        is.close();
                        FileOutputStream fos = new FileOutputStream(dbfile);
                        fos.write(buffer);
                        fos.flush();
                        fos.close();
                } catch (Exception e) { throw new RuntimeException(e);};
        }
        db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
        txtBabyName1 = (TextView) findViewById(R.id.txtBabyName);
        txtBabyName2 = (TextView) findViewById(R.id.txtMeaning);
        txtBabyName3 = (TextView) findViewById(R.id.txtMeaningO);
        Cursor cursor = db.rawQuery("Select * FROM tbl_Names", null);
        TextView[] tvs = new TextView[]{txtBabyName1, txtBabyName2, txtBabyName3};
        int i=0;
        if (cursor.moveToFirst()) {
                do {
                        tvs[i].setText(cursor.getString(1));
                } while ((++i<3)&&(cursor.moveToNext()));
        } else {
                txtBabyName.setText("No data.");
        }
}

QUESTION:
how can i display all my the data using ListView in android?

marcolz
  • 2,880
  • 2
  • 23
  • 28
code_gear
  • 3
  • 2
  • show your table: tbl_Names – Mohammed Ali Nov 15 '14 at 09:12
  • `I have a two column id and name.` - If you have **2** columns, then you can only access column **0** and column **1**. Colum 2 simply **doesn't exist**. And also column 3. – Phantômaxx Nov 15 '14 at 10:33
  • i just noted that while ago . now the problem is when i chance my string(1) to all 1 it only display one name that is Alysa. I don't know how display the three name. – code_gear Nov 15 '14 at 14:39

2 Answers2

0

cursor is returned as null.. check your query. Also add a null check to the cursor object before using it.

Aun
  • 1,883
  • 1
  • 17
  • 26
0

The problem is you're trying to fill your TextViews with result from the same row, which has 2 columns only. If what you really want is to display 3 first names in TextViews, the easiest way would be to create array of your TextViews and assign to it's members getString(1) in the loop. Something like this:

 TextView[] tvs = new TextView[]{txtBabyName1, txtBabyName2, txtBabyName3}
 int i=0;
 if (cursor.moveToFirst()){
        do{
            tvs[i].setText(cursor.getString(1));
          } 
        while ((++i<3)&&(cursor.moveToNext())); 
  }

But I suspect you have something different in mind, and if you want to display all of your available names, you should look into ListView and CursorAdapter.

Maxim Paliy
  • 376
  • 4
  • 7
  • thanks for the sample code it just solve my problem. But you mention that to display all of the variable names i should use ListView but i dont know how to use it. I'm just new at android programming specially in database. – code_gear Nov 16 '14 at 06:20
  • can you give a sample code like you did above, thanks – code_gear Nov 16 '14 at 08:16