Android各種Adapter用法
阿新 • • 發佈:2017-06-20
app urn turn drawable key save 比較 logs display
ArrayAdapter比較簡單,但它只能用於顯示文字。
1 public class MainActivity extends AppCompatActivity { 2 private Spinner myspinner; 3 private ArrayList<String> mlist=new ArrayList<String>(); 4 private ArrayAdapter myadapter; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) {ArrayAdapter主函數7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 myspinner=(Spinner)findViewById(R.id.spinner); 10 myadapter=new ArrayAdapter<String>(this,android.R.layout.select_dialog_item,getdata(mlist)); 11 myspinner.setAdapter(myadapter);12 } 13 private ArrayList<String> getdata(ArrayList<String> mArrayList){ 14 mArrayList.add("ENGLISH"); 15 mArrayList.add("CHINA"); 16 return mArrayList; 17 }
1 <Spinner 2 android:id="@+id/spinner" 3 android:layout_width="match_parent" 4關於Spinner的xml布局android:layout_height="wrap_content" 5 android:layout_weight="1" />
Context為當前的環境變量,TextViewResourceId為頁面布局,List<T>表示數據源。
SimpleAdapter則有很強的擴展性,可以自定義出各種效果
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 { 13 private Spinner myspinner; 14 private SimpleAdapter mysimpleadapter; 15 private List<HashMap<String,Object>> myhashmaps; 16 private HashMap<String,Object> mymap; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 myspinner = (Spinner) findViewById(R.id.spinner); 23 mysimpleadapter=new SimpleAdapter(this,getdata(),R.layout.spinner_layout,new String[]{"image","title","info"},new int[]{R.id.img,R.id.title,R.id.info}); 24 myspinner.setAdapter(mysimpleadapter); 25 } 26 private List<HashMap<String ,Object>> getdata(){ 27 myhashmaps=new ArrayList<HashMap<String, Object>>(); 28 mymap=new HashMap<String,Object>(); 29 mymap.put("image",R.drawable.img1); 30 mymap.put("title","CHINA"); 31 mymap.put("info","I LIKE CHINA"); 32 myhashmaps.add(mymap); 33 34 mymap=new HashMap<String,Object>(); 35 mymap.put("image",R.drawable.img2); 36 mymap.put("title","FRANCE"); 37 mymap.put("info","I LIKE FRANCE"); 38 myhashmaps.add(mymap); 39 40 mymap=new HashMap<String,Object>(); 41 mymap.put("image",R.drawable.img3); 42 mymap.put("title","ENGLISH"); 43 mymap.put("info","I LIKE ENGLISH"); 44 myhashmaps.add(mymap); 45 return myhashmaps; 46 } 47 }SimpleAdapter函數代碼
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="horizontal"> 7 8 <ImageView 9 android:id="@+id/img" 10 android:layout_width="100dp" 11 android:layout_height="80dp" 12 app:srcCompat="@mipmap/ic_launcher" /> 13 14 <LinearLayout 15 android:layout_width="match_parent" 16 android:layout_height="match_parent" 17 android:orientation="vertical"> 18 19 <TextView 20 android:id="@+id/title" 21 android:layout_width="match_parent" 22 android:layout_height="50dp" 23 android:textSize="20dp" 24 android:gravity="center" 25 android:text="TextView" /> 26 27 <TextView 28 android:id="@+id/info" 29 android:layout_width="match_parent" 30 android:layout_height="30dp" 31 android:gravity="center" 32 android:text="TextView" /> 33 </LinearLayout> 34 </LinearLayout>SimpleApadter頁面布局代碼
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="angrypig.lookingstar.MainActivity"> 8 9 10 <Spinner 11 android:id="@+id/spinner" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" 14 android:layout_weight="1" /> 15 </LinearLayout>關於Spinner的xml布局
Context為當前的環境變量,TextViewResourceId為頁面布局,List<T>表示數據源,Form對應map的Key,to對應的是布局文件的Id。
Android各種Adapter用法