关于ListView中的适配器Adapter的设置方法有很多种。可以使用SimpleAdapter实现简单的列表各项,当然我们一般使用比较多的是定义一个Adapter继承BaseAdapter实现的....
当然其他方法也有,但是本人目前只使用过这两个方法,当然也是比较常用的方法.
1、
SimpleAdapter adapter=new SimpleAdapter(this,list,R.layout.musiclist_item,new String[]{"title","artiest","dur_time","img"},
new int[]{ R.id.music,R.id.singer,R.id.time,R.id.img});
listview.setAdapter(adapter);
实例化SimpleAdapte过程中,第二个参数是列表中显示各项内容的一个List,第三项是列表项的Layout,第四个参数和第五个参数分别对应第二个参数和第三个参数里面各项的映射关系...如我们可以定义list为:List<Map<String, Object>> list =new ArrayList<Map<String, Object>>(); Map中的String键值参数需要与SimpleAdapter构造函数中的第四个参数里面参数对应,呵呵,可能说的不是很清楚..自己做个demo就能很好的实现所说的该功能...
2、使用BaseAdapter
BaseAdapter是实现了ListAdapter和SpinnerAdapter两个接口,当然它也可以直接给ListView和Spinner等UI组件直接提供数据。
自定义BaseAdapter子类,就需要实现下面几个方法,
public int getCount() {
}
public Object getItem(int position) {
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
}
第一个getCount()方法主要实现的是返回列表的行数,getItem()是指返回的Object值,而getItemId()方法则是返回列表中各项的id值,一般用于对列表进行单击监听处理时使用,比如对列表中最后的(更多)项进行监听处理时可以通过在getItemId中返回一个-1值进行确认选中该项。
getView()方法是最重要的方法,它是将获取数据后的View组件返回,如ListView中每一行里的TextView、Gallery中的每个ImageView等组件。通过该方法调用从而得到列表中的所有行项。
我们使用一个继承BaseAdapter的自定义适配器通常要和一个存储列表每项数据信息的List<?>对象进行搭配使用...
好了,闲话不说,上点代码..
class CommentAdapter extends BaseAdapter{ private View rowView; private Map<Integer, View> rowViews = new HashMap<Integer, View>(); public CommentAdapter(){ } @Override public int getCount() { // TODO Auto-generated method stub return comment_list.size()+1; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return comment_list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub if(this.getCount()-1==position) return -1; else return position; } @Override public View getView(int position, View view, ViewGroup arg2) { // TODO Auto-generated method stub if(position==this.getCount()-1){ view=LayoutInflater.from(Comment.this).inflate(R.layout.more_item, null); TextView more_item=(TextView)view.findViewById(R.id.more_item); if((this.getCount()-1)%PageSize!=0) more_item.setText("没有留言了哦..."); return view; } else{ LayoutInflater layoutInflater = LayoutInflater.from(Comment.this);//从当前上下文得到LayoutInflater rowView = rowViews.get(position);//用了一个类似缓存功能,不过据说List中就有缓存功能.... if(rowView==null){ rowView=layoutInflater.inflate(R.layout.comment_item, null); TextView comment_name = (TextView) rowView.findViewById(R.id.comment_item_name); TextView comment_time = (TextView) rowView.findViewById(R.id.comment_item_time); TextView comment_text = (TextView) rowView.findViewById(R.id.comment_item_text); comment_name.setText(comment_list.get(position).getName()); comment_time.setText(getTime(comment_list.get(position).getTime())); comment_text.setText(comment_list.get(position).getText()); rowViews.put(position, rowView); } return rowView; } }这样自定义的Adapter就完成了。
然后在对应的列表中使用setAdapter(adapter)即可。
CommentAdapter adapter=new CommentAdapter();
comment_listview.setAdapter(adapter);
3.在列表中添加最后一项(更多),当我们点击它的时候就会加载出更多的列表项..
此方法实现不是很难,只要在自定义的Adapter中的getCount()中return值加1,同时在getItemId(position)方法中处理一下。如下
public int getCount() { // TODO Auto-generated method stub return comment_list.size()+1; } public long getItemId(int position) { // TODO Auto-generated method stub if(this.getCount()-1==position) return -1; else return position; }
public View getView(int position, View view, ViewGroup arg2) { // TODO Auto-generated method stub if(position==this.getCount()-1){ view=LayoutInflater.from(Comment.this).inflate(R.layout.more_item, null); TextView more_item=(TextView)view.findViewById(R.id.more_item); if((this.getCount()-1)%PageSize!=0) more_item.setText("没有留言了哦..."); return view; } else{ ... } }
comment_listview.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) { // TODO Auto-generated method stub if(arg3==-1){//通过此判断为最后一个列表项 page++; comment_list.addAll(getComment(page));//添加更多的列表内容,加载 ((CommentAdapter)comment_listview.getAdapter()).notifyDataSetChanged();//更新列表... } } });