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

Android Adapter的一些记录

时间:2014-06-12 18:27:55      阅读:503      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   class   blog   code   

 一、摘要

  An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

  这是Android官方对Adapter的定义。这个关系可以描述为下面一个关系图,

  bubuko.com,布布扣

 

二、Adapter的继承关系

  bubuko.com,布布扣 bubuko.com,布布扣

 

三,样例

  这里附上北航《Android编程宝典》的一个例子,搭配ListView说明Adapter怎么样去扮演这个中间人,为View层提供数据。

  1,定义3个layout文件,分别是activity_main.xml,fragment_main.xml,my_listitem.xml。

  activity_main.xml

bubuko.com,布布扣
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="so.byj.example.mytestapp.MainActivity"
    tools:ignore="MergeRootFrame" />
bubuko.com,布布扣

  fragment_main.xml

bubuko.com,布布扣
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout01"
    android:baselineAligned="false"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView android:id="@+id/myListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>
    
</LinearLayout>
bubuko.com,布布扣

  my_listitem.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myListItem"
    android:paddingBottom="3dip"
    android:paddingLeft="10dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <TextView android:id="@+id/itemTitle"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textSize="30sp" />
    
    <TextView android:id="@+id/itemText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
bubuko.com,布布扣

 

  2,编写MainActivity,代码如下

bubuko.com,布布扣
package so.byj.example.mytestapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    
            
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            
            ListView list = (ListView) rootView.findViewById(R.id.myListView);
            
            ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
            for(int i=0;i<30;i++)
            {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("ItemTitle", "This is Title.....");
                map.put("ItemText", "This is text.....");
                mylist.add(map);
            }
            
            SimpleAdapter mSchedule = new SimpleAdapter(getActivity(),
                                                        mylist,
                                                        R.layout.my_listitem, // 这里定义了数据填充的样式
                                                        new String[] {"ItemTitle", "ItemText"}, 
                                                        new int[] {R.id.itemTitle,R.id.itemText});
            
            list.setAdapter(mSchedule);
            
            return rootView;
        }
    }

}
bubuko.com,布布扣

  如上面黑体部分,先把待填充数据的ListView拿出来,然后实例一个Adapter对ListView进行数据填充。

 

 

Android Adapter的一些记录,布布扣,bubuko.com

Android Adapter的一些记录

标签:des   android   style   class   blog   code   

原文地址:http://www.cnblogs.com/ultrazb/p/3781726.html

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