Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法
本文實(shí)例講述了Android實(shí)現(xiàn)在一個(gè)activity中添加多個(gè)listview的方法。分享給大家供大家參考,具體如下:
listview的id一般是這樣的android:id="@id/android:list"。請(qǐng)注意,這時(shí)的Activity是ListActivity,在這樣的Activity中到目前為止,我沒(méi)有發(fā)現(xiàn)添加多個(gè)activity的方法。要添加多個(gè)listview,所用的Activity要是一般的Activity,listview的id也要自己定義,改成android:id="@+id/mylist"。然后像使用一般的控件一樣,初始化就可以了。至于adapter我是繼承了BaseAdapter,為每個(gè)listview定義一個(gè)adapter,然后對(duì)應(yīng)的設(shè)置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; } }
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android使用自定義控件HorizontalScrollView打造史上最簡(jiǎn)單的側(cè)滑菜單
側(cè)滑菜單一般都會(huì)自定義ViewGroup,然后隱藏菜單欄,當(dāng)手指滑動(dòng)時(shí),通過(guò)Scroller或者不斷的改變leftMargin等實(shí)現(xiàn);多少都有點(diǎn)復(fù)雜,完成以后還需要對(duì)滑動(dòng)沖突等進(jìn)行處理,今天給大家?guī)?lái)一個(gè)簡(jiǎn)單的實(shí)現(xiàn),史上最簡(jiǎn)單有點(diǎn)夸張,但是的確是我目前遇到過(guò)的最簡(jiǎn)單的一種實(shí)現(xiàn)2016-02-02Android SpannableString設(shè)置超鏈接、顏色、字體等屬性
這篇文章主要介紹了Android SpannableString設(shè)置超鏈接、顏色、字體等屬性的相關(guān)資料,需要的朋友可以參考下2017-01-01android實(shí)現(xiàn)輪播圖引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)輪播圖引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09AndroidStudio 使用過(guò)程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過(guò)程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說(shuō)明,如何處理出現(xiàn)這種問(wèn)題,有需要的小伙伴可以參考下2016-09-09Android實(shí)現(xiàn)環(huán)信修改頭像和昵稱(chēng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)環(huán)信修改頭像和昵稱(chēng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02在AndroidManifest.xml中uses-sdk內(nèi)屬性意思
本文為大家講解下minSdkVersion、targetSdkVersion、maxSdkVersion、target API level四個(gè)數(shù)值的意思與區(qū)別,感興趣的朋友可以參考下哈2013-06-06