Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現在一個activity中添加多個listview的方法

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

編輯:關於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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved