Android ListView填充数据的方法

所属分类: 软件编程 / Android 阅读数: 61
收藏 0 赞 0 分享

Android ListView填充数据的方法

因为多人开发,为了是自己开发的模块方便融合到主框架中,同时也为了减小apk的大小,要求尽可能少的使用xml的布局文件,开发中需要在ListView中显示数据,网上查到的几乎所有的示例,都是通过xml文件来为ListView的Item提供布局样式,甚是不方便。

能不能将自己通过代码创建的布局(如View,LinearLayout)等动态的布局到ListView呢?当然可以。

为了给ListView提供数据,我们需要为其设置一个适配,我们可以从BaseAdapter继承,然后重写它的getView方法,这个方法中有一个参数convertView,我们可以将它设置为我们自定义的视图并返回,来实现加载用代码定义好的布局。

定义一个LinearLayout布局,它是继承自View的,所以可以通过getView返回(注意:不要为这个布局使用 LinearLayout.LayoutParams 参数,因为ListView不识别,他识别的是AbsListView LayoutParams

代码如下:

public class PriceBoard extends LinearLayout { 
  private ListView listView; 
  private List items; 
  private LinearLayout.LayoutParams params; 
  public PriceBoard(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    items = new ArrayList(); 
    this.setOrientation(HORIZONTAL); 
    params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
    listView = new ListView(context); 
    listView.setLayoutParams(params); 
    PriceBoardAdapter priceBoardAdapter = new PriceBoardAdapter(context); 
    listView.setAdapter(priceBoardAdapter); 
    addView(listView, params); 
  } 
  public void add(PriceData data){ 
    PriceBoardItem item = new PriceBoardItem(this.getContext(),null); 
    item.setItem(data); 
    items.add(item); 
    params.setMargins(10,0,0,2); 
//    item.setLayoutParams(params); 
  } 
  public PriceBoardItem getItemView(int index){ 
    return (PriceBoardItem)items.get(index); 
  } 
  private class PriceBoardItem extends LinearLayout{ 
    private TextView nameView; 
    private TextView enCodeView; 
    private TextView priceView; 
    private PriceData priceData; 
    public PriceBoardItem(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      this.setOrientation(HORIZONTAL); 
      nameView = new TextView(context); 
      nameView.setTextSize(TypedValue.COMPLEX_UNIT_PX,38); 
      enCodeView = new TextView(context); 
      enCodeView.setTextSize(TypedValue.COMPLEX_UNIT_PX,28); 
      priceView = new TextView(context); 
      priceView.setTextSize(TypedValue.COMPLEX_UNIT_PX,48); 
      priceView.setGravity(Gravity.CENTER); 
      setLayout(); 
    } 
    public TextView getNameView(){ 
      return nameView; 
    } 
    public TextView getEnCodeView(){ 
      return enCodeView; 
    } 
    public TextView getPriceView(){ 
      return priceView; 
    } 
    public PriceData getPriceData(){ 
      return priceData; 
    } 
    private void setLayout(){ 
      LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1); 
      LinearLayout linearLayout = new LinearLayout(getContext()); 
      linearLayout.setOrientation(VERTICAL); 
      linearLayout.addView(nameView,p); 
      linearLayout.addView(enCodeView,p); 
      addView(linearLayout, p); 
 
      p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,1); 
      linearLayout = new LinearLayout(getContext()); 
      linearLayout.setOrientation(VERTICAL); 
      linearLayout.addView(priceView,p); 
      addView(linearLayout, p); 
    } 
    public void setItem(PriceData data){ 
      priceData = data; 
    } 
  } 
  private class PriceBoardAdapter extends BaseAdapter{ 
    private Context _context; 
    public PriceBoardAdapter(Context context){ 
      _context = context; 
    } 
    public int getCount(){ 
      return items.size(); 
    } 
    public Object getItem(int position) { 
      return position; 
    } 
 
    @Override 
    public long getItemId(int position) { 
      return position; 
    } 
 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      PriceBoardItem view = getItemView(position); 
      PriceData data = ((PriceBoardItem) items.get(position)).getPriceData(); 
      view.getNameView().setText(data.getName()); 
      view.getEnCodeView().setText(data.getEnCode()); 
      view.getPriceView().setText(String.valueOf(data.getPrice())); 
      convertView = view; 
      return convertView; 
    } 
  } 

调用:

PriceBoard priceBoard = new PriceBoard(context,null); 
priceData = new PriceData(); 
priceData.setName("现货白银"); 
priceData.setEnCode("Ag"); 
priceData.setPrice(4006); 
priceBoard.add(priceData); 
priceData = new PriceData(); 
priceData.setName("现货铜"); 
priceData.setEnCode("Cu"); 
priceData.setPrice(43895); 
priceBoard.add(priceData); 
priceData = new PriceData(); 
priceData.setName("现货镍"); 
priceData.setEnCode("Ni"); 
priceData.setPrice(43895); 
priceBoard.add(priceData); 
addView(priceBoard); 

效果:

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多