listview的ID为啥必须是"@+id/android:list"
listview的ID为什么必须是"@+id/android:list"
而不能是"@+id/list1234"
一改就出错.
布局代码
------解决方案--------------------
可以是“@+id/list1234"
但程序里就需要改动啦
默认的 setListAdapter(adapter)
它是使用系统默认的id 必须是"@+id/android:list"
当自定义id时,程序里就不能直接 setListAdapter(adapter);
改动:
程序里:
ListView list=(ListView)findViewById(R.id.list1234);
...
....
list.setListAdapter(adapter)
------解决方案--------------------
ListActivity默认就绑定了这个listview ID(而且只能有这一个listview).
想用其他的ID 你可以直接用Activity。
而不能是"@+id/list1234"
一改就出错.
布局代码
<?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="fill_parent"
>
<LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="fill_parent">
<ListView android:layout_height="wrap_content" android:id="@+id/list1234" android:layout_width="fill_parent"></ListView>
</LinearLayout>
</LinearLayout>
package com.ListView;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class ListView extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*生成动态数组*/
ArrayList<HashMap<String, String>> list1 = new ArrayList<HashMap<String,String>>();
/*生成map*/
HashMap<String, String> map1 = new HashMap<String, String>();
HashMap<String, String> map2 = new HashMap<String, String>();
HashMap<String, String> map3 = new HashMap<String, String>();
/*往map里放值*/
map1.put("username", "123");
map1.put("userip", "10.143.107.58");
map2.put("username", "234");
map2.put("userip", "10.143.105.1");
map3.put("username", "345");
map3.put("userip", "10.143.106.131");
/*添加map至list数组里*/
list1.add(map1);
list1.add(map2);
list1.add(map3);
/*创建适配器*/
SimpleAdapter listadapter =new SimpleAdapter(this, list1, R.layout.user, new String[]{"username","userip"}, new int[]{R.id.textView2,R.id.textView1});
/*设置适配器*/
setListAdapter(listadapter);
}
}
------解决方案--------------------
可以是“@+id/list1234"
但程序里就需要改动啦
默认的 setListAdapter(adapter)
它是使用系统默认的id 必须是"@+id/android:list"
当自定义id时,程序里就不能直接 setListAdapter(adapter);
改动:
程序里:
ListView list=(ListView)findViewById(R.id.list1234);
...
....
list.setListAdapter(adapter)
------解决方案--------------------
ListActivity默认就绑定了这个listview ID(而且只能有这一个listview).
想用其他的ID 你可以直接用Activity。