Android使用SimpleAdapter

来源:互联网 发布:php 找不到指定的模块 编辑:程序博客网 时间:2024/05/16 05:35

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
0 0
原创粉丝点击