2013-08-26 44 views
-2

我嘗試了一個與listview.I的android項目通過使用hashmap在數據庫中添加值,並顯示在列表視圖中的結果。但我不能啓動correctly..pls幫我解決這個...使用hashmap從數據庫中的列表視圖數據不工作

這裏是我的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; 

    } 

} 

我找不到錯誤它的確切位置來from.So

現在我更新了我的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

+0

重複.....使用搜索!!!!!!!!!!! – Selvin

回答

0

從文檔引用

ListActivity的默認佈局由屏幕中央的單個全屏列表組成。但是,如果您願意,可以通過在onCreate()中使用setContentView()設置您自己的視圖佈局來自定義屏幕布局。要做到這一點,你自己的視圖必須包含一個ID爲「@android:id/list」的ListView對象(或者如果它是在代碼中的話)。

所以請確保您有以下爲您的列表視圖中的XML

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

我改變了我的列表視圖@android:id/list。但它不起作用 –

+0

@PrabuPalanisamy發佈更新的logcat – Raghunandan

0

因爲每當你使用ListActivity從您的代碼刪除l=(ListView) findViewById(R.id.list);行,然後機器人需要的ListView whoose ID是@android:id/list也刪除l=getListView();

+0

實際上生成的java文件無法創建。我不知道爲什麼?也許是一個問題? –

+0

您正在使用eclipse或android studio的IDE? –

相關問題