Listview的用法

sky123123 2010-10-29 03:37:45


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent();
// 如何在此方法中启动某个Activity,
我用intent.setClass(XXX.this, YYY.class);
startActivity(intent);
我这样启动的话,总是报:
Your content must have a ListView whose id attribute is 'android.R.id.list'
错误 ,请问大家是什么原因呐?

}
...全文
810 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
weida0906 2013-07-23
  • 打赏
  • 举报
回复
2楼说的是正确的,我刚刚出的问题·google解决的
wangxiaohong123 2012-09-04
  • 打赏
  • 举报
回复
xml错了,第二楼说的是正确的
maggiccrystal_3 2012-02-29
  • 打赏
  • 举报
回复
<ListView android:id="@android:id/list""
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>

ListActivity 不用布局 listView = getListView();
Listening_music 2011-10-24
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 smallhero007 的回复:]

看了楼主的代码终于晓得为什么错了。
你把ListActivity和activity中listView的用法搞混淆了。
第一,就你的代码而言,你继承了ListActivity,那么, 你就应该使getListView()这个方法来获取ListView 对象。
第二,如果你想用布局中,那么你就该用findViewByid来实例化化你的listView对象。

你看你SettingActi……
[/Quote]

++1
wangtiantian23 2011-05-13
  • 打赏
  • 举报
回复
学习贴了!
sky123123 2010-10-29
  • 打赏
  • 举报
回复
是不是


Intent intent = new Intent();
switch (arg2) {
case 1:
intent.setClass(SettingActivity.this, UserSetActivity.class);
listView.getContext().startActivity(intent);
startActivity(intent);
break;
}


这段有问题啊
sky123123 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 dinjay 的回复:]

listView = (ListView) findViewById(R.id.list);
换成
listView = (ListView)getListView();
[/Quote]

不行的啊 还是 报: ERROR/AndroidRuntime(7445): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
dinjay 2010-10-29
  • 打赏
  • 举报
回复

listView = (ListView) findViewById(R.id.list);
换成
listView = (ListView)getListView();
sky123123 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 dinjay 的回复:]
<ListView android:id="@android:id/list""
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>

这样不行?
[/Quote]

嗯,是啊 这样写的话,怎么样才能得到这ListView对象呢? 我得不到哦、。、
dinjay 2010-10-29
  • 打赏
  • 举报
回复
<ListView android:id="@android:id/list""
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>

这样不行?
sky123123 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 smallhero007 的回复:]
ListVeiw listView = (ListView) findViewById(R.id.list); 我写咯之后 会报错,找不到 R.id.list;
你写了这,但是你没有指定你的R.id.list中的list是那个布局中的呀?你有setContentView吗?
你看看你代码中继承了ListActivity ,但是有用getListView()来实例化listView?

建议……
[/Quote]


public class SettingActivity extends Activity{

private ListView listView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.list);
System.out.println(listView);
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.setting,new String[]{"opera_img","opera_name"},new int[]{R.id.menuImageView,R.id.operationName});
listView.setAdapter(adapter);
listView.setOnItemClickListener(listener);
}

private OnItemClickListener listener = new OnItemClickListener() {

public void onItemClick(AdapterView<?> a, View v, int arg2,
long arg3) {
System.out.println(a.getId() + " " + v.getId() + " " + arg2 + " " + arg3);
Intent intent = new Intent();
switch (arg2) {
case 1:
intent.setClass(SettingActivity.this, UserSetActivity.class);
listView.getContext().startActivity(intent);
startActivity(intent);
break;
}
}
};


private List<Map<String, Object>> getData() {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("opera_img", R.drawable.user_set);
map.put("opera_name", "用户设置");
list.add(map);

return list;
}
}



这这样写咯,可是还 是报 同样的错误:Your content must have a ListView whose id attribute is 'android.R.id.list'

ListView的xml文件时:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical">
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
SmallHero007 2010-10-29
  • 打赏
  • 举报
回复
ListVeiw listView = (ListView) findViewById(R.id.list); 我写咯之后 会报错,找不到 R.id.list;
你写了这,但是你没有指定你的R.id.list中的list是那个布局中的呀?你有setContentView吗?
你看看你代码中继承了ListActivity ,但是有用getListView()来实例化listView?

建议将ListActivity换成Activity,然后再和setContentView,在findViewByID实例化,再绑定数据,再设置监听事件。

sky123123 2010-10-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 smallhero007 的回复:]
看了楼主的代码终于晓得为什么错了。
你把ListActivity和activity中listView的用法搞混淆了。
第一,就你的代码而言,你继承了ListActivity,那么, 你就应该使getListView()这个方法来获取ListView 对象。
第二,如果你想用布局中,那么你就该用findViewByid来实例化化你的listView对象。

你看你SettingActiv……
[/Quote]

ListVeiw listView = (ListView) findViewById(R.id.list); 我写咯之后 会报错,找不到 R.id.list;

我的目的是就想自定义个ListView, 自定义的ListView的xml文件也好咯,但是我就是不知道 怎么样给ListView 初始话,所以 在我点击事件 启动 新的Activity时会报错的。。;


在使用ListView的时候 貌似得两个XML文件的哦 可是我不知道怎么用滴。。。

能不能指点一二啊 嘿嘿。。
SmallHero007 2010-10-29
  • 打赏
  • 举报
回复
看了楼主的代码终于晓得为什么错了。
你把ListActivity和activity中listView的用法搞混淆了。
第一,就你的代码而言,你继承了ListActivity,那么, 你就应该使getListView()这个方法来获取ListView 对象。
第二,如果你想用布局中,那么你就该用findViewByid来实例化化你的listView对象。

你看你SettingActivity 的代码中,从前到尾没有出现过ListView,如果连最基础的View都没有,你如何给他设置Adapter?又如何给他添加监听事件呢?

至于该怎么用?楼主你可以去apidemo中看,里面有10多个listView的例子。
sky123123 2010-10-29
  • 打赏
  • 举报
回复
一下是listView的xml文件 ,

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

<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="5sp"
/>

<TextView android:id="@+id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#FFFFFFFF"
android:gravity="center_vertical"
/>
</ListView>
</LinearLayout>
[Quote=引用 1 楼 dinjay 的回复:]
看一下你listview在xml中是怎么定义的,贴出来:
一定要这样:

Java code

<ListView
android:id="@android:id/list"//这里必须是android:id/list
android:layout_width="fill_parent" //自定义
android:layout_height="wr……
[/Quote]

sky123123 2010-10-29
  • 打赏
  • 举报
回复

public class SettingActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.setting,new String[]{"opera_img","opera_name"},new int[]{R.id.menuImageView,R.id.operationName});
setListAdapter(adapter);
}

private List<Map<String, Object>> getData() {
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String, Object>();
map.put("opera_img", R.drawable.user_set);
map.put("opera_name", "用户设置");
list.add(map);
return list;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent();
switch (position) {
case 0:
intent.setClass(SettingActivity.this, UserSetActivity.class);
startActivity(intent);
break;
}
}
}



以上就是我的那个activity啊 ,不知错误在哪里奥
SmallHero007 2010-10-29
  • 打赏
  • 举报
回复
这样应该不是你onListItem中的问题吧?
你的activity继承了ListActivity,那么里面就一定要实例化一个listview的,楼主检查下代码,或者将整个Activity的代码发上来也行。
dinjay 2010-10-29
  • 打赏
  • 举报
回复
看一下你listview在xml中是怎么定义的,贴出来:
一定要这样:

<ListView
android:id="@android:id/list"//这里必须是android:id/list
android:layout_width="fill_parent" //自定义
android:layout_height="wrap_content"//自定义
>
</ListView>

80,351

社区成员

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

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