Android listview与adapter用法
http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html
package com.example.listviewtest; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; //import com.example.listviewtest.MyAdapter; public class BaseAdapterTest extends Activity { private ListView lv; ArrayList<HashMap<String, Object>> listItem; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.listview01); MyAdapter mAdapter = new MyAdapter(this); //得到一个MyAdapter对象 lv.setAdapter(mAdapter); //为ListView绑定Adapter /*为ListView添加点击事件*/ lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.v("BaseAdapterTest", "你点击了ListView条目" + position); } }); } /*添加一个得到数据的方法,方便使用*/ private ArrayList<HashMap<String, Object>> getData() { ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String,Object>>(); /*为动态数组添加数据*/ for (int i = 0; i < 30; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemTitle", "第" + i + "行"); map.put("ItemText", "这是第" + i + "行"); listItem.add(map); } return listItem; } // 新建一个类继承BaseAdapter,实现视图与数据的绑定 private class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; //得到一个LayoutInfalter对象用来导入布局 public MyAdapter(Context context) { this.mInflater = LayoutInflater.from(context); } @Override public int getCount() { return getData().size(); //返回数组的长度 } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; Log.v("BaseAdapterTest", "getView " + position + " " + convertView); if (convertView == null) { convertView = mInflater.inflate(R.layout.listitem, null); holder = new ViewHolder(); /*得到各个控件的对象*/ holder.title = (TextView) convertView.findViewById(R.id.ItemTitle); holder.text = (TextView) convertView.findViewById(R.id.ItemText); holder.bt = (Button) convertView.findViewById(R.id.ItemButton); // to ItemButton convertView.setTag(holder); //绑定ViewHolder对象 } else { holder = (ViewHolder) convertView.getTag(); //取出ViewHolder对象 } /*设置TextView显示的内容,即我们存放在动态数组中的数据*/ holder.title.setText(getData().get(position).get("ItemTitle").toString()); holder.text.setText(getData().get(position).get("ItemText").toString()); /*为Button添加点击事件*/ holder.bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.v("BaseAdapterTest", "你点击了按钮" + position); //打印Button的点击信息 } }); return convertView; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } /*存放控件 的ViewHolder*/ public final class ViewHolder { public TextView title; public TextView text; public Button bt; } }