ListView 自定义适配器 网上查了有很多,在此记录一下。
ListView绑定数据显示,实现效果如下:
一、定义一个布局文件checkvouch_unverify_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="60dp" android:orientation="horizontal" > <!-- id --> <TextView android:id="@+id/id" android:textSize="40sp" android:gravity="center" android:layout_width="55dp" android:layout_height="match_parent" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="3" android:orientation="vertical" > <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- cMaker --> <TextView android:id="@+id/cMaker" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- dAddDate --> <TextView android:id="@+id/dAddDate" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <!-- cVerify --> <TextView android:id="@+id/cVerifyStatus" android:layout_weight="1" android:gravity="center" android:layout_width="wrap_content" android:layout_height="match_parent" /> <!-- cVerifyDate --> <!-- Button设置为没有焦点 --> <Button android:id="@+id/btnVerify" android:focusable="false" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="@string/btn_Verify" /> </LinearLayout>
二、自定义适配器类继承BaseAdapter
/** * 自定义ListView适配器 * @author zhenyun * */ private class CheckListAdapter extends BaseAdapter { private List<CheckVouch> list=null; private Context context=null; public CheckListAdapter(List<CheckVouch> list,Context context) { super(); this.list= list; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return list.get(position).getId(); } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder item = null; if(convertView ==null) { convertView = LayoutInflater.from(context).inflate(R.layout.checkvouch_unverify_item, null); item = new ViewHolder(); item.id= (TextView) convertView.findViewById(R.id.id); item.name= (TextView) convertView.findViewById(R.id.name); item.cMaker= (TextView) convertView.findViewById(R.id.cMaker); item.dAddDate= (TextView) convertView.findViewById(R.id.dAddDate); item.cVerifyStatus= (TextView) convertView.findViewById(R.id.cVerifyStatus); item.btnVerify= (Button) convertView.findViewById(R.id.btnVerify); convertView.setTag(item);//绑定ViewHolder对象 } else { item = (ViewHolder) convertView.getTag();//取出ViewHolder对象 } /**设置TextView显示的内容,即我们存放在动态数组中的数据*/ item.id.setText(String.valueOf(list.get(position).getId())); item.name.setText(list.get(position).getName()); item.cMaker.setText(list.get(position).getcMaker()); item.dAddDate.setText(DateUtils.DateToString(list.get(position).getdAddDate(),"yyyy-MM-dd")); item.cVerifyStatus.setText(list.get(position).getcVerify().equals("")?"未审":"已审"); /** * 设置按钮点击事件 */ item.btnVerify.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Log.i("---->","你点击了按钮:"+getItemId(position)); } }); return convertView; } } /** * 存储控件 * @author zhenyun * */ public class ViewHolder { public TextView id; public TextView name; public TextView cMaker; public TextView dAddDate; public TextView cVerifyStatus; public Button btnVerify; }
三、获取数据集合、绑定显示列表(MainActivity)
1\activity_main.xml布局文件
<?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="vertical" > <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </LinearLayout>
2、MainActivity类代码
public class MainActivity extends Activity { private ListView listView; private ProgressDialog dialog; //获取数据集合 private List<CheckVouch> list = new ArrayList<CheckVouch>(); private Handler handler = new Handler() { @Override public void handleMessage(android.os.Message msg) { ListResult<CheckVouch> result = (ListResult<CheckVouch>) msg.obj; if (result.getResult() == 1) { CheckListAdapter adapter = new CheckListAdapter(result.getList(), MainActivity.this); listView.setAdapter(adapter); } else { Toast.makeText(getApplicationContext(), result.getMessage(), Toast.LENGTH_LONG); } dialog.dismiss(); }; }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub //设置标题 setTitle(R.string.uncheck_titile); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) this.findViewById(R.id.listView); dialog = new ProgressDialog(this); dialog.setTitle("提示"); dialog.setMessage("正在查询..."); dialog.show(); new Thread(new SearchThread()).start(); } @Override public void setTitle(CharSequence title) { // TODO Auto-generated method stub //super.setTitle(R.string.uncheck_titile); super.setTitle(title); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub // return super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main, menu); return true; } private class SearchThread implements Runnable { @Override public void run() { // TODO Auto-generated method stub ListResult<CheckVouch> result = Common.getInstance() .checkvouch_SelectList(); Message msg = Message.obtain(); msg.obj = result; handler.sendMessage(msg); } } }
我读取的是网络数据,必须开启一个新的线程,所以放在handler中处理显示的数据。