vendredi 9 janvier 2015

How to retrieve data from database and display it on TextView in android eclipse


i'm making simple application that store and retrieve data in database, i can store a data but i'am having a problem in retrieving on it, i want to retrieve the data and display it on TextView:


Here is my MySQLiteHelper.java



public class MySQLiteHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "test";
private static final String DATABASE_TABLE = "test_table";
private static final String CREATE_TABLE = "CREATE TABLE " + DATABASE_TABLE + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)";

public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
this.onCreate(db);
}

//Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_AGE = "age";

//This will store data and this is OK
public void addNameAndAge(ReturningClass sample) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, sample.getName());
values.put(KEY_AGE, sample.getAge());
db.insert(DATABASE_TABLE, null, values);
db.close();
}

//This will retrieve data, and this is my problem
public String[] getNameAndAge(){
//I want to check first here if table has a value
//if the table is empty then return 'No Data Yet' value
//else then retrieve the data, but i don't know how to do that
String selectQuery = "SELECT * FROM " + DATABASE_TABLE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
String[] data = null;
if (cursor.moveToFirst()) {
do {
// I don't know how i going to retrieve data here
} while (cursor.moveToNext());
}
db.close();
return data;
}

}

}


Here is my ReturningClass.java model:



public class ReturningClass {

private int id;
private String nameThis;
private String ageThis;

public ReturningClass(){}

public ReturningClass(String name, String age) {
super();
this.nameThis = name;
this.ageThis = age;
}

public String getName() {
return nameThis;
}
public String getAge() {
return ageThis;
}
public int getId() {
return id;
}
}


and here is my MainActivity.java :



public class MainActivity extends Activity implements View.OnClickListener {

public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_activity);

db = new MySQLiteHelper(this);//DATABASE

TextView nameTextView = (TextView) findViewById(R.id.nameId);
TextView ageTextView = (TextView) findViewById(R.id.ageId);

String[] dataGet = db.getNameAndAge();
if(dataGet == "No Data Yet"){
//Some code here...
}else{
nameTextView.setText(dataGet[1]);
ageTextView.setText(dataGet[2]);
}


} }


I am very new in android developing so any helped will be appreciated. Thank you!





Aucun commentaire:

Enregistrer un commentaire