android listview的多列模版实例代码
androidlistview多列模版
在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:
publicclasslistviewAdapterextendsBaseAdapter
{
publicArrayList<HashMap<String,String>>list;
Activityactivity;
publiclistviewAdapter(Activityactivity,ArrayList<HashMap<String,String>>list){
super();
this.activity=activity;
this.list=list;
}
publicintgetCount(){
//TODOAuto-generatedmethodstub
returnlist.size();
}
publicObjectgetItem(intposition){
//TODOAuto-generatedmethodstub
returnlist.get(position);
}
publiclonggetItemId(intposition){
//TODOAuto-generatedmethodstub
return0;
}
privateclassViewHolder{
TextViewtxtFirst;
TextViewtxtSecond;
TextViewtxtThird;
TextViewtxtFourth;
}
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
//TODOAuto-generatedmethodstub
//TODOAuto-generatedmethodstub
ViewHolderholder;
LayoutInflaterinflater=activity.getLayoutInflater();
if(convertView==null)
{
convertView=inflater.inflate(R.layout.listview_row,null);
holder=newViewHolder();
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));
returnconvertView;
}
主程序:
publicclassMultiColumnActivityextendsActivity
{
privateArrayList<HashMap<String,String>>list;
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListViewlview=(ListView)findViewById(R.id.listview);
populateList();
listviewAdapteradapter=newlistviewAdapter(this,list);
lview.setAdapter(adapter);
}
privatevoidpopulateList(){
list=newArrayList<HashMap<String,String>>();
HashMap<String,String>temp=newHashMap<String,String>();
temp.put(FIRST_COLUMN,"ColoredNotebooks");
temp.put(SECOND_COLUMN,"ByNavNeet");
temp.put(THIRD_COLUMN,"Rs.200");
temp.put(FOURTH_COLUMN,"PerUnit");
list.add(temp);
HashMap<String,String>temp1=newHashMap<String,String>();
temp1.put(FIRST_COLUMN,"Diaries");
temp1.put(SECOND_COLUMN,"ByAmeeProducts");
temp1.put(THIRD_COLUMN,"Rs.400");
temp1.put(FOURTH_COLUMN,"PerUnit");
list.add(temp1);
HashMap<String,String>temp2=newHashMap<String,String>();
temp2.put(FIRST_COLUMN,"NoteBooksandStationery");
temp2.put(SECOND_COLUMN,"ByNationalProducts");
temp2.put(THIRD_COLUMN,"Rs.600");
temp2.put(FOURTH_COLUMN,"PerUnit");
list.add(temp2);
HashMap<String,String>temp3=newHashMap<String,String>();
temp3.put(FIRST_COLUMN,"CorporateDiaries");
temp3.put(SECOND_COLUMN,"ByDevarshPrakashan");
temp3.put(THIRD_COLUMN,"Rs.800");
temp3.put(FOURTH_COLUMN,"PerUnit");
list.add(temp3);
HashMap<String,String>temp4=newHashMap<String,String>();
temp4.put(FIRST_COLUMN,"WritingPad");
temp4.put(SECOND_COLUMN,"ByTechnoTalaktivePvt.Ltd.");
temp4.put(THIRD_COLUMN,"Rs.100");
temp4.put(FOURTH_COLUMN,"PerUnit");
list.add(temp4);
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!