ListView中两种数据适配器的使用
来源:互联网 发布:福州大学校园网络 编辑:程序博客网 时间:2024/06/08 18:57
ListView中两种数据适配器的使用
package com.example.tedemo01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView listView;
private ArrayAdapter<String>arr_adapter;
private SimpleAdapter simp_adapter;
private List<Map<String, Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.listView);
//新建一个数据适配器
//ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
//2 适配器加载数据源
String[]arr_data={"数据适配器","上下文","布局文件","数据源"};
dataList=new ArrayList<Map<String, Object>>();
arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);
//SimpleAdapter有五个参数分别为 context上下文(this);数据源data;(List<? extend Map<String, ?>> data) 一个Map所组成的list集合
//resource 列表项的布局文件ID; from :Map中的键名; to:绑定数据视图中的ID,与from成对应关系
simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic", "text"}, new int []{R.id.pic, R.id.text});
//3 视图(listview)加载适配器
//listView.setAdapter(arr_adapter);
listView.setAdapter(simp_adapter);
}
private List<Map<String, Object>> getData(){
for(int i=0; i<20; i++)
{
Map<String, Object>map=new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "商品"+i);
dataList.add(map);
}
return dataList;
}
}
第一种ArrayAdapter中的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:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
第二种SimpleAdapter中的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="horizontal" >
<ImageView
android:id="@+id/pic"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:text="demo"
/>
</LinearLayout>
package com.example.tedemo01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.R.anim;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView listView;
private ArrayAdapter<String>arr_adapter;
private SimpleAdapter simp_adapter;
private List<Map<String, Object>>dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
listView = (ListView) findViewById(R.id.listView);
//新建一个数据适配器
//ArrayAdapter(上下文,当前ListView加载的每一个列表项所对应的布局文件,数据源)
//2 适配器加载数据源
String[]arr_data={"数据适配器","上下文","布局文件","数据源"};
dataList=new ArrayList<Map<String, Object>>();
arr_adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr_data);
//SimpleAdapter有五个参数分别为 context上下文(this);数据源data;(List<? extend Map<String, ?>> data) 一个Map所组成的list集合
//resource 列表项的布局文件ID; from :Map中的键名; to:绑定数据视图中的ID,与from成对应关系
simp_adapter=new SimpleAdapter(this, getData(), R.layout.item, new String[]{"pic", "text"}, new int []{R.id.pic, R.id.text});
//3 视图(listview)加载适配器
//listView.setAdapter(arr_adapter);
listView.setAdapter(simp_adapter);
}
private List<Map<String, Object>> getData(){
for(int i=0; i<20; i++)
{
Map<String, Object>map=new HashMap<String,Object>();
map.put("pic", R.drawable.ic_launcher);
map.put("text", "商品"+i);
dataList.add(map);
}
return dataList;
}
}
第一种ArrayAdapter中的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:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
第二种SimpleAdapter中的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="horizontal" >
<ImageView
android:id="@+id/pic"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:text="demo"
/>
</LinearLayout>
0 0
- ListView中两种数据适配器的使用
- listview两种数据适配器以及监听处理
- Android中ListView常用的两种适配器ArrayAdapter和SimpleAdapter使用
- ListView中适配器的配置使用
- Ajax中XML和json两种数据格式的使用和比较
- 数据仓库中两种数据模型的分析比较
- 论MySQL数据库中两种数据引擎的差别
- Java中比较常用的两种数据转化
- Android中ListView,Gallery,GridView 的适配器Adapter的使用
- VB中两种数组的上限
- ListView的适配器中viewHolder
- ListView与适配器的使用
- android之ListView中使用的适配器CursorAdapter
- Gridview中两种数据操作方式
- Gridview中两种数据操作方式
- 关于javascript 中两种数据类型
- uPortal的两种数据源
- 两种数据源的配置
- (转)[教程] 程序中使用MATLAB编译产生的DLL (VC6.0, MATLAB R2010b, mwArray
- 如何优化buffer_cache
- mybatis模糊查询
- 读《进化:我们在互联网上奋斗的故事》之感想
- Android ADT安装技巧其常见错误处理
- ListView中两种数据适配器的使用
- uml中的那些关系
- 8.29 设置设备支持方向
- 第九周项目六 2
- 关于mwArray和一般数组的区别
- 9.03 视图控制器属性赋值
- Xcode6 找回消失的 Precompile Prefix Header
- c语言0x10个经典问题
- Young氏矩阵