【问题标题】:Creating ListView with check boxes...Android使用复选框创建 ListView...Android
【发布时间】:2010-01-17 18:39:18
【问题描述】:

我正在尝试创建一个在每个项目旁边都有复选框的列表视图。 (我正在关注android书中的代码),但我无法让它工作,每次我运行它时,它都会崩溃。因为我对这个 android 员工很陌生,所以我不知道该怎么做才能让它工作,感谢任何帮助。代码如下。 *我创建了两个名为 list 和 list_item 的布局文件。 主要活动的代码:

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>

【问题讨论】:

  • 我无法发布 xml 文件 :(
  • 您是否在清单中启用了 android.permission.READ_CONTACTS?

标签: android listview checkbox


【解决方案1】:

我正在阅读同一本书(Apress Android 2 Pro,源代码位于 http://www.apress.com/book/downloadfile/4529 - 示例 /Ch4/Listing4-17)

我发现当我将它复制/粘贴到一个eclipse项目中时,它不起作用,原因很简单,我没有在粘贴的源文件顶部设置包名。

试试看:

打包 asdf.adsf;

...(使用您的实际包名 ;-),在 java 文件的顶部,也许这会有所帮助。问题是它没有使用项目特定的“R”资源类。

【讨论】:

    猜你喜欢
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2016-02-03
    • 2015-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多