1

我需要扩展 ListActivity 并填充自定义布局,所以我执行以下操作

public class BizList extends ListActivity {

String bizNames[]={"one", "two","theree","four"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bizlist);

    //ListView listviewMe = (ListView)findViewById(android.R.id.list);
    //ListView listview = this.getListView();

    setListAdapter(new ArrayAdapter<String>(this, android.R.id.list, bizNames));

/*  ListView list1 = (ListView) findViewById(R.id.listView1);
        list1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,startDates_str));;*/

}

我的布局中的 ListView 是

  <ListView
        android:id="@id/android:list"//***have also tried*** android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageView2"
        android:layout_centerHorizontal="true" >

    </ListView>

但我不断收到此错误

android.content.res.resources$notfoundexception: 文件表单xml类型布局资源ID

非常感谢任何和所有帮助。我希望这很简单。谢谢你。

4

2 回答 2

0

如果你想把它添加到你必须写的资源中
android:id="@+id/listView" 你忘记了“+”

    <ListView
                android:id="@+id/listView"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/imageView2"
                android:layout_centerHorizontal="true" >

   </ListView>

在你的 onCreate 方法中

ListView lv = (ListView) findViewById(R.id.listView);

ArrayAdapter<String> myadpater = new ArrayAdapter<String>(
                        this, android.R.layout.simple_list_item_1, bizNames);
lv.setAdapter(myadpater);

希望它现在有效。如果正确请投票

于 2013-01-18T21:24:44.683 回答
0

首先ListActivity要自动查找列表,资源 id 必须是...

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

其次,当您创建 时ArrayAdapter,不要将ListView自身的 id 传递给构造函数。相反,您应该传递列表项布局的资源 ID。

试试这个...

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, bizNames));
于 2013-01-18T21:31:46.373 回答