Listview From Sqlite In Android
As I searched whole net still in problem with ListView From Sqlite. After searching so much i am trying my project on android hive example Link here. So in this in Database Handler
Solution 1:
Try to bind the data into the listview as below:
List<Contact> contact = newArrayList<Contact>();
contact=getAllContacts();
ArrayAdapteradapter=newArrayAdapter(this, android.R.layout.simple_list_item_1, contact);
listContent.setAdapter(adapter);
Solution 2:
Below is a code using which i suppose you can meet your requirements. In the below code i would fetch contacts saved in my database and display it in a listView
. If the user wants to delete a contact from the database, then he shall long press on the item, and using the dialog that appears, he can delete the contact. Below is the code:
publicclassviewContactsextendsListActivity {
privatestatic final StringTAG = "MYRECORDER";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);
setContentView(R.layout.showcontacts);
//Creating a List ViewArrayList<String> listItems = newArrayList<String>();
ArrayAdapter<String> adapter = newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, listItems);
ListView mylist=(ListView) findViewById(android.R.id.list);
mylist.setAdapter(adapter);
//Creating or opening an eisting databaseSQLiteDatabase db=openOrCreateDatabase("MYDB", Context.MODE_PRIVATE, null);
//Getting a cursor to fetch data from the databaseCursor c=db.rawQuery("SELECT Number,Name FROM myTbl", null);
Log.d(TAG, "Cursor reference obtained...");
c.moveToFirst();
Log.d(TAG, "Cursor Moved to First Number....");
if(c!=null){
//If there are contents in the database, then c!=null, so using do-while loop access data // in databasedo{
String num=c.getString(c.getColumnIndex("Number"));
String name=c.getString(c.getColumnIndex("Name"));
StringName_num=name+" : "+num;
listItems.add(Name_num);
c.moveToNext();
}while(!c.isAfterLast());
//update the list
adapter.notifyDataSetChanged();
//closing the database after use
db.close();
//Below is the code to delete items in data base
mylist.setOnItemClickListener(newOnItemClickListener() {
String str=null;
publicvoidonItemClick(AdapterView<?> arg0, View view,
int arg2, long arg3) {
// TODO Auto-generated method stubString item = ((TextView)view).getText().toString();
str=item.substring(item.lastIndexOf('+'));
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
//Creating an Alert DialogAlertDialog .Builder builder=newAlertDialog.Builder(viewContacts.this);
builder.setMessage("Are you sure you want to delete the contact "+str+" ?");
builder.setCancelable(false);
builder.setPositiveButton("Yes", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stubSQLiteDatabase db=openOrCreateDatabase("MYDB", MODE_PRIVATE, null);
Toast.makeText(getBaseContext(), "The contact: "+str+" was successfully deleted", Toast.LENGTH_LONG).show();
String table="myTbl";
String whereClause = "Number = ?";
String[] whereArgs = newString[] { str };
db.delete(table, whereClause, whereArgs);
db.close();
}
});
builder.setNegativeButton("No", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
} );
AlertDialog alert=builder.create();
alert.show();
}
});
}
}
}
Solution 3:
Just use SimpleCursorAdapter
in this case: http://www.java2s.com/Code/Android/UI/UsingSimpleCursorAdapter.htm
Solution 4:
I solve that adding the function toString to my class object.
In your case, add that function to the class contact
publicStringtoString(){
return name;
}
Post a Comment for "Listview From Sqlite In Android"