常见数据适配器

常见数据适配器

  • ArrayAdapter 数组适配器

    java代码:(要记得声明ListView控件)
    
    String[] objects = new String[]{"app","context","Media","nfc","os"}
    lv.setAdapter(new ArrayAdapter<String>(this,R.layout.item,objects));
    
    Android代码: 
    
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:textSize="20dp"
           android:textColor="#66ff0000"
    >
    </TextView>
    

  • SimpleAdapter 简单数据适配器

  • java代码:(要记得声明ListView控件)
    
    List<Map<String,Object>> data= new                            ArrayList<Map<String,Object>>();
          Map<String,Object> map1 = new HashMap<String, Object>();
          map1.put("icon",R.drawable.ic_launcher);
          map1.put("name", "功能设置");
          data.add(map1);
          Map<String,Object> map2 = new HashMap<String, Object>();
          map2.put("icon",R.drawable.ic_tupian);
          map2.put("name", "qita设置");
          data.add(map2);
      lv.setAdapter(newSimpleAdapter(this,data,R.layout.item, new String[]{"icon","name"}, new int[]{R.id.iv,R.id.tv}));
      
    Android代码: 
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <ImageView 
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView  
            android:id="@+id/tv"          
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <LinearLayout/>
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容