-2

我尝试了一个带有 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);
        }
    }


}

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

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

4

2 回答 2

0

从文档中引用

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

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

  <ListView android:id="@android:id/list"
于 2013-08-26T11:34:31.963 回答
0

从您的代码中删除l=(ListView) findViewById(R.id.list);行,因为每当您使用时ListActivity,android 都会获取 listview whoose id is@android:id/list和 remove l=getListView();

于 2013-08-26T12:20:36.747 回答