當前位置:首頁 > IT技術 > 移動平臺 > 正文

【Android】Android適配器之ArrayAdapter、SimpleAdapter和BaseAdapter的簡單用法
2021-09-08 11:44:32

?



ArrayAdapter、SimpleAdapter和BaseAdapter的一些簡短代碼片段,希望用時方便想起其用法。

1.ArrayAdapter

?

只可以簡單的顯示一行文本

代碼片段:

?

?
  1. ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(??
  2. ????????????????this,??
  3. ????????????????R.layout.item,//只能有一個定義了id的TextView??
  4. ????????????????data);//data既可以是數(shù)組,也可以是List集合??


?

2.SimpleAdapter

?

可以顯示比較復雜的列表,包括每行顯示圖片、文字等,但不能對列表進行后期加工(在java代碼中加工),
也是只是單純的負責顯示(當然可以設計復雜點的布局來顯示復雜列表),例如,每行顯示不同背景等。

代碼片段:

?

?
  1. List<Map<String,Object>>?mData=?new?ArrayList<Map<String,Object>>();;??
  2. for(int?i?=0;?i?<?lengh;?i++)?{??????
  3. ????Map<String,Object>?item?=?new?HashMap<String,Object>();??????
  4. ????item.put("image",?R.drawable.portrait);??????
  5. ????item.put("title",?mListTitle[i]);??????
  6. ????item.put("text",?mListStr[i]);??????
  7. ????mData.add(item);???????
  8. }??????
  9. SimpleAdapter?adapter?=?new?SimpleAdapter(??
  10. ????????????????this,??
  11. ????????????????mData,??
  12. ????????????????R.layout.item,??????
  13. ????????????????new?String[]{"image","title","text"},??
  14. ????????????????new?int[]{R.id.image,R.id.title,R.id.text});?????

?

?

3.BaseAdapter

?

可以實現(xiàn)復雜的列表布局,由于BaseAdapter是一個抽象類,使用該類需要自己寫一個適配器繼承該類,
正是由于繼承了該類,需要我們重寫一些方法,讓我們可以在代碼里控制列表的樣式,更加靈活。

代碼片段:

?

?
  1. private?class?MyListAdapter?extends?BaseAdapter{????
  2. ????????private?Context?mContext;????
  3. ????????private?int[]?colors=new?int[]{0xff626569,0xff4f5257?};????
  4. ?????????public?MyListAdapter(Context?context){????
  5. ?????????????mContext=context;????
  6. ?????????}????
  7. ????????@Override????
  8. ????????public?int?getCount()?{????
  9. ????????????//?TODO?Auto-generated?method?stub????
  10. ????????????return?mListText.length;????
  11. ????????}????
  12. ????
  13. ????????@Override????
  14. ????????public?Object?getItem(int?position)?{????
  15. ????????????//?TODO?Auto-generated?method?stub????
  16. ????????????return?position;????
  17. ????????}????
  18. ????
  19. ????????@Override????
  20. ????????public?long?getItemId(int?position)?{????
  21. ????????????//?TODO?Auto-generated?method?stub????
  22. ????????????return?position;????
  23. ????????}????
  24. ????
  25. ????????@Override????
  26. ????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{????
  27. ????????????ImageView?image=null;??//這些控件可以單獨封裝成一個類(Holder),便與優(yōu)化??
  28. ????????????TextView?title=null;????
  29. ????????????TextView??content=null;????
  30. ????????????if(convertView==null){????
  31. ????????????????convertView=LayoutInflater.from(mContext).inflate(R.layout.colorlist,?null);????
  32. ????????????????image=(ImageView)?convertView.findViewById(R.id.color_image);????
  33. ????????????????title=(TextView)?convertView.findViewById(R.id.color_title);????
  34. ????????????????content=(TextView)?convertView.findViewById(R.id.color_text);????
  35. ????????????}???
  36. ????????????int?colorPos=position%colors.length;????
  37. ????????????convertView.setBackgroundColor(colors[colorPos]);????
  38. ????????????title.setText(mListTitle[position]);????
  39. ????????????content.setText(mListText[position]);????
  40. ????????????image.setImageResource(R.drawable.portrait);????
  41. ????????????????
  42. ????????????return?convertView;????
  43. ????????}????
  44. ????????????
  45. ????}????
  46. ??
  47. --------------------------下面樣例列表頁的控件單獨封裝成了一個類(Holder),便與優(yōu)化-----??
  48. ??
  49. ??
  50. ?public?class?MyBaseAdapter?extends?BaseAdapter{??
  51. ???????
  52. ???????????private?LayoutInflater?mInflater;??
  53. ????????????public?MyAdapter(Context?context){??
  54. ???????????????this.mInflater?=?LayoutInflater.from(context);??
  55. ??????????}??
  56. ????????????@Override??
  57. ???????????public?int?getCount()?{??
  58. ????????????????//?TODO?Auto-generated?method?stub??
  59. ????????????????return?mData.size();??
  60. ????????????}??
  61. ???????
  62. ????????????@Override??
  63. ????????????public?Object?getItem(int?arg0)?{??
  64. ????????????????//?TODO?Auto-generated?method?stub??
  65. ????????????????return?null;??
  66. ???????????}??
  67. ???????
  68. ????????????@Override??
  69. ????????????public?long?getItemId(int?arg0)?{??
  70. ????????????????//?TODO?Auto-generated?method?stub??
  71. ????????????????return?0;??
  72. ????????????}??
  73. ???????
  74. ????????????@Override??
  75. ????????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  76. ???????????????????
  77. ????????????????ViewHolder?holder?=?null;??
  78. ????????????????if?(convertView?==?null)?{??
  79. ????????????????????holder=new?ViewHolder();???
  80. ????????????????????convertView?=?mInflater.inflate(R.layout.vlist2,?null);??
  81. ????????????????????holder.img?=?(ImageView)convertView.findViewById(R.id.img);??
  82. ????????????????????holder.title?=?(TextView)convertView.findViewById(R.id.title);??
  83. ????????????????????holder.info?=?(TextView)convertView.findViewById(R.id.info);??
  84. ????????????????????holder.viewBtn?=?(Button)convertView.findViewById(R.id.view_btn);??
  85. ????????????????????convertView.setTag(holder);??
  86. ???????????????????????
  87. ????????????????}else?{???????????????????????
  88. ????????????????????holder?=?(ViewHolder)convertView.getTag();??
  89. ????????????????}??
  90. ????????????????holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));??
  91. ????????????????holder.title.setText((String)mData.get(position).get("title"));??
  92. ????????????????holder.info.setText((String)mData.get(position).get("info"));??
  93. ????????????????//給每一個列表后面的按鈕添加響應事件??
  94. ????????????????holder.viewBtn.setOnClickListener(new?View.OnClickListener()?{??
  95. ????????????????????@Override??
  96. ????????????????????public?void?onClick(View?v)?{??
  97. ????????????????????????showInfo();??????????????????
  98. ????????????????????}??
  99. ????????????????});??
  100. ??
  101. ????????????????return?convertView;??
  102. ????????????}??
  103. ????????------------??
  104. ????????public?final?class?ViewHolder{??
  105. ????????????public?ImageView?img;??
  106. ????????????public?TextView?title;??
  107. ????????????public?TextView?info;??
  108. ????????????public?Button?viewBtn;??
  109. ????????}??


?

?

?

原文來源:javascript:void(0)

1.ArrayAdapter

?

只可以簡單的顯示一行文本

代碼片段:

?

?
  1. ArrayAdapter<String>?adapter?=?new?ArrayAdapter<String>(??
  2. ????????????????this,??
  3. ????????????????R.layout.item,//只能有一個定義了id的TextView??
  4. ????????????????data);//data既可以是數(shù)組,也可以是List集合??



?

2.SimpleAdapter

?

可以顯示比較復雜的列表,包括每行顯示圖片、文字等,但不能對列表進行后期加工(在java代碼中加工),
也是只是單純的負責顯示(當然可以設計復雜點的布局來顯示復雜列表),例如,每行顯示不同背景等。

代碼片段:

?

?
  1. List<Map<String,Object>>?mData=?new?ArrayList<Map<String,Object>>();;??
  2. for(int?i?=0;?i?<?lengh;?i++)?{??????
  3. ????Map<String,Object>?item?=?new?HashMap<String,Object>();??????
  4. ????item.put("image",?R.drawable.portrait);??????
  5. ????item.put("title",?mListTitle[i]);??????
  6. ????item.put("text",?mListStr[i]);??????
  7. ????mData.add(item);???????
  8. }??????
  9. SimpleAdapter?adapter?=?new?SimpleAdapter(??
  10. ????????????????this,??
  11. ????????????????mData,??
  12. ????????????????R.layout.item,??????
  13. ????????????????new?String[]{"image","title","text"},??
  14. ????????????????new?int[]{R.id.image,R.id.title,R.id.text});?????

?

?

3.BaseAdapter

?

可以實現(xiàn)復雜的列表布局,由于BaseAdapter是一個抽象類,使用該類需要自己寫一個適配器繼承該類,
正是由于繼承了該類,需要我們重寫一些方法,讓我們可以在代碼里控制列表的樣式,更加靈活。

代碼片段:

?

?
  1. private?class?MyListAdapter?extends?BaseAdapter{????
  2. ????????private?Context?mContext;????
  3. ????????private?int[]?colors=new?int[]{0xff626569,0xff4f5257?};????
  4. ?????????public?MyListAdapter(Context?context){????
  5. ?????????????mContext=context;????
  6. ?????????}????
  7. ????????@Override????
  8. ????????public?int?getCount()?{????
  9. ????????????//?TODO?Auto-generated?method?stub????
  10. ????????????return?mListText.length;????
  11. ????????}????
  12. ????
  13. ????????@Override????
  14. ????????public?Object?getItem(int?position)?{????
  15. ????????????//?TODO?Auto-generated?method?stub????
  16. ????????????return?position;????
  17. ????????}????
  18. ????
  19. ????????@Override????
  20. ????????public?long?getItemId(int?position)?{????
  21. ????????????//?TODO?Auto-generated?method?stub????
  22. ????????????return?position;????
  23. ????????}????
  24. ????
  25. ????????@Override????
  26. ????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{????
  27. ????????????ImageView?image=null;??//這些控件可以單獨封裝成一個類(Holder),便與優(yōu)化??
  28. ????????????TextView?title=null;????
  29. ????????????TextView??content=null;????
  30. ????????????if(convertView==null){????
  31. ????????????????convertView=LayoutInflater.from(mContext).inflate(R.layout.colorlist,?null);????
  32. ????????????????image=(ImageView)?convertView.findViewById(R.id.color_image);????
  33. ????????????????title=(TextView)?convertView.findViewById(R.id.color_title);????
  34. ????????????????content=(TextView)?convertView.findViewById(R.id.color_text);????
  35. ????????????}???
  36. ????????????int?colorPos=position%colors.length;????
  37. ????????????convertView.setBackgroundColor(colors[colorPos]);????
  38. ????????????title.setText(mListTitle[position]);????
  39. ????????????content.setText(mListText[position]);????
  40. ????????????image.setImageResource(R.drawable.portrait);????
  41. ????????????????
  42. ????????????return?convertView;????
  43. ????????}????
  44. ????????????
  45. ????}????
  46. ??
  47. --------------------------下面樣例列表頁的控件單獨封裝成了一個類(Holder),便與優(yōu)化-----??
  48. ??
  49. ??
  50. ?public?class?MyBaseAdapter?extends?BaseAdapter{??
  51. ???????
  52. ???????????private?LayoutInflater?mInflater;??
  53. ????????????public?MyAdapter(Context?context){??
  54. ???????????????this.mInflater?=?LayoutInflater.from(context);??
  55. ??????????}??
  56. ????????????@Override??
  57. ???????????public?int?getCount()?{??
  58. ????????????????//?TODO?Auto-generated?method?stub??
  59. ????????????????return?mData.size();??
  60. ????????????}??
  61. ???????
  62. ????????????@Override??
  63. ????????????public?Object?getItem(int?arg0)?{??
  64. ????????????????//?TODO?Auto-generated?method?stub??
  65. ????????????????return?null;??
  66. ???????????}??
  67. ???????
  68. ????????????@Override??
  69. ????????????public?long?getItemId(int?arg0)?{??
  70. ????????????????//?TODO?Auto-generated?method?stub??
  71. ????????????????return?0;??
  72. ????????????}??
  73. ???????
  74. ????????????@Override??
  75. ????????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??
  76. ???????????????????
  77. ????????????????ViewHolder?holder?=?null;??
  78. ????????????????if?(convertView?==?null)?{??
  79. ????????????????????holder=new?ViewHolder();???
  80. ????????????????????convertView?=?mInflater.inflate(R.layout.vlist2,?null);??
  81. ????????????????????holder.img?=?(ImageView)convertView.findViewById(R.id.img);??
  82. ????????????????????holder.title?=?(TextView)convertView.findViewById(R.id.title);??
  83. ????????????????????holder.info?=?(TextView)convertView.findViewById(R.id.info);??
  84. ????????????????????holder.viewBtn?=?(Button)convertView.findViewById(R.id.view_btn);??
  85. ????????????????????convertView.setTag(holder);??
  86. ???????????????????????
  87. ????????????????}else?{???????????????????????
  88. ????????????????????holder?=?(ViewHolder)convertView.getTag();??
  89. ????????????????}??
  90. ????????????????holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));??
  91. ????????????????holder.title.setText((String)mData.get(position).get("title"));??
  92. ????????????????holder.info.setText((String)mData.get(position).get("info"));??
  93. ????????????????//給每一個列表后面的按鈕添加響應事件??
  94. ????????????????holder.viewBtn.setOnClickListener(new?View.OnClickListener()?{??
  95. ????????????????????@Override??
  96. ????????????????????public?void?onClick(View?v)?{??
  97. ????????????????????????showInfo();??????????????????
  98. ????????????????????}??
  99. ????????????????});??
  100. ??
  101. ????????????????return?convertView;??
  102. ????????????}??
  103. ????????------------??
  104. ????????public?final?class?ViewHolder{??
  105. ????????????public?ImageView?img;??
  106. ????????????public?TextView?title;??
  107. ????????????public?TextView?info;??
  108. ????????????public?Button?viewBtn;??
  109. ????????}??



?

?

?

本文摘自 :https://blog.51cto.com/u

開通會員,享受整站包年服務立即開通 >