編輯:關於Android編程
本文實例講述了Android實現在一個activity中添加多個listview的方法。分享給大家供大家參考,具體如下:
listview的id一般是這樣的android:id="@id/android:list"。請注意,這時的Activity是ListActivity,在這樣的Activity中到目前為止,我沒有發現添加多個activity的方法。要添加多個listview,所用的Activity要是一般的Activity,listview的id也要自己定義,改成android:id="@+id/mylist"。然後像使用一般的控件一樣,初始化就可以了。至於adapter我是繼承了BaseAdapter,為每個listview定義一個adapter,然後對應的設置adapter就可以了。
參考代碼:
public class myActivity extends Activity implements OnClickListener { private ListViewlistview_psychologicalstate,listview_weatherstate; private ListAdapter listadapter_psychologicalstate,listadapter_weatherstate; private ArrayList<String> psychologicalstatearray = new ArrayList<String>(),weatherstatearray = new ArrayList<String>(); private String [] strpsychologicalstate = new String[]{"郁悶","亢奮","壓力","失眠","嗜睡","易驚恐"}; private String [] strweatherstate = new String[]{"濕熱","燥熱","寒冷"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.personalizedorderview); findView(); } public void findView() { listview_psychologicalstate = (ListView) findViewById(R.id.psychologicalstatelistid); listview_weatherstate = (ListView) findViewById(R.id.weatherstatelistid); for(int i = 0;i<strpsychologicalstate.length;i++) psychologicalstatearray.add(strpsychologicalstate[i]); for(int i = 0;i<strweatherstate.length;i++) weatherstatearray.add(strweatherstate[i]); listadapter_psychologicalstate = new GetinflamedAdapter(this,psychologicalstatearray); listview_psychologicalstate.setAdapter(listadapter_psychologicalstate); listadapter_weatherstate = new GetinflamedAdapter(this,weatherstatearray); listview_weatherstate.setAdapter(listadapter_weatherstate); } public class GetinflamedAdapter extends BaseAdapter { private Context context; private LayoutInflater mInflater; private ArrayList<String> arraylist; public GetinflamedAdapter(Context context,ArrayList<String> arraylist ) { this.context=context; this.arraylist = arraylist; mInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { // TODO Auto-generated method stub return arraylist.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return arraylist.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = mInflater.inflate(R.layout.inflamedchild, null); ViewHolder viewHolder = null; viewHolder = new ViewHolder(); viewHolder.label = (TextView) convertView.findViewById(R.id.bodystateid1); viewHolder.label.setText(arraylist.get(position).toString()); viewHolder.label.setTextColor(Color.GREEN); viewHolder.label.setTextSize(22); convertView.setTag(viewHolder); return convertView; } private class ViewHolder { TextView label; } }
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android視圖View技巧總結》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
Android 5.0之後Android新增加的兩個UI控件RecyclerView,CardView。RecyclerView可以看出是ListView的加強版,能夠更
(一)概述本章給大家帶來的是Android中的Menu(菜單),而在Android中的菜單有如下幾種:OptionMenu:選項菜單,android中最常見的菜單,通過M
前言通過“計算器”應用我們已經熟悉了安卓應用開發的大致流程,具備了開發的初步知識。接下來,我們將開始制作一個“視頻播放器”
在Android中,視圖控件大致被分為兩類,即ViewGroup和View,ViewGroup控件作為父控件,包含並管理著子View,通過ViewGroup和View便形