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?
The problem is you're trying to fill your
TextView
s with result from the same row, which has 2 columns only. If what you really want is to display 3 first names inTextView
s, the easiest way would be to create array of yourTextView
s and assign to it's membersgetString
(1) in the loop. Something like this: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
andCursorAdapter
.