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

作者:与时俱进 时间:2023-10-13 14:41:22 

本文实例讲述了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,listview
0
投稿

猜你喜欢

  • Springboot+ElementUi实现评论、回复、点赞功能

    2022-06-16 04:34:00
  • Android在linux下刷机教程

    2023-09-03 17:21:04
  • Kotlin Flow数据流的3种使用场景详解

    2021-06-03 09:06:18
  • C语言实现自定义扫雷游戏(递归版)

    2023-11-03 02:12:36
  • Android录制按钮源码解析

    2022-06-24 19:24:44
  • 利用Springboot+Caffeine实现本地缓存实例代码

    2022-05-31 09:13:33
  • Java Springboot 重要知识点整理汇总

    2022-03-17 01:06:57
  • Unity中的PostProcessScene实用案例深入解析

    2021-06-09 04:57:28
  • Android自定义View实现带数字的进度条实例代码

    2021-05-27 01:31:49
  • 利用POI生成EXCEL文件的方法实例

    2023-11-23 21:44:14
  • Java使用JSON传递字符串注意事项解析

    2021-11-05 13:35:37
  • Android 中在有序广播中添加自定义权限的实例

    2021-08-10 05:09:35
  • JAVA学习之一步步搭建spring框架

    2023-02-24 06:39:44
  • Spring Boot 整合 TKMybatis 二次简化持久层代码的实现

    2021-06-17 06:21:26
  • 关于MyBatis中SqlSessionFactory和SqlSession简解

    2022-02-18 13:57:37
  • java中Executor,ExecutorService,ThreadPoolExecutor详解

    2023-10-31 10:50:56
  • Jenkins自动构建部署项目到远程服务器上的方法步骤

    2023-11-25 00:21:40
  • Spring Boot调用 Shell 脚本实现看门狗功能

    2023-09-14 18:52:07
  • C++ vector数组用法及解析

    2022-09-20 01:04:25
  • Java解析Excel内容的方法

    2023-03-20 20:36:26
  • asp之家 软件编程 m.aspxhome.com