android ListView is not a view

本文记录了一次使用Android ListView结合SimpleAdapter显示数据时遇到的问题,并详细解释了如何从XML布局文件中替换ListView为TextView来正确展示数据的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在学习android的ListView时,在XML布局文件里布局列表时,用的是ListView,代码中用SimpleAdapter绑定数据,然后运行时出现错误,错误提示为:ListView  is  not  a  view。在网上搜索这个错误信息,但是没有搜索到,然后我又搜索了simpleadapter的用法,此时我才发现原来是我的XML出错了,应该用TextView而不是ListView。。

 

<TextView
     android:id="@+id/view1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />
 <TextView
     android:id="@+id/view2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

 

(以前的错误代码

<ListView
     android:id="@+id/view1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />
 <ListView
     android:id="@+id/view2"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     />

 

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  PrepareDate();
  SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.list_view,new String[]{"姓名","性别"},new int[]{R.id.view1,R.id.view2});
  listview=new ListView(this);
  listview.setAdapter(adapter);
  setContentView(listview);
 }

 

private void PrepareDate(){
  data=new ArrayList<Map<String,Object>>();
  Map<String,Object> item;
  item=new HashMap<String,Object>();
  item.put("姓名", "张三");
  item.put("性别","男");
  data.add(item);
  item=new HashMap<String,Object>();
  item.put("姓名", "张三");
  item.put("性别","男");
  data.add(item);
  item=new HashMap<String,Object>();
  item.put("姓名", "张三");
  item.put("性别","男");
  data.add(item);
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值