stanga bogdan stanga bogdan - 1 month ago 4
Android Question

Populating a listview from a SQLite database

I have an activity where i write a name which is insert in the database. In another activity I want to put a ListView which is populated with those names which are in the database or to add the new item directly when i write it in the edittext from the first activity.
I realized the insert into database part but i can't figure out how to populate that listview.

public class Add extends Activity implements OnClickListener{

Button sqlUpdate;
EditText sqlName;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
sqlUpdate = (Button) findViewById(R.id.bFinishAdd);
//sqlView = (Button) findViewById(R.id.bSQLopenView);
sqlName = (EditText) findViewById(R.id.etAdd);
sqlUpdate.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean didItWork = true;

try{
String name = sqlName.getText().toString();

DBhelp entry = new DBhelp(Add.this);
entry.open();
entry.createEntry(name);
entry.close();
Intent i = new Intent(Add.this, Liste.class);
startActivity(i);

}catch(Exception i){
didItWork = false;
}finally{
}
}


});

}


public void onClick(View arg0) {
// TODO Auto-generated method stub

}


}

Here is the database helper:

public class DBhelp {

public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";



private static final String DATABASE_NAME = "DBhelpdb";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;


private static class DbHelper extends SQLiteOpenHelper{

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL);"


);
}

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}

}

public DBhelp(Context c){
ourContext = c;
}
public DBhelp open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public void createEntry(String name) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);

ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";

int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + "\n";
}

return result;
}
public String getName(long l) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c!=null){
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}

public void updateEntry(long lRow, String mName) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mName);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow, null);
}
public void deteleEntry(long lRow1) {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);}
}


And here is the activity where I want to implement the listview with that items :

public class Liste extends Activity{

Button btnAdd;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
btnAdd = (Button) findViewById(R.id.bAdd);
btnAdd.setOnClickListener(new OnClickListener(){

public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent("com.project.mapshop.ADD");
startActivity(i);
}

});
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
DBhelp info = new DBhelp(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);

}}


I would be very grateful if you could explain me how to do it or give me a link with an advice or tutorial which could help me. Thank you very much!

Answer

Please follow some simple tutorials first, some good tutorials are:

1- this one.

2- Simple Example

3- ListView of Data from SQLiteDatabase

Comments