android - listview data from database by using hashmap does not working -
this question has answer here:
i tried android project listview.i added values in db using hashmap , display result in listview. couldn't started correctly..pls me solve this...
here java code
package com.example.subitemlistviewdb; import java.util.arraylist; import java.util.hashmap; import android.r; import android.os.bundle; import android.app.activity; import android.app.listactivity; import android.view.menu; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; public class mainactivity extends listactivity { dbcontroller dbc=new dbcontroller(this); listview l; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_item); l=(listview) findviewbyid(r.id.list); arraylist<hashmap<string, string>> countrylist=dbc.getinfo(); if(countrylist.size()!=0) { l=getlistview(); listadapter adapter=new simpleadapter(mainactivity.this, countrylist, r.layout.list_content, new string [] {"id","cname","capital"}, new int [] {r.id.title,r.id.text1,r.id.text2}); setlistadapter(adapter); } } }
i added data using insert query. want display result in listview. here db class
package com.example.subitemlistviewdb; 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 country (id,'cname','capital') values (1,'india', delhi)"); db.execsql("insert country (id,'cname','capital') values (2,'china', beijing)"); db.execsql("insert country (id,'cname','capital') values (3,'england', london)"); db.execsql("insert country (id,'cname','capital') values (4,'usa', washington)"); db.execsql("insert country (id,'cname','capital') values (5,'sri lanka', columbo)"); db.execsql("insert country (id,'cname','capital') values (6,'afghanisthan', kaabul)"); } public void onupgrade(sqlitedatabase db,int version_old,int version_new){ db.execsql("drop table exists country"); oncreate(db); } public arraylist<hashmap<string, string>> getinfo(){ arraylist<hashmap<string, string>> clist; clist=new arraylist<hashmap<string,string>>(); string query="select * country"; sqlitedatabase db=this.getwritabledatabase(); cursor c=db.rawquery(query,null); if(c.movetofirst()) { { hashmap<string, string> map=new hashmap<string, 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; } }
i couldn't find error come from.so
now updated logcat output:
08-26 11:30:34.351: e/androidruntime(2138): fatal exception: main 08-26 11:30:34.351: e/androidruntime(2138): java.lang.runtimeexception: unable start activity componentinfo{com.example.subitemlistviewdb/com.example.subitemlistviewdb.mainactivity}: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list' 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activitythread.access$600(activitythread.java:141) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 08-26 11:30:34.351: e/androidruntime(2138): @ android.os.handler.dispatchmessage(handler.java:99) 08-26 11:30:34.351: e/androidruntime(2138): @ android.os.looper.loop(looper.java:137) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activitythread.main(activitythread.java:5041) 08-26 11:30:34.351: e/androidruntime(2138): @ java.lang.reflect.method.invokenative(native method) 08-26 11:30:34.351: e/androidruntime(2138): @ java.lang.reflect.method.invoke(method.java:511) 08-26 11:30:34.351: e/androidruntime(2138): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-26 11:30:34.351: e/androidruntime(2138): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 08-26 11:30:34.351: e/androidruntime(2138): @ dalvik.system.nativestart.main(native method) 08-26 11:30:34.351: e/androidruntime(2138): caused by: java.lang.runtimeexception: content must have listview id attribute 'android.r.id.list' 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.listactivity.oncontentchanged(listactivity.java:243) 08-26 11:30:34.351: e/androidruntime(2138): @ com.android.internal.policy.impl.phonewindow.setcontentview(phonewindow.java:273) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activity.setcontentview(activity.java:1881) 08-26 11:30:34.351: e/androidruntime(2138): @ com.example.subitemlistviewdb.mainactivity.oncreate(mainactivity.java:22) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activity.performcreate(activity.java:5104) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 08-26 11:30:34.351: e/androidruntime(2138): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 08-26 11:30:34.351: e/androidruntime(2138): ... 11 more 08-26 11:35:34.872: i/process(2138): sending signal. pid: 2138 sig: 9 08-26 11:40:04.811: e/trace(2219): error opening trace file: no such file or directory (2) 08-26 11:40:05.691: d/null(2219): created 08-26 11:40:06.791: d/androidruntime(2219): shutting down vm 08-26 11:40:06.791: w/dalvikvm(2219): threadid=1: thread exiting uncaught exception (group=0x40a71930)
here xml file
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <listview android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"> </listview> </relativelayout>
i use eclipse ide
quoting docs
listactivity has default layout consists of single, full-screen list in center of screen. however, if desire, can customize screen layout setting own view layout setcontentview() in oncreate(). this, own view must contain listview object id "@android:id/list" (or list if it's in code).
so make sure have below listview in xml
<listview android:id="@android:id/list"
Comments
Post a Comment