再来一个带有按钮的吧。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="25px" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="15px" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看详情"
android:layout_gravity="left" />
</LinearLayout>
</LinearLayout>
MianActivity类:
public class MainActivity extends ListActivity {
private List<Map<String, Object>> mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = getData();
MyAdapter adapter = new MyAdapter(this);
setListAdapter(adapter);
}
private List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "Android简介");
map.put("message", "google公司");
map.put("image", R.drawable.away);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "运行第一个Android程序");
map.put("message", "java的环境");
map.put("image", R.drawable.busy);
list.add(map);
map = new HashMap<String, Object>();
map.put("title", "Android的常用UI控件");
map.put("message", "很多中,如TextView、");
map.put("image", R.drawable.msn);
list.add(map);
return list;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
public void showInfo(){
new AlertDialog.Builder(this)
.setTitle("我的listview")
.setMessage("介绍...")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
public final class ViewHolder{
public ImageView image;
public TextView title;
public TextView message;
public Button button;
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;
public MyAdapter(Context context){
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder=new ViewHolder();
convertView = mInflater.inflate(R.layout.buttonlistview, null);
holder.image = (ImageView)convertView.findViewById(R.id.image);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.message = (TextView)convertView.findViewById(R.id.message);
holder.button = (Button)convertView.findViewById(R.id.button);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.image.setBackgroundResource((Integer)mData.get(position).get("image"));
holder.title.setText((String)mData.get(position).get("title"));
holder.message.setText((String)mData.get(position).get("message"));
holder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showInfo();
}
});
return convertView;
}
}
}
看看最后的运行结果吧:
好了。再不懂ListView的朋友,是不是应该........拉出去........呵呵,开玩笑的啦。相信大家应该懂了吧。再不懂的朋友就把我写的代码自己再一个字母一个字母的敲吧。