Androidプログラミングにおける常用アダプター及びカスタムアダプターの使用例の分析


本明細書の例は、Androidプログラミングにおける一般的なアダプタおよびカスタムアダプタの使用例を示す。皆さんに参考にしてあげます。具体的には以下の通りです。
一、アダプター
名前の通り、いくつかのデータを適当にして、Viewで表示しやすいようにします。界面データバインディングの理解であると考えられる。操作されているデータは一般に、行列、チェーン、データベース、集合などの複雑なデータです。アダプターはディスプレイのように複雑なものを人が納得できるように見せます。
アダプターはどのようにして得られたデータを処理して表示しますか?実はとても簡単です。アダプターというのも一つの種類です。種類の中で父のような方法を実現しました。

publicint getCount() //       
public Object getItem(int position)//  position        
public long getItemId(int position)//        ID
//                    ,       ,           
//             ,                       
publicView getView(int position, View convertView, ViewGroup parent) 

私たちがよく使うアダプターは全部で三つあります。もちろんカスタムアダプターは含まれていません。どれを使ったかは知っています。
それはアルラ・アダプター、SimpleAdapter、SimpleCurrsorAdapterの三つです。彼らはすべてBaseAdapterに受け継がれています。
二、前の二つのアダプターに対して、彼らのデータソースはString[]またはListにほかならない。二つの例の一つを挙げます。
一例として、配列はデータソースとして、充填されているのはArayAdapterである。

public class Example extends ListActivity{
 String[] sex = new String(){" "," "}//   
 ArrayAdapter<String> adapter;//     ,     
 public voidonCreate(Bundle SavedInstanceState){
 super.onCreate(SavedInstanceStat);
 //           ,             ,
 //this.android.R.Layout.Simple_List_Item_1   
 //                       .    
 adapter=newArrayAdapter<String>(this.android.R.Layout.Simple_List_Item_1,sex);
 this.setAdapter(adapter);//       ,                  。
 }
}

例二:Listはデータソースとして、パディングはSimpleAdapterである。

ListView list = (ListView)findViewById(R.id.MyListView);  
//      ,      
ArrayList<HashMap<String, String>> mylist = newArrayList<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);
  }
 //     ,  ===》ListItem
  SimpleAdapter mSchedule = new SimpleAdapter(this, //      mylist,//      R.layout.my_listitem,//ListItem XML   //     ListItem        
  new String[]{"ItemTitle", "ItemText"}, //ListItem XML       TextView ID new int[] {R.id.ItemTitle,R.id.ItemText});
  //      
  list.setAdapter(mSchedule);
}

三、二つの例を話しても難しくないです。全部いくつかの私達がよく見ている使い方です。SimpleCurrsorAdapterはどのように使いますか?SimpleCurrsorAdapterは主にデータベースに使います。そのデータソースは主にデータベースで調べられたCurerです。次の例を見てみます。

String uriString = "content://contacts/people/";
Cursor myCursor =managedQuery(Uri.parse(uriString), null, null, null, null);
String[] fromColumns = new String[]{People.NUMBER, People.NAME};
int[] toLayoutIDs = new int[] {R.id.nameTextView, R.id.numberTextView};
SimpleCursorAdapter myAdapter;
myAdapter=newSimpleCursorAdapter(this,R.layout.simplecursorlayout,myCursor,fromColumns,
toLayoutIDs);//        、  layout  ,         :         
//   ,   (    )    View    ID,         
  。
myListView.setAdapter(myAdapter);

私たちは一つのラベルをListViewに結び付けて、カスタムのlayout表示を使ってItemを表示します。
 
次に私達は自分のアダプターを定義します。
なぜ自分のアダプターを定義するのかというと、私たちが他の表現方法を使いたい時や、私たちが必要としている表現方法をDIYしなければならないからです。
まず、クラスを定義して、BaseAdapterから継承して、その中のいくつかの方法を実現させます。このカスタムアダプターがいいです。
中のいくつかの方法を上で紹介しましたが、ここでは詳しく説明しません。

public class ImageAdapter extendsBaseAdapter {
  private Context mcontext;
  };
  //           ,        ,       。
public ImageAdapter(Integer[] imgIds,Context c){
  mcontext=c;
  imageIds=imgIds;
  }
publicint getCount() {
  // TODO Auto-generated method stub
  return imageIds.length;
  }
publicObject getItem(int position) {
  // TODO Auto-generated method stub
  return null;
  }
publiclong getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
  }
//         ,       ,         
publicView getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ImageView imageview = newImageView(mcontext);
  imageview.setImageResource(imageIds[position]);
  imageview.setLayoutParams(newGallery.LayoutParams(120,120));
  imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
  return imageview;
  }
}

最後にこのアダプターが使えます。
ここで述べたように、皆さんのAndroidプログラムの設計に役に立ちます。