RSS

Code to load data from database to listview in Android

11 Aug

private void loadListData()
{
ListView mylist = (ListView)findViewById(R.id.listView);
//mylist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,mStrings));
String[] from = new String[] {“rowid”, “col_1”, “col_2”, “col_3″};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
File sdcard = Environment.getExternalStorageDirectory();
File objFile = new File(sdcard+”/nikhil.db”);
Boolean isMyDatabase= objFile.exists();
if(isMyDatabase.equals(true))
{
SQLiteDatabase db = openOrCreateDatabase(sdcard+”/nikhil.db”, MODE_PRIVATE, null);
Cursor objCursor = db.rawQuery(“SELECT * FROM SentMail”, null);
objCursor.moveToFirst();
String firstItem ;
String SecondItem ;
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
while(!objCursor.isAfterLast())
{
HashMap<String, String> map = new HashMap<String, String>();
//map.put(“rowid”, “” + objCursor.getCount());
map.put(“col_1”, objCursor.getString(1));
map.put(“col_2”, “Subject:”+objCursor.getString(2));
map.put(“col_3”, “Today”);
fillMaps.add(map);
objCursor.moveToNext();
}
objCursor.close();
db.close();
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.demo_message, from, to);
mylist.setAdapter(adapter);
}
else
{
setContentView(R.layout.message);
}
}

Advertisements
 
Leave a comment

Posted by on August 11, 2011 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: