-2

I tried a android project with listview.I added the values in DB by using hashmap and display the result in a listview. But i couldn't started correctly..pls help me solve this...

Here is my 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 the data by using insert query. so that i want to display the result in a listview. Here is my 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 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)");
    }
    public void onUpgrade(SQLiteDatabase db,int version_old,int version_new){
        db.execSQL("DROP TABLE IT EXISTS country");
        onCreate(db);
    }
    public ArrayList<HashMap<String, String>> getinfo(){
        ArrayList<HashMap<String, 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, 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 the error exactly where it is come from.So

Now i updated my 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 to start activity ComponentInfo{com.example.subitemlistviewdb/com.example.subitemlistviewdb.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.os.Looper.loop(Looper.java:137)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at java.lang.reflect.Method.invoke(Method.java:511)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at dalvik.system.NativeStart.main(Native Method)
08-26 11:30:34.351: E/AndroidRuntime(2138): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.Activity.setContentView(Activity.java:1881)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at com.example.subitemlistviewdb.MainActivity.onCreate(MainActivity.java:22)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.Activity.performCreate(Activity.java:5104)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-26 11:30:34.351: E/AndroidRuntime(2138):     at 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 with uncaught exception (group=0x40a71930)

Here is my 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

2 Answers2

0

Quoting from docs

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or list if it's in code).

So make sure you have the below for your listview in xml

  <ListView android:id="@android:id/list"
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Remove l=(ListView) findViewById(R.id.list); line from your code because whenever you are using ListActivity then android takes listview whoose id is @android:id/list and also remove l=getListView();.

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36