Android使用SimpleAdapter

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

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
目录
相关文章
|
XML Java Android开发
Android安卓——SimpleAdapter的简单使用
本文是博主对Adapter(适配器)的一些理解,为了加深对Adapter的理解以及记录自己的阶段学习而写,同时也适合初学者阅读,参考本条博客的逻辑进行学习。
102 0
|
Android开发 数据格式 XML
|
Android开发
Android零基础入门第41节:使用SimpleAdapter
原文:Android零基础入门第41节:使用SimpleAdapter    通过ArrayAdapter实现Adapter虽然简单、易用,但ArrayAdapter的功能比较有限,它的每个列表项只能给一个TextView动态填充内容。
1619 0
|
Android开发 容器
Android——继承SimpleAdapter重写自己的getView
                            在上篇Android——ListView实现简单列表 中,利用SimpleAdapter实现了简单的列表绑定。在本文中,主要是通过重写getView方法,来看下adapter内部绑定数据大致是个什么流程。
1426 0