I'm new to android and I'm stuck in a problem.
This is my main class and simply inserting the data and showing the data in Logcat. I want to show the data in ListView in a new activity. Now I am not getting how to get data from cursor and show it on ListView . I have made a view.xml
& view.java
to show data but I don't know how to show data on that activity. Should I make an adapter class, cursoradapter or flater, etc. (I want to know the next steps).
Thanks in advance :)
Main Class
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
database_delete();
database_add();
}
public void database_add() {
try{
Database_handler db = new Database_handler(this);
Log.d("Insert: ", "Inserting ..");
db.addData(new get_set(131,"A-Lucy")) ;
db.addData(new get_set(132,"A-kk")) ;
db.addData(new get_set(133,"A_Tweety")) ;
db.addData(new get_set(134,"A-Naruto")) ;
Log.d("Reading: ", "Reading all contacts..");
List<get_set> getSetList = db.getalldata();
for(get_set set : getSetList){
String log = "CODEE: " + set.getCode() + ", Description: " + set.getDescr();
Log.d("CODE: ", log);
}
here is some code of my database handler class having functionality of inserting,retrieving data.
Database handler class
public class Database_handler extends SQLiteOpenHelper {
private static final int DATABASE_Version = 1;
private static final String DATABASE_Name = "android_db";
private static final String TABLE_Name = "all_data";
private static final String Column_Name_code = "code";
private static final String Column_name_desc = "description";
private SQLiteDatabase db;
Database_handler(Context cont){
super(cont,DATABASE_Name,null,DATABASE_Version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_ANDRO = " CREATE TABLE " + TABLE_Name + "(" + Column_Name_code + " INTEGER PRIMARY KEY , "
+ Column_name_desc + " TEXT )";
db.execSQL(CREATE_TABLE_ANDRO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int Older_version, int new_version) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name );
onCreate(db);
}
// Inserting record
public void addData (get_set getSet){
SQLiteDatabase db = this.getReadableDatabase();
ContentValues cv = new ContentValues();
cv.put (Column_Name_code, getSet.getCode());
cv.put(Column_name_desc, getSet.getDescr());
db.insert(TABLE_Name,null,cv);
db.close();
}
Retrieving data
public List<get_set> getalldata(){
List<get_set> data_list = new ArrayList<get_set>();
String select_query = "SELECT * FROM " + TABLE_Name;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.rawQuery(select_query,null);
if (cur.moveToFirst()){
do {
get_set getsetObj = new get_set();
getsetObj.setCode(Integer.parseInt(cur.getString(0)));
getsetObj.setDescr(cur.getString(1));
data_list.add(getsetObj);
} while (cur.moveToNext());
}
return data_list;
}
In the database_add() function, after insertion of data into the database, Call an intent to the next Activity. In that new activity, call getalldata() function. AS you are already getting returned by a list, use Array adapter List view, with String type of an ArrayList, by following the below tutorial. It will work like a charm.
http://www.vogella.com/articles/AndroidListView/article.html
And also, I'd suggest you to include db.getalldata() function inside AsyncTask, because you dont want heavy database transactions in the main thread.