ListView使用过程记录
2015-09-07 13:48
537 查看
使用listView需要用到三个要素,list数据、xml布局文件、adapter。
adapter的作用就是将list数据放入到xml布局文件中。
首先是xml布局文件:一个是主layout,上面linearLayout中是列名,下面定义了一个listview,用SwiperRefreshLayout包起来,这是个刷新组件。
到此,就完成了整个listview的配置。
附:SwiperRefreshLayout是google官方给的刷新组件,其配置如下,可以实现多彩的刷新效果。
adapter的作用就是将list数据放入到xml布局文件中。
首先是xml布局文件:一个是主layout,上面linearLayout中是列名,下面定义了一个listview,用SwiperRefreshLayout包起来,这是个刷新组件。
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#7DB6E9" android:orientation="horizontal" > <TextView android:id="@+id/car_num" android:layout_width="fill_parent" android:layout_height="30dp" android:layout_weight="0.9" android:gravity="center" android:text="车号" android:textSize="16sp" /> <TextView android:id="@+id/createtime" android:layout_width="fill_parent" android:layout_height="30dp" android:layout_weight="0.75" android:gravity="center" android:text="时间" android:textSize="16sp" /> <TextView android:id="@+id/goods_name" android:layout_width="fill_parent" android:layout_height="30dp" android:layout_weight="1" android:gravity="center" android:text="油品" android:textSize="16sp" /> </LinearLayout>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/swipe_container" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </android.support.v4.widget.SwipeRefreshLayout>另一个layout定义了listview中的具体内容,单独一个文件:list_view.xml这个文件中有三个textview,表示有三列。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/car_num_value" android:layout_width="120dip" android:layout_height="30dip" android:textSize="10pt" android:singleLine="true" /> <TextView android:id="@+id/createtime_value" android:layout_width="150dip" android:layout_height="fill_parent" android:gravity="left" android:textSize="10pt" /> <TextView android:id="@+id/goods_name_value" android:layout_width="70dip" android:layout_height="fill_parent" android:gravity="center" android:textSize="10pt" /> </LinearLayout>然后是准备数据list,我的代码里定义了一个mlist,用来装PoundList对象,list中需要往listview中放入的数据只有三条,但是可以存放多余的数据。
public ArrayList<PoundList> mlist = new ArrayList<PoundList>();
jArr = new JSONArray(executeHttpGet); if (jArr != null) { for (int i = 0; i < jArr.length(); i++) { JSONObject dataObj = jArr.getJSONObject(i); String str[] = new String[5]; str[0] = dataObj.getString("BLD_CPH"); str[1] = dataObj.getString("SY_CREATETIME").substring(2,13)+"时"; str[2] = dataObj.getString("BLD_HPMC"); str[3] = dataObj.getString("BLD_BH"); str[4] = dataObj.getString("BLD_GHDW_NAME"); PoundList bean = new PoundList(str); mlist.add(bean); }<span style="font-family: Arial, Helvetica, sans-serif;">}</span>接下来是adapter,继承了BaseAdapter,定义了一个viewHolder类,将list_view.xml中的每列都对象化为viewHolder中的一个成员,通过给该对象赋值来达到使listview显示数据的目的。
public class MyListAdapter extends BaseAdapter { public List<PoundList> mList; LayoutInflater inflater; public MyListAdapter(Context pContext, List<PoundList> pList) { inflater=LayoutInflater.from(pContext); if (pList != null) { mList = pList; } else { mList = new ArrayList<PoundList>(); } } @Override public int getCount() { // TODO Auto-generated method stub return mList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { holder= new ViewHolder(); convertView = inflater.inflate( R.layout.list_view, null); holder.tv1 = (TextView) convertView .findViewById(R.id.car_num_value); holder.tv2 = (TextView) convertView .findViewById(R.id.createtime_value); holder.tv3 = (TextView) convertView .findViewById(R.id.goods_name_value); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.tv1.setText(mList.get(position).getCarNum()); holder.tv2.setText(mList.get(position).getCreatetime()); holder.tv3.setText(mList.get(position).getGoodsName()); return convertView; } private static class ViewHolder { public TextView tv1; public TextView tv2; public TextView tv3; } }最后是最终实现赋值的过程。直接实例化adapter,配置adapter。这里给list加了监听事件,在点击一个item时将PoundList对象序列化,放入intent中传给下一个activity。
list = (ListView) findViewById(R.id.listView1); adapter = new MyListAdapter(this, mlist); list.setAdapter(adapter); list.setOnItemClickListener(new OnItemClickListener() { <span style="white-space:pre"> </span>@Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { // TODO Auto-generated method stub PoundList poundList = mlist.get(position); Intent intent = new Intent(); <span style="white-space:pre"> </span>intent.setClass(PoundListActivity.this, PoundListDetailActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("poundList", (Serializable) poundList); intent.putExtras(bundle); PoundListActivity.this.startActivity(intent); }
到此,就完成了整个listview的配置。
附:SwiperRefreshLayout是google官方给的刷新组件,其配置如下,可以实现多彩的刷新效果。
在onCreate()中 swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,android.R.color.holo_green_light,android.R.color.holo_orange_light,android.R.color.holo_red_light);
//下拉刷新监听 public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { swipeLayout.setRefreshing(false); adapter.notifyDataSetChanged(); } }, 2000); }
相关文章推荐
- 用户·角色·权限·表
- Java代理模式
- Android软键盘强制弹出,隐藏输入法.
- ORACLE 全局索引和本地索引
- liunx下swf转换 相关问题____http://bbs.csdn.net/topics/390797316
- json集合字符串转对象
- Play 起步
- AFNetworking作用和用法详解
- zookeeper安装小记
- 多媒体
- 面试题 14
- 面试题(一)
- XssSniper 扩展介绍
- R语言数据框
- C++ 16进制转10进制
- 5种类型的程序员-兄弟连IT教育
- 网站的可扩展架构
- __stdcall
- How to install / setup /upgrade PHP 5.5.x on Ubuntu 12.04 LTS
- php-fpm开机自动启动脚本其实源码包里边就有