2010-01-17 77 views
0

我想創建一個列表視圖,每個項目旁邊都有複選框。 (我正在遵循android的代碼),但是我無法使它工作,每當我運行它時,它都會崩潰。因爲我對這個android工作人員很新,我不知道該怎麼做才能使它工作,任何幫助表示讚賞。代碼如下。 *我創建了兩個名爲list和list_item的佈局文件。 爲主要活動代碼:用複選框創建ListView ... Android

public class ListDemoActivity extends ListActivity { 
    /** Called when the activity is first created. */ 
    String[] listItems = {"exploring", "android","list", "activities"}; 
    private SimpleCursorAdapter adapter; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.lists); 
     //setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems));   

     //setContentView(R.layout.lists); 
     Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null); 
     startManagingCursor(c); 
     String[] cols = new String[]{People.NAME}; 
     int[] names = new int[]{R.id.row_tv}; 
     adapter = new SimpleCursorAdapter(this,R.layout.list_item,c,cols,names); 
     this.setListAdapter(adapter); 
    } 
} 

列表文件內容:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

      <ListView 
      android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:stackFromBottom="true" 
      android:transcriptMode="normal"/> 
    </LinearLayout> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

      <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Submit Selection"/> 
     </LinearLayout> 
</LinearLayout> 

和LIST_ITEM文件內容:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <CheckBox 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/row_chbox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/row_tv" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 
+0

我無法發佈xml文件:( – deewangan 2010-01-17 19:00:24

+0

您是否在清單中啓用了android.permission.READ_CONTACTS? – RickNotFred 2010-01-17 20:03:41

回答

0

我讀同一本書(Apress出版的Android 2 Pro,源代碼http://www.apress.com/book/downloadfile/4529 - example/Ch4/Listing4-17)

我發現當我複製/粘貼到eclipse項目中時,它沒有工作,原因很簡單,我沒有在粘貼的源文件的頂部設置包名稱。

試穿:

package asdf.adsf;

...(與您的實際包名稱;-),在java文件的頂部,也許這將有所幫助。問題在於它沒有使用項目特定的'R'資源類。