Android各种Adapter用法

智慧并不产生于学历,而是来自对于知识的终生不懈的追求。这篇文章主要讲述Android各种Adapter用法相关的知识,希望能为你提供帮助。
ArrayAdapter比较简单,但它只能用于显示文字。

Android各种Adapter用法

文章图片
Android各种Adapter用法

文章图片
1 public class MainActivity extends AppCompatActivity { 2private Spinner myspinner; 3private ArrayList< String> mlist=new ArrayList< String> (); 4private ArrayAdapter myadapter; 5@Override 6protected void onCreate(Bundle savedInstanceState) { 7super.onCreate(savedInstanceState); 8setContentView(R.layout.activity_main); 9myspinner=(Spinner)findViewById(R.id.spinner); 10myadapter=new ArrayAdapter< String> (this,android.R.layout.select_dialog_item,getdata(mlist)); 11myspinner.setAdapter(myadapter); 12} 13private ArrayList< String> getdata(ArrayList< String> mArrayList){ 14mArrayList.add("ENGLISH"); 15mArrayList.add("CHINA"); 16return mArrayList; 17}

ArrayAdapter主函数
Android各种Adapter用法

文章图片
Android各种Adapter用法

文章图片
1< Spinner 2android:id="@+id/spinner" 3android:layout_width="match_parent" 4android:layout_height="wrap_content" 5android:layout_weight="1" />

关于Spinner的xml布局
Android各种Adapter用法

文章图片

Context为当前的环境变量,TextViewResourceId为页面布局,List< T> 表示数据源。
 
SimpleAdapter则有很强的扩展性,可以自定义出各种效果
Android各种Adapter用法

文章图片
Android各种Adapter用法

文章图片
1 import android.support.v7.app.AppCompatActivity; 2 import android.os.Bundle; 3 import android.widget.SimpleAdapter; 4 import android.widget.Spinner; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 9 /** 10* @author LinJinTang 11*/ 12 public class MainActivity extends AppCompatActivity { 13private Spinner myspinner; 14private SimpleAdapter mysimpleadapter; 15private List< HashMap< String,Object> > myhashmaps; 16private HashMap< String,Object> mymap; 17 18@Override 19protected void onCreate(Bundle savedInstanceState) { 20super.onCreate(savedInstanceState); 21setContentView(R.layout.activity_main); 22myspinner = (Spinner) findViewById(R.id.spinner); 23mysimpleadapter=new SimpleAdapter(this,getdata(),R.layout.spinner_layout,new String[]{"image","title","info"},new int[]{R.id.img,R.id.title,R.id.info}); 24myspinner.setAdapter(mysimpleadapter); 25} 26private List< HashMap< String ,Object> > getdata(){ 27myhashmaps=new ArrayList< HashMap< String, Object> > (); 28mymap=new HashMap< String,Object> (); 29mymap.put("image",R.drawable.img1); 30mymap.put("title","CHINA"); 31mymap.put("info","I LIKE CHINA"); 32myhashmaps.add(mymap); 33 34mymap=new HashMap< String,Object> (); 35mymap.put("image",R.drawable.img2); 36mymap.put("title","FRANCE"); 37mymap.put("info","I LIKE FRANCE"); 38myhashmaps.add(mymap); 39 40mymap=new HashMap< String,Object> (); 41mymap.put("image",R.drawable.img3); 42mymap.put("title","ENGLISH"); 43mymap.put("info","I LIKE ENGLISH"); 44myhashmaps.add(mymap); 45return myhashmaps; 46} 47 }

SimpleAdapter函数代码
Android各种Adapter用法

文章图片
Android各种Adapter用法

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:orientation="horizontal"> 7 8< ImageView 9android:id="@+id/img" 10android:layout_width="100dp" 11android:layout_height="80dp" 12app:srcCompat="@mipmap/ic_launcher" /> 13 14< LinearLayout 15android:layout_width="match_parent" 16android:layout_height="match_parent" 17android:orientation="vertical"> 18 19< TextView 20android:id="@+id/title" 21android:layout_width="match_parent" 22android:layout_height="50dp" 23android:textSize="20dp" 24android:gravity="center" 25android:text="TextView" /> 26 27< TextView 28android:id="@+id/info" 29android:layout_width="match_parent" 30android:layout_height="30dp" 31android:gravity="center" 32android:text="TextView" /> 33< /LinearLayout> 34 < /LinearLayout>

SimpleApadter页面布局代码
Android各种Adapter用法

文章图片
Android各种Adapter用法

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context="angrypig.lookingstar.MainActivity"> 8 9 10< Spinner 11android:id="@+id/spinner" 12android:layout_width="match_parent" 13android:layout_height="wrap_content" 14android:layout_weight="1" /> 15 < /LinearLayout>

关于Spinner的xml布局
Android各种Adapter用法

文章图片

Android各种Adapter用法

文章图片

【Android各种Adapter用法】Context为当前的环境变量,TextViewResourceId为页面布局,List< T> 表示数据源,Form对应map的Key,to对应的是布局文件的Id。

    推荐阅读