SimpleAdapter的使用步骤如下:

  • 声明ListView,并进行初始化操作
  • 准备数据集,一般用list来实现,当然也可以使用数组
  • 为listview适配simpleadapter
    如下代码:

声明ListView

private ListView mListView;

准备数据集

static List<Map<String,Object>> data=null;    static{        data=new ArrayList<Map<String,Object>>();        for(int i=1;i<=28;i++){            Map<String,Object>map=new HashMap<String,Object>();            map.put("nametext", i);            map.put("iconid", R.drawable.ic_launcher);            data.add(map);            map=null;        }    };

适配操作

 SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,data , R.layout.items,                new String[]{"nametext","iconid"}, new int[]{R.id.imageview,R.id.textview});        mListView.setAdapter(adapter);

注意,在这个过程中我们来看一看需要注意的地方;
1,data就是我们刚才准备的数据集;
2,接下来是R.layout.items,这是什么呢?答案就是我们在ListView中展示的内部信息。等会我会展示R.layout.item的xml内容;
3, 接下来是对应data和item.xml文件中的相关的数据项的内容
4,这个int型的数组就是我们在item.xml文件中声明的id的值

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="54dp" android:orientation="horizontal" >    <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="fitXY" android:layout_weight="5" android:src="@drawable/ic_launcher" />    <TextView  android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="2" android:text="Result" android:gravity="center" /></LinearLayout>

ListView简易进阶
添加点击事件和长按删除事件。也是有如下步骤

  • 声明ListView的实例
  • 添加点击事件处理方法
  • 添加长按事件处理方法
    代码如下

点击事件处理方法

 mListView.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> arg0, View view, int arg2,                    long arg3) {                // TODO Auto-generated method stub                 TextView tv = (TextView)view.findViewById(R.id.textview);                      Toast.makeText(getApplicationContext(),                              tv.getText()+" Clicked!", Toast.LENGTH_SHORT).show();             }        });

长按事件,删除操作处理方法,代码如下

 mListView.setOnItemLongClickListener(new OnItemLongClickListener() {            @Override            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,                    int arg2, long arg3) {                // TODO Auto-generated method stub                TextView tv = (TextView)arg1.findViewById(R.id.textview);                 Toast.makeText(getApplicationContext(),                          tv.getText()+" Deleted!", Toast.LENGTH_SHORT).show();                data.remove(arg2);                SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,data , R.layout.items,                        new String[]{"nametext","iconid"}, new int[]{R.id.imageview,R.id.textview});                mListView.setAdapter(adapter);                return false;            }        });

总结
这样就能方便快捷的实现你的ListView的使用了。当然实际开发过程中,仅仅会这一种方式的适配器是远远不够的,比如BaseAdapter就是比较常用 的一种适配器。我们应该全面进行掌握,这样才能在开发过程中更加灵活。

//比如下面这篇博文,就真的不错哦//http://blog.csdn.net/zhanggang740/article/details/50146121

更多相关文章

  1. Android设置透明效果的三种方法
  2. Android(安卓)如何通过menu id来得到menu item 控件--binbinyang
  3. Android(安卓)平板 控制软键盘只弹出一半,自动盯着界面中EditText
  4. android 多界面应用程序退出的方法
  5. [forward]Android完全退出应用程序
  6. Android(安卓)7.1 双卡双待机器,首选网络类型设置 详细分析
  7. 《Android(安卓)对话框大全》 方法超简单
  8. Cocos2d-x微信登陆Demo
  9. Android(安卓)background背景图片平铺

随机推荐

  1. Android 的实现TextView中文字链接的4种
  2. Android 网络(一) HTTP协议
  3. 1. Android启动过程
  4. Android中的五大存储
  5. android之用户定位(一)
  6. SavedStateHandle的使用,临时保存数据
  7. Android 系统服务管家servicemanager启动
  8. 引用 Android上dip、dp、px、sp等单位说
  9. Android短消息推送启动应用程序(SMS PUSH)(B
  10. Android浮动窗口实现原理及代码实例