欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android listview的多列模版实例代码

时间:2021-01-25 10:28:12|栏目:Android代码|点击:

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); 
  } 
} 

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

上一篇:android全屏去掉title栏的多种实现方法

栏    目:Android代码

下一篇:Android自定义图片轮播Banner控件使用解析

本文标题:android listview的多列模版实例代码

本文地址:http://www.codeinn.net/misctech/50871.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有