Android常使用的控件(中)
ListView控件
首先建立一个xml文件 list_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/txtID"
android:layout_width="120px"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/txtName"
android:layout_width="120px"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btnShow"
android:layout_width="120px"
android:layout_height="wrap_content"
android:text="显示姓名"
/>
</LinearLayout>
再建一个 listview.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="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
创建一个PersonBean的用来当做源数据
import java.util.ArrayList;
import java.util.List;
public class PersonBean {
private int ID;
private String Name;
public int getID() {
return ID;
}
public void setiD(int ID) {
this.ID = ID;
}
public String getName() {
return Name;
}
public void setName(String name) {
this.Name = name;
}
public static List<PersonBean> GetData() {
List<PersonBean> list=new ArrayList<PersonBean>();
for (int i = 0; i < 10; i++) {
PersonBean personBean=new PersonBean();
personBean.ID=i;
personBean.Name="ulimate"+i;
list.add(personBean);
}
return list;
}
}
自定义个适配器,用来当做绑定listview的桥梁
import java.util.List;
import java.util.Map;
import android.R.layout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater; //用来实例化布局
private List<Map<String, Object>> list; //数据源
Context context;
/**
* 构造函数
* @param context
* @param list
* @param LayoutID
* @param flag
* @param ItemIDs
*/
public MyAdapter(Context context,List<Map<String, Object>> list){
this.mInflater=LayoutInflater.from(context);
this.list=list;
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
/**
*
* @param postion 绘制的行数
* @param convertView listview每一行的布局
* @param parent view的集合,不设置就为null
*/
@Override
public View getView(int postion, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=mInflater.inflate(R.layout.list_item, null); //将布局的通过mInflater实例化为一个view
}
final TextView txtID=(TextView)convertView.findViewById(R.id.txtID); //获取list_item.xml中的txtid控件
txtID.setText(list.get(postion).get("ID").toString()); //赋值
final TextView txtName=(TextView)convertView.findViewById(R.id.txtName);//获取list_item.xml中的txtname控件
txtName.setText(list.get(postion).get("Name").toString());//赋值
final Button btnShow=(Button)convertView.findViewById(R.id.btnShow);//获取list_item.xml中的btnDemo控件
btnShow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==btnShow){
Toast.makeText(context, txtID.getText()+":"+txtName.getText(), Toast.LENGTH_LONG).show();
}
}
});
return convertView;
}
}
这样我们就做好了所有的准备,下面在Activity里面加入下面代码就可以实现listview的绑定,且每行按钮的点击事件
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {//每个Activity创建的时候的入口函数
super.onCreate(savedInstanceState);
MyAdapter myAdapter=new MyAdapter(this, getData());
setContentView(R.layout.listview);
lv=(ListView)this.findViewById(R.id.lv);
lv.setAdapter(myAdapter);
}
/**
* 获取数据
* @return
*/
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); //定义一个变量
List<PersonBean> listPerson=PersonBean.GetData(); //获取数据
for (PersonBean personBean : listPerson) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("ID", personBean.getID());
map.put("Name", personBean.getName());
list.add(map);
}
return list;
}