Android实现在一个activity中添加多个listview的方法
本文实例讲述了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就可以了。
参考代码:
publicclassmyActivityextendsActivityimplementsOnClickListener { privateListViewlistview_psychologicalstate,listview_weatherstate; privateListAdapterlistadapter_psychologicalstate,listadapter_weatherstate; privateArrayList<String>psychologicalstatearray=newArrayList<String>(),weatherstatearray=newArrayList<String>(); privateString[]strpsychologicalstate=newString[]{"郁闷","亢奋","压力","失眠","嗜睡","易惊恐"}; privateString[]strweatherstate=newString[]{"湿热","燥热","寒冷"}; @Override publicvoidonCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.personalizedorderview); findView(); } publicvoidfindView() { listview_psychologicalstate=(ListView)findViewById(R.id.psychologicalstatelistid); listview_weatherstate=(ListView)findViewById(R.id.weatherstatelistid); for(inti=0;i<strpsychologicalstate.length;i++) psychologicalstatearray.add(strpsychologicalstate[i]); for(inti=0;i<strweatherstate.length;i++) weatherstatearray.add(strweatherstate[i]); listadapter_psychologicalstate=newGetinflamedAdapter(this,psychologicalstatearray); listview_psychologicalstate.setAdapter(listadapter_psychologicalstate); listadapter_weatherstate=newGetinflamedAdapter(this,weatherstatearray); listview_weatherstate.setAdapter(listadapter_weatherstate); } publicclassGetinflamedAdapterextendsBaseAdapter { privateContextcontext; privateLayoutInflatermInflater; privateArrayList<String>arraylist; publicGetinflamedAdapter(Contextcontext,ArrayList<String>arraylist) { this.context=context; this.arraylist=arraylist; mInflater=(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override publicintgetCount(){ //TODOAuto-generatedmethodstub returnarraylist.size(); } @Override publicObjectgetItem(intposition){ //TODOAuto-generatedmethodstub returnarraylist.get(position); } @Override publiclonggetItemId(intposition){ //TODOAuto-generatedmethodstub returnposition; } @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ //TODOAuto-generatedmethodstub convertView=mInflater.inflate(R.layout.inflamedchild,null); ViewHolderviewHolder=null; viewHolder=newViewHolder(); 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); returnconvertView; } privateclassViewHolder { TextViewlabel; } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android编程之activity操作技巧总结》、《Android视图View技巧总结》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android开发入门与进阶教程》、《Android资源操作技巧汇总》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。