在一个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;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值