天自己简单的总结了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。
下面是一个简单的例子,运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/929ec55a315d52d18fbf7deb0779317f.png)
activityde 代码如下:
package lm.muilThreadDownload; |
import java.util.ArrayList; |
import java.util.HashMap; |
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.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 { |
public void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.main); |
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" ); |
map = new HashMap<String, String>(); |
map.put( "name" , "liming2.mp3" ); |
map = new HashMap<String, String>(); |
map.put( "name" , "liming3.mp3" ); |
SimpleAdapter adapter = new SimpleAdapter( this , data, |
R.layout.list_item, new String[] { "name" }, |
new int [] { R.id.tv_resouce_name }); |
xml文件的代码如下:
<?xml version= "1.0" encoding= "utf-8" ?> |
android:orientation= "vertical" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
android:id= "@+id/mainlayout" |
android:id= "@android:id/list" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
我们看到,上面的ListView的id用的就是系统自带的"@android:id/list"。
其次,我们也可以不用布局文件,自己定义一个ListView的对象,通过id来获得加载的视图文件。具体代码如下:
import android.app.ListActivity; |
import android.content.Intent; |
import android.content.IntentFilter; |
import android.os.Bundle; |
import android.os.Environment; |
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 ; |
public void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
listView = new ListView( this ); |
listView.setId(android.R.id.list); |
setContentView(listView); |
private void showListView() { |
String[] from = { "全部音乐" , "最近播放音乐" }; |
ArrayAdapter<String> adapter = new ArrayAdapter<String>( this ,android.R.layout.simple_list_item_1,from); |
listView.setAdapter(adapter); |
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/9a1ffc72776a14a8bf475264416674b9.png)
最后,我们看一下ExpandableListActivity的用法,开始运行效果图如下:
![](https://i-blog.csdnimg.cn/blog_migrate/77c4c54010e468b76fc56efe89bb6ab2.png)
当我们展开向右的箭头时,效果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/9fbc3fec75d2c9a3dbfb6bc4127b0649.png)
我们看到“国家”和“语言”分别是组名,每个组名下面还有很多child(中国,美国),(汉语,英语),其实ExpandableListActivity就是实现这样的功能,能更方便的现实一些列表信息。具体代码如下:
package lm.expendablelistAcitivity; |
import java.util.ArrayList; |
import java.util.HashMap; |
import android.app.ExpandableListActivity; |
import android.os.Bundle; |
import android.widget.SimpleExpandableListAdapter; |
public class MyExpendableListActivityActivity extends ExpandableListActivity{ |
/** Called when the activity is first created. */ |
public 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>(); |
Map<String,String> map2 = new HashMap<String,String>(); |
List<Map<String,String>> listChild1 = new ArrayList<Map<String,String>>(); |
Map<String,String> map3 = new HashMap<String,String>(); |
map3.put( "country" , "中国" ); |
Map<String,String> map4 = new HashMap<String,String>(); |
map4.put( "country" , "美国" ); |
List<Map<String,String>> listChild2 = new ArrayList<Map<String,String>>(); |
Map<String,String> map5 = new HashMap<String,String>(); |
map5.put( "country" , "汉语" ); |
Map<String,String> map6 = new HashMap<String,String>(); |
map6.put( "country" , "英语" ); |
List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>(); |
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}); |
<?xml version= "1.0" encoding= "utf-8" ?> |
android:orientation= "vertical" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
android:id= "@+id/tv_group" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
android:paddingLeft= "60px" |
android:paddingTop= "10px" |
android:paddingBottom= "10px" |
child的xml文件代码如下:
<?xml version= "1.0" encoding= "utf-8" ?> |
android:orientation= "vertical" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
android:id= "@+id/tv_child" |
android:layout_width= "fill_parent" |
android:layout_height= "fill_parent" |
android:paddingLeft= "50px" |
android:paddingBottom= "5px" |
好了,以上就是我总结的内容,希望大家多多指教!