user2746864 user2746864 - 12 days ago 6
Android Question

How to display database content on android Listview?

I am working on a contact book like application.I have a database and,I want to display the data in ListView. Can anybody tell me how to achieve this? Below is what I have tried so far,

public class Contacts {

private int id;
private String name;
private String phone_number;
private String email;


public Contacts()
{

}

public Contacts(int id, String name, String phone_number, String email)
{
this.id = id;
this.name = name;
this.phone_number = phone_number;
this.email = email;
}

public Contacts(String name, String phone_number, String email)
{
this.name = name;
this.phone_number = phone_number;
this.email = email;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}

public void setEmail(String email) {
this.email = email;
}

public String getEmail() {
return email;
}

}

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "ContactsManager";
private static final String TABLE_CONTACTS = "contacts";

private static final String KEY_ID = "id";

private static final String KEY_NAME = "name";

private static final String KEY_PH_NO = "phone_number";

private static final String KEY_EMAIL = "email";


public DatabaseHandler(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

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("

+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"

+ KEY_PH_NO + " TEXT," + KEY_EMAIL + " TEXT" + ")";

db.execSQL(CREATE_CONTACTS_TABLE);

}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

onCreate(db);

}


public void insertContact(Contacts contacts)
{

SQLiteDatabase db = this.getWritableDatabase();



ContentValues cv = new ContentValues();

cv.put(KEY_NAME, contacts.getName()); // Contact Name

cv.put(KEY_PH_NO, contacts.getPhone_number()); // Contact Phone

cv.put(KEY_EMAIL, contacts.getEmail()); //Contact Email



// Inserting Row

db.insert(TABLE_CONTACTS, null, cv);

db.close();

}


// Getting All Contacts

public List<Contacts> getAllContacts()
{

List<Contacts> contactList = new ArrayList<Contacts>();

// Select All Query

String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;



SQLiteDatabase db = this.getWritableDatabase();

Cursor cursor = db.rawQuery(selectQuery, null);




// looping through all rows and adding to list

if (cursor.moveToFirst())
{

do {

Contacts contact = new Contacts();

//contact.setId(Integer.parseInt(cursor.getString(0)));

contact.setName(cursor.getString(1));

contact.setPhone_number(cursor.getString(2));

contact.setEmail(cursor.getString(3));

// Adding contact to list

contactList.add(contact);

}
while (cursor.moveToNext());

}



// return contact list


return contactList;
}
}

public class View_data extends ListActivity {

DatabaseHandler db = new DatabaseHandler(this);
Cursor c ;

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



db.getWritableDatabase();

List<Contacts> values = db.getAllContacts();

ArrayAdapter<Contacts> adapter = new ArrayAdapter<Contacts>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_data, menu);
return true;
}

}


In
View_data
class i tried to display the data but couldn't do it.
Any Suggestion will be highly appreciated.

Answer

try this:

you can use

http://dj-android.blogspot.in/2012/10/android-show-data-from-sqlite-db-into.html

and get solution.. In this you have to just use ListView instead of GridView.