您的位置:首页 > 移动开发 > Android开发

Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定

2013-07-18 18:01 941 查看
1.listview的数据填充可以通过ArrayAdapter,SimpleAdapter,也可以是一个xml文件,但是ArrayAdapter与SimpleAdapter的区别是:
2     ArrayAdapter可以让一个类继承自BaseAdapter之后,可以对listview中的button,checkBox等空间进行事件的监听操作,而SimpleAdapter只能对listview填充数据的一个操作,不具有对空间的事件监听功能。
3 下面通过实例进行说明(是通过自定义listview):
(1)SimpleAdapter:
listview中每一项中的数据的布局文件及时listItem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:focusable="false"
android:id="@+id/textView1"
android:textSize="14dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:focusable="false"
android:id="@+id/textView2"
android:textSize="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
<ImageButton
android:focusable="true"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagebutton1"/>
</LinearLayout>

lisview的布局文件(list.xml):

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

//此方法是从sdcard的RecorderFile文件夹中扫描到以.3gp结尾的文件存放到一个集合中
public List<Map<String,Object>> serachFile()
{
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File file=new File(Environment.getExternalStorageDirectory().toString()+"/RecorderFile");
File[] files=file.listFiles();

for(int i=0;i<files.length;i++)
{
if(files[i].getName().endsWith(".3gp"))
{
//这个map对象必须在这里声明创建,否侧会出现数据重复显示一项数据
Map<String,Object> map=new HashMap<String,Object>();
map.put("img", R.drawable.ic_launcher);
map.put("title", files[i].getName());
map.put("info", files[i].getPath());
map.put("button", R.drawable.control_play_blue);
list.add(map);
}
}
System.out.println("**************"+Environment.getExternalStorageState());
}
return list;
}

下面是listview中最要的一部是数据填充器(SimpleAdapter)
    SimpleAdapter simpleAdapter=new SimpleAdapter(this, serachFile(),
88            R.layout.listitem,new String[]{"img","title","info","button"},
  new int[]{R.id.imageView1,R.id.textView1,R.id.textView2,R.id.playButton})
参数:
91             1.当前的类对象,可以通过thisl表示
92         2.这个参数是一个集合对象儿上面的方法正好是返回一个集合对象所以是serachFile()
93         3。这个是listview中每一项中的组件的布局文件
94         4.是map对象的以键值对以字符数组的形式
95         5.这个参数是,listview中每一项中的组件的id,把他以数组的方式存放
96 只有通过findviewById()找到listview控件并设置数据源。listview.setAdapter(simpleAdapter)这样就可以把数据填充到listview空间中
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐