listview的ID为什么必须是"@+id/android:list"

slmax1 2011-05-12 06:28:33
而不能是"@+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);
}
}
...全文
1733 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
月出惊弓鸟 2013-11-25
  • 打赏
  • 举报
回复
引用 8 楼 Wuhuixia8511191 的回复:
我想请问一下楼主 那个 ListView extend ListActivity 是自动生成的 还是 手写的啊?如果是自动生成的 应该怎么弄啊? 我怎么新建 都是 默认 继承 Activity!!!(菜鸟 望 勿喷)
手写
Wuhuixia8511191 2013-11-07
  • 打赏
  • 举报
回复
我想请问一下楼主 那个 ListView extend ListActivity 是自动生成的 还是 手写的啊?如果是自动生成的 应该怎么弄啊? 我怎么新建 都是 默认 继承 Activity!!!(菜鸟 望 勿喷)
prince58 2011-05-12
  • 打赏
  • 举报
回复
更详细信息看:

Android之ListActivity:布局与数据绑定
http://blog.csdn.net/prince58/archive/2011/04/25/6362284.aspx
slmax1 2011-05-12
  • 打赏
  • 举报
回复
听说我结贴了?
slmax1 2011-05-12
  • 打赏
  • 举报
回复
怪不得我换其它ID时,都查不了.
prince58 2011-05-12
  • 打赏
  • 举报
回复
ListActivity默认就绑定了这个listview ID(而且只能有这一个listview).

想用其他的ID 你可以直接用Activity。
slmax1 2011-05-12
  • 打赏
  • 举报
回复
好,我即刻试试.
lizhengjun2010 2011-05-12
  • 打赏
  • 举报
回复
可以是“@+id/list1234"
但程序里就需要改动啦

默认的 setListAdapter(adapter)
它是使用系统默认的id 必须是"@+id/android:list"

当自定义id时,程序里就不能直接 setListAdapter(adapter);
改动:
程序里:
ListView list=(ListView)findViewById(R.id.list1234);
...
....
list.setListAdapter(adapter)

xfchenlei 2011-05-12
  • 打赏
  • 举报
回复
你把数字去掉,试试!用带有数字的id在R.java中生成就会有问题

80,469

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧