标签:
编写listView最重要的就是编写适配器Adapter,
adapter要完成以下几个工作
1,决定使用listview的子view
2,使用给listView显示数据的list
3,重写getView方法,为子view的控件设置内容
另外还要准备编写子view以及显示数据的类,比如listview显示的是水果的信息,那就编写一个水果类,图片名字等,定义setter getter
在Activity中初始化几个水果的类,并都装入到一个list中,然后使用adapter即可
代码如下:
我要编写的是一个用来显示奖项的listview,每个子view里面有奖项名,导演名以及这个导演的作品个数
首先编写好这个item view 定义四个textView
show_bonus_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/bonus_category_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/director_name"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="作品数:"/>
<TextView
android:id="@+id/bonus_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
然后编写三个textview显示名字的类,第三个textview因为是固定显示"作品数"的所以不用再另外处理
public class ShowBonus {
private String bonusCatrgory;
private String directorName;
private int moviesCount;
public String getBonusCatrgory() {
return bonusCatrgory;
}
public void setBonusCatrgory(String bonusCatrgory) {
this.bonusCatrgory = bonusCatrgory;
}
public String getDirectorName() {
return directorName;
}
public void setDirectorName(String directorName) {
this.directorName = directorName;
}
public int getMoviesCount() {
return moviesCount;
}
public void setMoviesCount(int moviesCount) {
this.moviesCount = moviesCount;
}
}
再编写一个adapter,重写getview方法
public class ShowBonusAdapter extends ArrayAdapter<ShowBonus>{
private int resourceID;
public ShowBonusAdapter(Context context, int resource, int textViewResourceId, List<ShowBonus> objects) {
super(context, resource, textViewResourceId, objects);
resourceID = textViewResourceId ;
}
//重写getView方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//获取当前的ShowBonus实例
ShowBonus showBonus = getItem(position);
//为子view的各个控件设置内容
View view = LayoutInflater.from(getContext()).inflate(resourceID,null);
TextView bonusCatrgory = (TextView) view.findViewById(R.id.bonus_category_text);
bonusCatrgory.setText(showBonus.getBonusCatrgory());
TextView directorName = (TextView) view.findViewById(R.id.director_name);
directorName.setText(showBonus.getDirectorName());
TextView bonusCount = (TextView) view.findViewById(R.id.bonus_count);
bonusCount.setText(""+showBonus.getMoviesCount());
return view;
}
}
然后就是在Activity里面初始化几个ShowBonus实例,将他们都装入到一个list中,再使用适配器即可,因为我用的是fragment,与Activity写法基本一致,只是findviewbyid的时候多用了一个view.findViewById
public class FragmentShowBonus extends Fragment {
private Context context;
private List<ShowBonus> listShowBonus = new ArrayList<ShowBonus>();
private ListView showBonusListView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstancestate) {
View view = inflater.inflate(R.layout.show_bonus_layout, container, false);
initBonus();
ShowBonusAdapter showBonusAdapter = new ShowBonusAdapter
(this.getActivity(), R.layout.show_bous_item, listShowBonus);
showBonusListView = (ListView) view.findViewById(R.id.bonus_listview);
showBonusListView.setAdapter(showBonusAdapter);
return view;
}
//实例化几个ShowBonus对象
private void initBonus() {
ShowBonus showBonus1 = new ShowBonus("1,最佳微电影导演奖", "李安", 20);
ShowBonus showBonus2 = new ShowBonus("2,最佳栏目剧导演奖", "张艺谋", 16);
ShowBonus showBonus3 = new ShowBonus("3,最佳纪录片导演奖", "贾樟柯", 19);
ShowBonus showBonus4 = new ShowBonus("4,最佳儿童剧导演奖", "冯小刚", 13);
ShowBonus showBonus5 = new ShowBonus("5,最佳电视剧导演奖", "陈凯歌", 23);
ShowBonus showBonus6 = new ShowBonus("6,最佳企宣片导演奖", "王家卫", 25);
ShowBonus showBonus7 = new ShowBonus("7,最佳产品片导演奖", "李安", 20);
ShowBonus showBonus8 = new ShowBonus("7,最佳晚会导演奖", "斯皮尔伯格", 13);
listShowBonus.add(showBonus1);
listShowBonus.add(showBonus2);
listShowBonus.add(showBonus3);
listShowBonus.add(showBonus4);
listShowBonus.add(showBonus5);
listShowBonus.add(showBonus6);
listShowBonus.add(showBonus7);
listShowBonus.add(showBonus8);
}
}
标签:
原文地址:http://www.cnblogs.com/qiaofei/p/4710116.html