当前位置: 移动技术网 > 移动技术>移动开发>Android > android listview的多列模版实例代码

android listview的多列模版实例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

android listview多列模版

在listview中,可以做出多列模版的效果,关键还是在listview的模版本,比如如下:

<linearlayout  
  android:id="@+id/relativelayout1"  
  android:layout_height="fill_parent"  
  android:layout_width="fill_parent" 
  xmlns:android="http://schemas.android.com/apk/res/android"> 
 
  <textview 
    android:id="@+id/firsttext" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="first" 
    android:layout_weight="1"> 
  </textview> 
   
  <textview 
    android:id="@+id/secondtext" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="second" 
    android:layout_weight="2"> 
  </textview> 
   
  <textview 
    android:id="@+id/thirdtext" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="third" 
    android:layout_weight="1"> 
  </textview> 
   
  <textview 
    android:id="@+id/fourthtext" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:text="fourth" 
    android:layout_weight="1"> 
  </textview> 
</linearlayout> 

listviewadapter.java:

public class listviewadapter extends baseadapter 
{ 
  public arraylist<hashmap<string,string>> list; 
  activity activity; 
   
  public listviewadapter(activity activity, arraylist<hashmap<string,string>> list) { 
    super(); 
    this.activity = activity; 
    this.list = list; 
  } 
 
   
  public int getcount() { 
    // todo auto-generated method stub 
    return list.size(); 
  } 
 
   
  public object getitem(int position) { 
    // todo auto-generated method stub 
    return list.get(position); 
  } 
 
 
  public long getitemid(int position) { 
    // todo auto-generated method stub 
    return 0; 
  } 
 
  private class viewholder { 
      textview txtfirst; 
      textview txtsecond; 
      textview txtthird; 
      textview txtfourth; 
   } 
 
    
 
  public view getview(int position, view convertview, viewgroup parent) { 
    // todo auto-generated method stub 
     
    // todo auto-generated method stub 
        viewholder holder; 
        layoutinflater inflater = activity.getlayoutinflater(); 
 
        if (convertview == null) 
        { 
          convertview = inflater.inflate(r.layout.listview_row, null); 
          holder = new viewholder(); 
          holder.txtfirst = (textview) convertview.findviewbyid(r.id.firsttext); 
          holder.txtsecond = (textview) convertview.findviewbyid(r.id.secondtext); 
          holder.txtthird = (textview) convertview.findviewbyid(r.id.thirdtext); 
          holder.txtfourth = (textview) convertview.findviewbyid(r.id.fourthtext); 
          convertview.settag(holder); 
        } 
        else 
        { 
          holder = (viewholder) convertview.gettag(); 
        } 
 
        hashmap<string, string> map = list.get(position); 
        holder.txtfirst.settext(map.get(first_column)); 
        holder.txtsecond.settext(map.get(second_column)); 
        holder.txtthird.settext(map.get(third_column)); 
        holder.txtfourth.settext(map.get(fourth_column)); 
 
      return convertview; 
  } 

主程序: 

public class multicolumnactivity extends activity  
{ 
  private arraylist<hashmap<string,string>> list; 
   
  public void oncreate(bundle savedinstancestate)  
  { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
 
    listview lview = (listview) findviewbyid(r.id.listview); 
    populatelist(); 
    listviewadapter adapter = new listviewadapter(this, list); 
    lview.setadapter(adapter); 
  }   
 
  private void populatelist() { 
     
    list = new arraylist<hashmap<string,string>>(); 
     
    hashmap<string,string> temp = new hashmap<string,string>(); 
      temp.put(first_column,"colored notebooks"); 
      temp.put(second_column, "by navneet"); 
      temp.put(third_column, "rs. 200"); 
      temp.put(fourth_column, "per unit"); 
    list.add(temp); 
     
    hashmap<string,string> temp1 = new hashmap<string,string>(); 
      temp1.put(first_column,"diaries"); 
      temp1.put(second_column, "by amee products"); 
      temp1.put(third_column, "rs. 400"); 
      temp1.put(fourth_column, "per unit"); 
    list.add(temp1); 
     
    hashmap<string,string> temp2 = new hashmap<string,string>(); 
      temp2.put(first_column,"note books and stationery"); 
      temp2.put(second_column, "by national products"); 
      temp2.put(third_column, "rs. 600"); 
      temp2.put(fourth_column, "per unit"); 
    list.add(temp2); 
     
    hashmap<string,string> temp3 = new hashmap<string,string>(); 
      temp3.put(first_column,"corporate diaries"); 
      temp3.put(second_column, "by devarsh prakashan"); 
      temp3.put(third_column, "rs. 800"); 
      temp3.put(fourth_column, "per unit"); 
    list.add(temp3); 
     
    hashmap<string,string> temp4 = new hashmap<string,string>(); 
      temp4.put(first_column,"writing pad"); 
      temp4.put(second_column, "by technotalaktive pvt. ltd."); 
      temp4.put(third_column, "rs. 100"); 
      temp4.put(fourth_column, "per unit"); 
    list.add(temp4); 
  } 
} 

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网