Android应用开发基础--Adapter

匿名 (未验证) 提交于 2019-12-03 00:42:01

Android――Adapter

  1. 基本概念

    AdapterAndroidAdapterUI

  2. 常用Adapter

    ListAdapterSpinnerAdapterListAdapterAbsListViewSpinnerAdapterAbsSpinnerAdapter

    BaseAdapterArrayAdapterSimpleAdapterSimpleCursorAdapterAdapterBaseAdapterAdapter

    ArrayAdapter<T>

    (T)

    BaseAdapter

    AdapterListView(ListAdapter)Spinner(SpinnerAdapter)

    CursorAdapter

    CursorListView

    HeaderViewListAdapter

    ListView HeaderListView

    ListAdapter

    AdapterListViewts_

    ResourceCursorAdapter

    XMLviews

    SimpleAdapter

    XMLview

    SimpleCursorAdapter

    cursorXMLTextViewImageView

    SpinnerAdapter

    AdapterSpinnerts_

    WrapperListAdapter

    AdapterAdapter

  3. 使用举例
ArrayAdapterListView

XMLListViewActivityAdapterAdapterDemoActivity

Java

package com.example.adapterdemo;  import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView;  public class AdapterDemo extends Activity {         @Override     protected void onCreate(Bundle savedInstanceState)     {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_adapterdemo);         String[] data= {"北京","上海","广州","西安","南京","深圳","杭州"};                  ListView myLV = (ListView)findViewById(R.id.myListView);         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.mylv_item,data);                  myLV.setAdapter(adapter);     } }

ItemTextViewItem

<?xml version="1.0" encoding="utf-8"?> <TextView     android:id="@+id/mylv_item"     android:textSize="18sp"     android:layout_width="match_parent"     android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> </TextView>

<RelativeLayout 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"     tools:context="${relativePackage}.${activityClass}" >     <ListView         android:id="@+id/myListView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/> </RelativeLayout>
ListActivity

ListActivityActivityListActivityItem

setContentView()AdapterListActivity

ListActivityListViewListActivityListViewListActivity璁剧疆AdapteronCreate()setContentView(int layoutId)id"@+id/android:list"android.R.id.lsitListView

XmlXMLjava

package com.example.listactivitydemo;  import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter;  public class ListActivityDemo extends ListActivity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         //setContentView(R.layout.activity_ts_list); //不需要布局文件,如果需要定制布局文件,则需要按上述说明,按规定指定@+id/android:list,上述详见说明                  String [] str = {"HeFie","BeiJing"};                  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,str);         setListAdapter(adapter);             } }

SimpleAdapterListView

ListViewListViewListViewSimpleAdapter

package com.example.simpleadapter_demo;  import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;  import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import android.widget.SimpleAdapter;  public class SimpleAdapter_Demo extends Activity {          private String[] nameStrings = new String[]{"小红","小明","小华"};     private String[] descsStrings = new String[]{"红红","明明","华华",};     private int[] imageIds = new int[]{R.drawable.libai,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.tiger};      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_simple_adapter__demo);                  List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();         for(int i=0; i<nameStrings.length; i++)         {             Map<String, Object> listItem = new HashMap<String, Object>();             listItem.put("header", imageIds[i]);             listItem.put("personName", nameStrings[i]);             listItem.put("decs", descsStrings[i]);             listItems.add(listItem);         }                  SimpleAdapter simpleAdapter = new SimpleAdapter(this,                 listItems,                  R.layout.simple_item,                  new String[] {"personName","header","decs"},                  new int[]{R.id.name,R.id.header,R.id.desc});                  ListView mylistview = (ListView)findViewById(R.id.mylistview);         mylistview.setAdapter(simpleAdapter);             } }

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="horizontal"     android:layout_width="match_parent"     android:layout_height="wrap_content"> <!-- 定义一个ImageView,用于作为列表项的一部分。 --> <ImageView android:id="@+id/header"     android:layout_width="wrap_content"     android:layout_height="wrap_content"      android:paddingLeft="10dp"     /> <LinearLayout     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="wrap_content"> <!-- 定义一个TextView,用于作为列表项的一部分。 --> <TextView android:id="@+id/name"     android:layout_width="wrap_content"      android:layout_height="wrap_content"     android:textSize="20dp"     android:textColor="#f0f"     android:paddingLeft="10dp"     /> <!-- 定义一个TextView,用于作为列表项的一部分。 --> <TextView android:id="@+id/desc"     android:layout_width="wrap_content"      android:layout_height="wrap_content"     android:textSize="14dp"     android:paddingLeft="10dp"     /> </LinearLayout> </LinearLayout>

ListView

<RelativeLayout 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"     tools:context="${relativePackage}.${activityClass}" >          <ListView         android:id="@+id/mylistview"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         /> </RelativeLayout>
BaseAdapterListView

BaseAdapterAdapter

Java

package com.example.baseadapter;  import java.util.List;  import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView;  public class BaseAdapter_Demo extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_base_adapter__demo);                  ListView myList = (ListView) findViewById(R.id.myList);         BaseAdapter adapter = new BaseAdapter()         {             @Override             public int getCount()             {                 // 指定一共包含40个选项                 return 40;             }             @Override             public Object getItem(int position)             {                 return null;             }             // 重写该方法,该方法的返回值将作为列表项的ID             @Override             public long getItemId(int position)             {                 return position;             }             // 重写该方法,该方法返回的View将作为列表框             @Override             public View getView(int position                     , View convertView , ViewGroup parent)             {                 // 创建一个LinearLayout,并向其中添加2个组件                 LinearLayout line = new LinearLayout(BaseAdapter_Demo.this);                 line.setOrientation(0);                 ImageView image = new ImageView(BaseAdapter_Demo.this);                 image.setImageResource(R.drawable.ic_launcher);                 TextView text = new TextView(BaseAdapter_Demo.this);                 text.setText("第" + (position +1 ) + "个列表项");                 text.setTextSize(20);                 text.setTextColor(Color.RED);                 line.addView(image);                 line.addView(text);                 // 返回LinearLayout实例                 return line;             }         };         myList.setAdapter(adapter);              } }

XML

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="match_parent" > <ListView     android:id="@+id/myList"     android:layout_width="match_parent"     android:layout_height="match_parent"/> </LinearLayout>

原文:https://www.cnblogs.com/tsts/p/9347332.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!