Hi friends, I have a project which is diaplay the data from database in a
listview. it works dine. But I also want display an image with the
data.how can i solve this...
Here is my java class:
package com.example.subitemdb;
import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.ListActivity;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
DBController d=new DBController(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<hashmap><string,>> countrylist=d.getinfo();
if (countrylist.size()!=0)
{
ListAdapter adapter=new SimpleAdapter(MainActivity.this,countrylist,
R.layout.list_item, new String [] {"cname", "capital"}, new int [] {R.id.text2,
R.id.text3});
setListAdapter(adapter);
}
}
}
Here is my DB class:
package com.example.subitemdb;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBController extends SQLiteOpenHelper{
private static final String LOGCAT="null";
public DBController(Context con)
{
super(con,"sqlite.db",null,1);
Log.d(LOGCAT, "CREATED");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE country (id INTEGER PRIMARY KEY AUTOINCREMENT, cname TEXT, capital TEXT)");
db.execSQL("INSERT INTO country (id,'cname','capital') Values (1,'INDIA', 'Delhi')");
db.execSQL("INSERT INTO country (id,'cname','capital') Values (2,'CHINA', 'Beijing')");
db.execSQL("INSERT INTO country (id,'cname','capital') Values (3,'ENGLAND', 'London')");
db.execSQL("INSERT INTO country (id,'cname','capital') Values (4,'USA', 'Washington')");
db.execSQL("INSERT INTO country (id,'cname','capital') Values (5,'SRI LANKA', 'Columbo')");
db.execSQL("INSERT INTO country (id,'cname','capital') Values (6,'AFGHANISTHAN', 'Kaabul')");
}
@Override
public void onUpgrade(SQLiteDatabase db,int version_old,int version_new) {
db.execSQL("DROP TABLE IT EXISTS country");
onCreate(db);
}
public ArrayList<HashMap><string,>> getinfo() {
ArrayList<hashmap><String,>> clist;
clist = new ArrayList<HashMap><String,string>>();
String query = "SELECT * FROM country";
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(query,null);
if (c.moveToFirst())
{
do
{
HashMap<String,> map=new HashMap<String,>();
map.put("id", c.getString(0));
map.put("cname", c.getString(1));
map.put("capital", c.getString(2));
clist.add(map);
} while(c.moveToNext());
}
return clist;
}
}
Very informative post.I am really impressed by this incredible precious site.
ReplyDelete