欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现在一个activity中添加多个listview的方法

程序员文章站 2024-03-07 11:47:21
本文实例讲述了android实现在一个activity中添加多个listview的方法。分享给大家供大家参考,具体如下: listview的id一般是这样的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程序设计有所帮助。