您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Android中如何通過自定義Adapter顯示列表”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Android中如何通過自定義Adapter顯示列表”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
在res\layout目錄下創建一個 activitylist.xml。內容如下:
這次我們不從AndroidManifest.xml中讀取Activity列表,而是使用String Array資源。在res\value\string.xml 中添加下列Array資源:
<string-array name=”activity_name”> <item>Bezier</item> <item>Brush</item> <item>Colors</item> <item>Font</item> <item>Image</item> <item>Path</item> <item>Pen</item> <item>Shape</item> <item>Transform</item> </string-array> <string-array name=”activity_info”> <item>Bezier</item> <item>Pattern ,Gradients</item> <item>Colors</item> <item>FontDemo, FontTypes</item> <item>DrawMap, JumbleImage, SeeThroughImage</item> <item>Polys, Paths</item> <item>Lines, Dashes, LineCap,LineJoin</item> <item>Oval ,Pear ,Shape2DDemo</item> <item>Transform</item> </string-array>
定義了這些資源后,可以在程序中使用自定義Adapter來顯示列表:
class ActivityInfo{ int iconIndex; String activityName; String activityInfo; } class ActivityInfoAdapter extends ArrayAdapter<ActivityInfo>{ int resource; public ActivityInfoAdapter(Context context, int resourceId, List<ActivityInfo> objects) { super(context, resourceId, objects); resource=resourceId; } @Override public View getView(int position,View ConvertView,ViewGroup parent){ LinearLayout activityInfoView; ActivityInfo activityInfo=getItem(position); String activity_Name=activityInfo.activityName; String activity_Info=activityInfo.activityInfo; int iconIndex=activityInfo.iconIndex; if(ConvertView==null){ activityInfoView=new LinearLayout(getContext()); String inflater=Context.LAYOUT_INFLATER_SERVICE; LayoutInflater vi; vi=(LayoutInflater)getContext().getSystemService(inflater); vi.inflate(resource, activityInfoView,true); }else{ activityInfoView=(LinearLayout)ConvertView; } TextView activity_NameView =(TextView)activityInfoView.findViewById(R.id.activityName); TextView activity_InfoView =(TextView)activityInfoView.findViewById(R.id.activityInfo); ImageView iconView =(ImageView)activityInfoView.findViewById(R.id.iconImage); activity_NameView.setText(activity_Name); activity_InfoView.setText(activity_Info); iconView.setImageResource(iconIndex); return activityInfoView; } } public class AndroidGraphics2DTutorial extends ListActivity { private ArrayList<ActivityInfo> activityInfos =new ArrayList<ActivityInfo>(); private ActivityInfoAdapter aa; private final static String packgeName="com.pstreets.graphics2d"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Resources res = getResources(); String[] activity_Names = res.getStringArray(R.array.activity_name); String[] activity_Infos = res.getStringArray(R.array.activity_info); for(int i=0;i<activity_Names.length;i++){ ActivityInfo activityInfo=new ActivityInfo(); activityInfo.activityName=activity_Names[i]; activityInfo.activityInfo=activity_Infos[i]; activityInfo.iconIndex=R.drawable.icon1+i; activityInfos.add(activityInfo); } aa=new ActivityInfoAdapter(this,R.layout.activitylist,activityInfos); setListAdapter(aa); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ActivityInfo activityInfo = (ActivityInfo) l.getItemAtPosition(position); Intent intent = new Intent(); intent.setClassName(this, packgeName+".example." +activityInfo.activityName); startActivity(intent); } }
類ActivityInfo定義列表每個元素的Data Model,為Activity的Icon資源ID,Activity Name以及Activity Info.
類ActivityInfoAdapter為自定義Adapter,關鍵的是public View getView(int position,View ConvertView,ViewGroup parent)。這個函數返回用來顯示沒個類別元素的View的示例。例子中為activitylist.xml 對應的Layout。
讀到這里,這篇“Android中如何通過自定義Adapter顯示列表”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。