日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

listActivity和ExpandableListActivity的简单用法

發布時間:2023/12/13 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 listActivity和ExpandableListActivity的简单用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  今天自己簡單的總結了listActivity和ExpandableListActivity二者的簡單用法。

  首先,先說一下listActivity的用法:

  ListActivity是一個綁定到一個數據源,并且用來顯示這一串數據的Activity。ListActivity擁有一個listview對象來實現數據源的綁定與顯示,通常會是一個array或者一個擁有查詢結果的cursor.ListActivity本身有一個默認的layout,其中包含一個全屏的list。如果用默認的layout,你必須要在onCreate()中注釋掉setContentView()那一句。但是如果你如果你想要定制自己的layout你可以創建一個你自己的layout文件,并且在onCreate()中調用setContentView()來指定這個layout.,需要注意的是你自己的layout中必須用到系統給定的id為"@android:id/list"的ListView。

  下面是一個簡單的例子,運行結果如下:

activityde 代碼如下:

package lm.muilThreadDownload;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import lm.muilThreadEntity.DownloadInfo; import lm.muilThreadService.Downloader;import android.app.ListActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast;public class MuilThreadDownloadActivity extends ListActivity { @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);showListView();//顯示listView}private void showListView() {List<Map<String, String>> data = new ArrayList<Map<String, String>>();Map<String, String> map = new HashMap<String, String>();map.put("name", "liming.mp3");data.add(map);map = new HashMap<String, String>();map.put("name", "liming2.mp3");data.add(map);map = new HashMap<String, String>();map.put("name", "liming3.mp3");data.add(map);SimpleAdapter adapter = new SimpleAdapter(this, data,R.layout.list_item, new String[] { "name" },new int[] { R.id.tv_resouce_name });setListAdapter(adapter);} }

xml文件的代碼如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/mainlayout"> <ListViewandroid:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

我們看到,上面的ListView的id用的就是系統自帶的"@android:id/list"。

其次,我們也可以不用布局文件,自己定義一個ListView的對象,通過id來獲得加載的視圖文件。具體代碼如下:

package lm.mediaPlayer;import android.app.ListActivity; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView;public class MyMediaPlayerActivity extends ListActivity {private ListView listView;private ScannerSDCardReceiver receiver;private boolean b = false;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);listView = new ListView(this);listView.setId(android.R.id.list);//獲得listView的idsetContentView(listView);//加載listViewshowListView();}private void showListView() {//顯示listViewString[] from = {"全部音樂","最近播放音樂"};ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,from);listView.setAdapter(adapter);} }

運行結果如下:

?

最后,我們看一下ExpandableListActivity的用法,開始運行效果圖如下:

當我們展開向右的箭頭時,效果如下:

我們看到“國家”和“語言”分別是組名,每個組名下面還有很多child(中國,美國),(漢語,英語),其實ExpandableListActivity就是實現這樣的功能,能更方便的現實一些列表信息。具體代碼如下:

package lm.expendablelistAcitivity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import android.app.ExpandableListActivity; import android.os.Bundle; import android.widget.SimpleExpandableListAdapter; //首先繼承ExpandableListActivity public class MyExpendableListActivityActivity extends ExpandableListActivity{/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);List<Map<String,String>> list = new ArrayList<Map<String,String>>();//組名Map<String,String> map1 = new HashMap<String,String>();map1.put("group", "國家");Map<String,String> map2 = new HashMap<String,String>();map2.put("group", "語言");list.add(map1);list.add(map2);List<Map<String,String>> listChild1 = new ArrayList<Map<String,String>>();//childMap<String,String> map3 = new HashMap<String,String>();map3.put("country", "中國");listChild1.add(map3);Map<String,String> map4 = new HashMap<String,String>();map4.put("country", "美國");listChild1.add(map4);List<Map<String,String>> listChild2 = new ArrayList<Map<String,String>>();//childMap<String,String> map5 = new HashMap<String,String>();map5.put("country", "漢語");listChild2.add(map5);Map<String,String> map6 = new HashMap<String,String>();map6.put("country", "英語");listChild2.add(map6);List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();//將兩個child加入的集合中childs.add(listChild1);childs.add(listChild2);SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, list, R.layout.group, new String[]{"group"},new int[]{R.id.tv_group}, childs, R.layout.child, new String[]{"country"}, new int[]{R.id.tv_child});setListAdapter(adapter);//適配器} } 其中group的xml文件代碼如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <TextViewandroid:id="@+id/tv_group" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px"android:paddingTop="10px"android:paddingBottom="10px"android:textSize="25sp"android:text="無數據"/> </LinearLayout>

child的xml文件代碼如下:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <TextViewandroid:id="@+id/tv_child" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="50px"android:paddingTop="5px"android:paddingBottom="5px"android:textSize="20sp"android:text="無數據"/> </LinearLayout>

好了,以上就是我總結的內容,希望大家多多指教!

轉載于:https://www.cnblogs.com/limingblogs/archive/2011/10/09/2204866.html

總結

以上是生活随笔為你收集整理的listActivity和ExpandableListActivity的简单用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。