美文网首页
常见数据适配器

常见数据适配器

作者: 姜水伟杰 | 来源:发表于2017-08-06 17:40 被阅读59次

    常见数据适配器

    • 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/>
      

    相关文章

      网友评论

          本文标题:常见数据适配器

          本文链接:https://www.haomeiwen.com/subject/rshnlxtx.html