android UI控件之ListView详解之二listView+SimpleAdpater

android UI控件之ListView详解之二listView+SimpleAdpater

SimpleAdpater是BaseAdpater,实现了BaseAdpater的四个抽象方法并进行了封装。也需要对数据进行适配。

构造方法:

Public SimpleAdpater(Contextcontext,List>data,int resource,String[]from,int[] to);

Context context:Context上下文对象

List>data:数据集合,data中的每一项对应着ListView中每一项数据。

int resource:Item布局的资源id

String[] from :map集合里面的key值。

Int[]  to:Item布局相应的控件id。

 代码MainActivity.java:

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
    private ListView LV;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LV=(ListView)findViewById(R.id.LV);
        //准备集合数据
        List> data=new ArrayList>();
        Map map=new HashMap();
        map.put("icon",R.drawable.m1);
        map.put("name","name--1");
        map.put("context","context--1");
        data.add(map);
        map = new HashMap();
        map.put("icon",R.drawable.m2);
        map.put("name","name--2");
        map.put("context","context--2");
        data.add(map);
        map = new HashMap();
        map.put("icon",R.drawable.m3);
        map.put("name","name--3");
        map.put("context","context--3");
        data.add(map);
        map = new HashMap();
        map.put("icon",R.drawable.m4);
        map.put("name","name--4");
        map.put("context","context--4");
        data.add(map);
        map = new HashMap();
        map.put("icon",R.drawable.m5);
        map.put("name","name--5");
        map.put("context","context--5");
        data.add(map);
        map = new HashMap();
        map.put("icon",R.drawable.m6);
        map.put("name","name--6");
        map.put("context","context--6");
        data.add(map);
        map = new HashMap();
        map.put("icon",R.drawable.m7);
        map.put("name","name--7");
        map.put("context","context--7");
        data.add(map);
        //map对象中的key的数组, 用于得到对应的value
        String [] from={"icon","name","context"};
        //Item布局文件中的子view的id的数组,from...to..从哪对应到哪。一定是一一对应的
        int[] to={R.id.icon,R.id.tv_name,R.id.tv_context};
        //准备SimpleAdapter对象
        SimpleAdapter simpleAdapter=new SimpleAdapter(this,data,R.layout.item_simple_adpater,from,to);
        //设置Adapter显示列表
        LV.setAdapter(simpleAdapter);
    }
}
item_simple_adpater.xml:


     android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/btn_star_big_on" />
    
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name"
             />
        android:id="@+id/tv_context"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Context"
            />
    
main_activity.xml:


    android:id="@+id/LV"//数据通过适配器,添加到Listview
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >


android UI控件之ListView详解之二listView+SimpleAdpater_第1张图片


你可能感兴趣的:(Android)