带有自定义适配器的Android DrawerLayout和Listview

带有自定义适配器的Android DrawerLayout和Listview,第1张

概述我正在创建一个抽屉布局,里面有一个相对布局和一个列表视图(一个用于主要内容,另一个用于导航) 对于listview,我创建了一个自定义适配器,我使用list_item xml文件创建每一行,该文件具有imageview和textview. 该应用程序运行,但当我打开抽屉时,我只看到没有列表视图的背景. 现在,如果我使用ArrayAdapter(默认)尝试它,列表视图就会显示出来. 有什么建议? 我 我正在创建一个抽屉布局,里面有一个相对布局和一个列表视图(一个用于主要内容,另一个用于导航)

@H_403_8@

对于ListvIEw,我创建了一个自定义适配器,我使用List_item xml文件创建每一行,该文件具有imagevIEw和textvIEw.@H_403_8@

该应用程序运行,但当我打开抽屉时,我只看到没有列表视图的背景.
现在,如果我使用ArrayAdapter(默认)尝试它,列表视图就会显示出来.@H_403_8@

有什么建议?@H_403_8@

我的定制适配器@H_403_8@

@H_403_8@

public class CustomAdapter extends ArrayAdapter<CategorIEs>{Context context;int layoutResourceID;CategorIEs[] data = null;public CustomAdapter(Context context,int layoutResourceID,CategorIEs[] categs) {    super(context,layoutResourceID);    this.layoutResourceID = layoutResourceID;    this.context = context;    data = categs;}@OverrIDepublic VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {    VIEw row = convertVIEw;    PHolder holder = null;    if(row == null)    {        LayoutInflater inflater = ((Activity)context).getLayoutInflater();        row = inflater.inflate(layoutResourceID,parent,false);        holder = new PHolder();        holder.imgIcon = (ImageVIEw)row.findVIEwByID(R.ID.categimage);        holder.txtTitle = (TextVIEw)row.findVIEwByID(R.ID.categtext);        row.setTag(holder);    }    else    {        holder = (PHolder)row.getTag();    }    CategorIEs categ = data[position];    holder.txtTitle.setText(categ.Title);    holder.imgIcon.setimageResource(categ.icon);    return row;}static class PHolder{    ImageVIEw imgIcon;    TextVIEw txtTitle;}

}@H_403_8@

在我的主要活动中@H_403_8@

@H_403_8@

mDrawerList = (ListVIEw) findVIEwByID(R.ID.categList);    CategorIEs data[] = new CategorIEs[]    {        new CategorIEs(R.drawable.restaurant,R.string.food),new CategorIEs(R.drawable.bar_coktail,R.string.bar),new CategorIEs(R.drawable.mall,R.string.shop),new CategorIEs(R.drawable.agritourism,R.string.out),new CategorIEs(R.drawable.dance_class,R.string.art),new CategorIEs(R.drawable.officebuilding,R.string.other),new CategorIEs(R.drawable.university,R.string.education),new CategorIEs(R.drawable.townhouse,R.string.house),new CategorIEs(R.drawable.junction,R.string.transport)    };    CustomAdapter ca = new CustomAdapter(this,R.layout.List_item,data);    VIEw header = (VIEw)getLayoutInflater().inflate(R.layout.List_header,null);    mDrawerList.addheaderVIEw(header);    mDrawerList.setAdapter(ca);
解决方法 使用:

@H_403_8@

super(context,layoutResourceID,categs);@H_403_8@

在构造函数中或覆盖getCount()方法:@H_403_8@

@H_403_8@

@OverrIDepublic int getCount() {   return data.length;}
总结

以上是内存溢出为你收集整理的带有自定义适配器的Android DrawerLayout和Listview全部内容,希望文章能够帮你解决带有自定义适配器的Android DrawerLayout和Listview所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1124967.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存