SimpleAdapter

来源:互联网 发布:java可视化开发工具 编辑:程序博客网 时间:2024/06/05 01:09

SimpleAdapter使用list<map>形式的数据源,而map中又包含不同控件的数据源,比如一个图片资源、一段文字资源等等,所以SimpleAdapter可以为每一个列表项中包含多个不同控件的ListView填充数据,list中的一个map对应一个列表,map中的一个元素对应一个控件资源,

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivityextends ListActivity {

    private ArrayList<HashMap<String, Object>>dataList;
    SimpleAdapter simpleAdapter;




    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //用于存放数据源
        
dataList=newArrayList<HashMap<String, Object>>();
        //数据源
        
HashMap<String, Object> hashMap;
        for(inti=1;i<=50;i++){
            hashMap=new HashMap<String, Object>();
            hashMap.put("img", R.drawable.ic_launcher);
            hashMap.put("text","第"+i+"项文本信息");
            hashMap.put("cbx","");
            dataList.add(hashMap);
        }
           //实例化SimpleAdapter
        //上下文、         数据源<list<map>>        String数组  key    int数组控件的id
        // 控件的id需要跟数据源的map的key一致
        
simpleAdapter=newSimpleAdapter(this,dataList, R.layout.simpleadapter_item_list,new String[]{
                "img","text","cbx"

        
}, new int[]{R.id.img,R.id.text,R.id.cbk});

        this.setListAdapter(simpleAdapter);

    }

    @Override
    protected voidonListItemClick(ListView l, View v, intposition, long id) {
        super.onListItemClick(l, v, position, id);

//提示信息
        Toast.makeText(MainActivity.this, ((TextView)((RelativeLayout)v).getChildAt(1)).getText() +"被删除了", Toast.LENGTH_SHORT).show();

//移除被点击过的控件
        dataList.remove(position);

//刷新每个Item的内容。
          simpleAdapter.notifyDataSetChanged();


    }
}

 

 

 

Xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

<ImageView 

    android:id="@+id/img"

    android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginTop="10dp"

android:contentDescription="@drawable/ic_launcher"

    />

    

    

    

    <TextView 

        android:id="@+id/text"

        android:layout_width="wrap_content"

              android:layout_height="wrap_content"

                android:layout_marginLeft="10dp"

                android:layout_centerVertical="true"

                android:layout_toRightOf="@+id/img"

                android:textSize="14sp"

                

        />

    <CheckBox 

        android:id="@+id/cbk"

          android:layout_width="wrap_content"

              android:layout_height="wrap_content"

      android:layout_toRightOf="@+id/text"

    android:layout_marginLeft="10dp"

         android:layout_centerVertical="true"

    android:focusable="false"

    android:focusableInTouchMode="false"

         

        />

 

</RelativeLayout>

 

 

效果:点击第五项然后第五项就被删除了

哈哈

 

 

 

 

 

 

目录:

哈哈

 

原创粉丝点击