Android實現在一個activity中添加多個listview的方法_Android

來源:互聯網
上載者:User

本文執行個體講述了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[]{"濕熱","燥熱","寒冷"};@Overridepublic 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);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn arraylist.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn arraylist.get(position);}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubconvertView = 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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.