使用hashmap从数据库中获取listview数据不起作用

时间:2013-08-26 11:00:42

标签: android android-listview android-sqlite

我尝试了一个带有listview的android项目。我使用hashmap在DB中添加了值,并在listview中显示结果。但我无法正确启动..请帮我解决这个问题......

这是我的java代码

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);
        }
    }


}

我使用插入查询添加了数据。所以我想在列表视图中显示结果。 这是我的DB类

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;

    }

}

我找不到错误的确切位置。所以

现在我更新了我的logcat输出:

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)

这是我的xml文件

<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>

我使用eclipse IDE

2 个答案:

答案 0 :(得分:0)

从文档引用

ListActivity具有默认布局,该布局由屏幕中央的单个全屏列表组成。但是,如果需要,可以通过在onCreate()中使用setContentView()设置自己的视图布局来自定义屏幕布局。为此,您自己的视图必须包含一个ID为“@android:id / list”的ListView对象(如果它在代码中则列出)。

因此,请确保您在xml的列表视图中有以下内容

  <ListView android:id="@android:id/list"

答案 1 :(得分:0)

从代码中删除l=(ListView) findViewById(R.id.list);行,因为无论何时使用ListActivity,Android都会将列表视图设为@android:id/list,并删除l=getListView();