码迷,mamicode.com
首页 > 移动开发 > 详细

Android各种Adapter用法

时间:2017-06-20 22:32:27      阅读:449      评论:0      收藏:0      [点我收藏+]

标签:hid   lead   inner   encoding   author   sch   english   info   data   

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) {
 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     }
ArrayAdapter主函数
技术分享
1   <Spinner
2         android:id="@+id/spinner"
3         android:layout_width="match_parent"
4         android:layout_height="wrap_content"
5         android:layout_weight="1" />
关于Spinner的xml布局

技术分享

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用法

标签:hid   lead   inner   encoding   author   sch   english   info   data   

原文地址:http://www.cnblogs.com/AngryPigs/p/7056423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!