- <?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/listview3listview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- </ListView>
- </LinearLayout>
listitem3.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="horizontal"
- android:id="@+id/MyListItem3"
- android:paddingBottom="3dip"
- android:paddingLeft="10dip" >
- <ImageView
- android:id="@+id/listitem3imageview"
- android:layout_height="fill_parent"
- android:layout_width="wrap_content"
- />
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <TextView
- android:id="@+id/listitem3ItemTitle"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:textSize="30dip"
- />
- <TextView
- android:id="@+id/listitem3ItemText"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- />
- </LinearLayout>
- <Button
- android:id="@+id/listitem3button"
- android:layout_height="fill_parent"
- android:layout_width="wrap_content"
- android:text="delete"
- />
- </LinearLayout>
ListViewActivity3.java
- package my.android.activity;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.AlertDialog;
- import android.app.ListActivity;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- 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.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- public class ListViewActivity3 extends ListActivity {
- private List<Map<String, Object>> mData;
- MyAdapter adapter = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- mData = getData();
- 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", "G1");
- map.put("info", "google 1");
- map.put("img", R.drawable.android1);
- list.add(map);
- map = new HashMap<String, Object>();
- map.put("title", "G2");
- map.put("info", "google 2");
- map.put("img", R.drawable.android2);
- list.add(map);
- map = new HashMap<String, Object>();
- map.put("title", "G3");
- map.put("info", "google 3");
- map.put("img", R.drawable.android3);
- list.add(map);
- return list;
- }
- // ListView 中某项被选中后的逻辑
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- Log.v("MyListView4-click", (String) mData.get(position).get("title"));
- }
- // listview中点击按键弹出对话框
- public void showInfo(final int position) {
- new AlertDialog.Builder(this).setTitle("我的提示").setMessage("确定要删除吗?")
- .setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- mData.remove(position);
- // 通过程序我们知道删除了,但是怎么刷新ListView呢?
- // 只需要重新设置一下adapter
- setListAdapter(adapter);
- }
- }).show();
- }
- public final class ViewHolder {
- public ImageView img;
- public TextView title;
- public TextView info;
- public Button viewBtn;
- }
- 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;
- }
- /**
- * listView在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得到listView的长度
- * 然后根据这个长度,调用getView()逐一绘制每一行。 如果你的getCount()返回值是0的话,列表将不显示同样return
- * 1,就只显示一行。 系统显示列表时,首先实例化一个适配器。
- *
- * 当手动完成适配时,必须手动映射数据,这需要重写getView()方法。 系统在绘制列表的每一行的时候都将调用此方法,(可以设个变量试试)
- * getView()有三个参数 position表示将显示的是第几行 covertView是从布局文件中inflate来的布局
- * 我们用LayoutInflater的方法将定义好的vlist2.xml文件提取成View实例用来显示。
- * 然后将xml文件中的各个组件实例化(简单的findViewById()方法)。 这样便可以将数据对应到各个组件上了
- *
- * 但是按钮为了响应点击事件,需要为它添加点击监听器,这样就能捕获点击事件。 至此一个自定义的listView就完成了
- *
- * 现在让我们回过头从新审视这个过程 系统要绘制ListView了,他首先获得要绘制的这个列表的长度,然后开始绘制第一行,怎么绘制呢?
- * 调用getView()函数。在这个函数里面首先获得一个View(实际上是一个ViewGroup) 然后再实例并设置各个组件并显示。
- * 绘制完这一行了。再绘制下一行,直到绘完为止。
- */
- @Override
- public View getView(final int position, View convertView,
- ViewGroup parent) {
- // 通过Log.i()可以发现这个会多次调用,容易理解
- ViewHolder holder = null;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = mInflater.inflate(R.layout.listitem3, null);
- holder.img = (ImageView) convertView
- .findViewById(R.id.listitem3imageview);
- holder.title = (TextView) convertView
- .findViewById(R.id.listitem3ItemTitle);
- holder.info = (TextView) convertView
- .findViewById(R.id.listitem3ItemText);
- holder.viewBtn = (Button) convertView
- .findViewById(R.id.listitem3button);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.img.setBackgroundResource((Integer) mData.get(position).get(
- "img"));
- holder.title.setText((String) mData.get(position).get("title"));
- holder.info.setText((String) mData.get(position).get("info"));
- holder.viewBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- showInfo(position);
- }
- });
- return convertView;
- }
- }
- }
理解了前一篇ListView(BaseAdapter)的这一篇应该很好理解了。就是把参数定义成了final,方便调用。
也可以按照前一篇的模式来写,我也感觉前一篇的模式比较好。
截图:那图片上面有123不是程序搞得。。。是自己把图片搞了一下。。。
http://blog.youkuaiyun.com/zhengliusu/article/details/7178681