android ListView布局之二(是用simpleAdapter绑定数据)
2014-01-22 17:02
686 查看
http://blog.csdn.net/chenzheng_java/article/details/6200536
最终效果图
目录结构
main.xml主布局文件,代码
[xhtml] view
plaincopy
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="@string/name"
android:gravity="center"
android:layout_width="150px"
android:layout_height="wrap_content"
/>
<TextView
android:text="@string/age"
android:gravity="center"
android:layout_width="170px"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
user.xml组件布局文件代码
[xhtml] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<!-- 创建存放一行数据的组件 -->
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<ImageView
android:id="@+id/image"
android:layout_width="50px"
android:layout_height="50px"
></ImageView>
<TextView
android:id="@+id/userName"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="150px"
></TextView>
<TextView
android:id="@+id/userAge"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="170px"
></TextView>
</TableRow>
</TableLayout>
主Activity,listView.java代码
[java] view
plaincopy
package cn.com.android.listView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class listView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView);
/* 参数一多,有些人就头晕了。这里解说下,各个参数的意思。
* 第一个参数 this 代表的是当前上下文,可以理解为你当前所处的activity
* 第二个参数 getData() 一个包含了数据的List,注意这个List里存放的必须是map对象。simpleAdapter中的限制是这样的List<? extends Map<String, ?>> data
* 第三个参数 R.layout.user 展示信息的组件
* 第四个参数 一个string数组,数组内存放的是你存放数据的map里面的key。
* 第五个参数:一个int数组,数组内存放的是你展示信息组件中,每个数据的具体展示位置,与第四个参数一一对应
* */
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.user,
new String[]{"image","userName","userAge"}, new int[]{R.id.image,R.id.userName,R.id.userAge});
listView.setAdapter(adapter);
}
/**
* @author chenzheng_java
* @description 准备一些测试数据
* @return 一个包含了数据信息的hashMap集合
*/
private ArrayList<HashMap<String, Object>> getData(){
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
for(int i=0;i<10;i++){
HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
tempHashMap.put("image", R.drawable.icon);
tempHashMap.put("userName", "用户"+i);
tempHashMap.put("userAge", 30-i);
arrayList.add(tempHashMap);
}
return arrayList;
}
}
strings.xml代码
[xhtml] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">布局列表展示</string>
<string name="app_name">列表布局</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
</resources>
废话连绵:
我们一起看看结构,一个主布局文件,一个组件布局文件,一个Activity类。
依旧分为三步:
第一步:定义布局文件,设计UI,包括寻找合适的图片了等等……
第二步:获取数据。这里用的是simpleAdapter,所以要求数据必须固定格式的
第三步:绑定数据源
然后,我们就可以看到我们想要的结果了。
最终效果图
目录结构
main.xml主布局文件,代码
[xhtml] view
plaincopy
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="@string/name"
android:gravity="center"
android:layout_width="150px"
android:layout_height="wrap_content"
/>
<TextView
android:text="@string/age"
android:gravity="center"
android:layout_width="170px"
android:layout_height="wrap_content"
/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
user.xml组件布局文件代码
[xhtml] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<!-- 创建存放一行数据的组件 -->
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<ImageView
android:id="@+id/image"
android:layout_width="50px"
android:layout_height="50px"
></ImageView>
<TextView
android:id="@+id/userName"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="150px"
></TextView>
<TextView
android:id="@+id/userAge"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="170px"
></TextView>
</TableRow>
</TableLayout>
主Activity,listView.java代码
[java] view
plaincopy
package cn.com.android.listView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class listView extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView);
/* 参数一多,有些人就头晕了。这里解说下,各个参数的意思。
* 第一个参数 this 代表的是当前上下文,可以理解为你当前所处的activity
* 第二个参数 getData() 一个包含了数据的List,注意这个List里存放的必须是map对象。simpleAdapter中的限制是这样的List<? extends Map<String, ?>> data
* 第三个参数 R.layout.user 展示信息的组件
* 第四个参数 一个string数组,数组内存放的是你存放数据的map里面的key。
* 第五个参数:一个int数组,数组内存放的是你展示信息组件中,每个数据的具体展示位置,与第四个参数一一对应
* */
SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.user,
new String[]{"image","userName","userAge"}, new int[]{R.id.image,R.id.userName,R.id.userAge});
listView.setAdapter(adapter);
}
/**
* @author chenzheng_java
* @description 准备一些测试数据
* @return 一个包含了数据信息的hashMap集合
*/
private ArrayList<HashMap<String, Object>> getData(){
ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();
for(int i=0;i<10;i++){
HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
tempHashMap.put("image", R.drawable.icon);
tempHashMap.put("userName", "用户"+i);
tempHashMap.put("userAge", 30-i);
arrayList.add(tempHashMap);
}
return arrayList;
}
}
strings.xml代码
[xhtml] view
plaincopy
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">布局列表展示</string>
<string name="app_name">列表布局</string>
<string name="name">姓名</string>
<string name="age">年龄</string>
</resources>
废话连绵:
我们一起看看结构,一个主布局文件,一个组件布局文件,一个Activity类。
依旧分为三步:
第一步:定义布局文件,设计UI,包括寻找合适的图片了等等……
第二步:获取数据。这里用的是simpleAdapter,所以要求数据必须固定格式的
第三步:绑定数据源
然后,我们就可以看到我们想要的结果了。
相关文章推荐
- android ListView布局之二(是用simpleAdapter绑定数据)
- android ListView布局之二(是用simpleAdapter绑定数据)
- Android中ListView同过自定义布局并使用SimpleAdapter的方式实现数据的绑定
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- android ListView布局之三(使用自定义的Adapter绑定数据,通过contextView.setTag绑定数据)有按钮的ListView
- Android采用ListView实现数据列表显示1-使用SimpleCursorAdapter进行数据绑定
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(一)
- gridview布局示例二(通过simpleAdapter进行图片等数据绑定)
- gridview布局示例二(通过simpleAdapter进行图片等数据绑定)
- Android控件 – ListView的使用(一)– 使用Adapter绑定数据
- android 从activity用intent跳转到listactivity并在其中用simpleadapter在listview中显示数据列
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据(二)
- 安卓:将数据库中的数据查询出来用adapter(SimpleAdapter,SimpleCursorAdapter,自定义适配器CursorAdapter)绑定到listview上
- Android train——ListView绑定ArrayAdapter、SimpleAdapter、SimpleCursorAdapter、BaseAdapter
- android中,用SimpleAdapter创建有自己布局的listView,以及listView的修改更新notifyDataSetChanged()的使用。
- 【Android】使用自定义Adapter优化ListView、修改数据及控件内部布局
- android ListView利用SimpleAdapter显示特定布局并且异步加载网络图片
- android--(布局ListView、优化、刷新分页、ArrayAdapter、SimpleAdapter 、BaseAdapter)
- ListView的Adapter使用(绑定数据) 之 自定义每一项的布局去绑定数据