求大神们帮帮忙,listview没办法正常显示,求解决

[复制链接]
查看11 | 回复7 | 2021-1-27 06:36:13 | 显示全部楼层 |阅读模式
这是listview的适配器代码:
publicclassMyListViewAdapterextendsBaseAdapter{
//填充数据的list
privateArrayListfoodlist;
//用来控制CheckBox的选中状况
privatestaticHashMap[I]isSelected;
//上下文
privateContextcontext;
//用来导入布局
privateLayoutInflaterinflater=null;
//构造器
publicMyListViewAdapter(ArrayListlist,Contextcontext){
this.context=context;
this.foodlist=list;
inflater=LayoutInflater.from(context);
isSelected=newHashMap[I]();
//初始化数据
initDate();
}
//初始化isSelected的数据
privatevoidinitDate(){
for(inti=0;ifoods=newArrayList();
privateMyListViewAdapteradapter;
privateCheckBoxcheckBox;

protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
initView();//初始化控件
Viewview=LayoutInflater.from(this).inflate(R.layout.item,null);
checkBox=(CheckBox)view.findViewById(R.id.check_box);
initData();//初始化虚拟数据
adapter=newMyListViewAdapter(foods,getApplicationContext());
listView.setAdapter(adapter);
}
publicvoidinitView(){
listView=(ListView)findViewById(R.id.drink_list);//listview列表控件
ok=(Button)findViewById(R.id.btn_commit);//确定按钮
ok.setOnClickListener(this);
listView.setOnItemClickListener(this);
}
/*
*初始化虚拟数据
**/
publicvoidinitData(){
Classcls=R.drawable.class;//反射
try{
foods.add(newFood(cls.getDeclaredField("d1").getInt(null),"猕猴桃汁",
"10"));
}catch(Exceptione){e.printStackTrace();}
}
/*按钮点击事件处理*/
@Override
publicvoidonClick(Viewv){
intmID=v.getId();
switch(mID){
caseR.id.btn_commit:
myPrice();//计算总价并输出
break;
}
}
/*
*计算总价格的方法
**/
publicvoidmyPrice(){
HashMap[I]map=MyListViewAdapter.getIsSelected();
Stringstr="";
intmoney=0;
for(inti=0;iparent,Viewview,intposition,longid){
MyListViewAdapter.ViewHolderholder=(MyListViewAdapter.ViewHolder)view.getTag();
MyListViewAdapter.getIsSelected().put(position,holder.cb.isChecked());
}
}
item的布局代码



[I]




main的布局代码


[B]

结果却是这样



分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
listivew
android:layout_width="match_parent"
android:layout_height="match_parent"
item
android:layout_width="match_parent"
android:layout_height="wrap_content"
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层



回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
打印下initData()数据是否为空
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
引用3楼jklwan的回复:打印下initData()数据是否为空不输出数据,但是代码看不出有啥错误呀
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
引用2楼键盘舞者113的回复:这段有错误吗
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
楼主,不要纠结自己的这段代码,去网上下载示例程序,找能跑的起来的,然后看看别人是怎么写的,再看看自己的代码跟他们有哪里是不一样的,再调试一下就出来了.
回复

使用道具 举报

千问 | 2021-1-27 06:36:13 | 显示全部楼层
出现问题第一条:看日志
第二条:debug
先debug你的foods有没有数据再说吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行