您的位置:首页 > 产品设计 > UI/UE

【Android 开发】:UI控件之 ListView 列表控件的使用(二)

2013-06-09 21:54 1001 查看
    在上一讲中我们讲了ListView的使用,该讲内容中的适配器主要是使用ArrayAdapter的方式,它主要是数组的形式来展示的,这种方式也是比较单一的,这一讲我们将来学习一下ListView的其他用法来展示一下二维的数据的方法。如下图所示



1. 主要程序

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/pname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="产品名称"
android:textSize="15sp" />

<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="产品价格"
android:textSize="15sp" />

<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight="1"
android:text="产品地址"
android:textSize="15sp" />
</LinearLayout>

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>数据源代码:
package com.android.listviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//数据源类
public class MyDataSource {

public MyDataSource() {
// TODO Auto-generated constructor stub
}

public static List<Map<String, String>> getMaps(){

List<Map<String, String>> listMaps = new ArrayList<Map<String,String>>();

Map<String, String> map1 = new HashMap<String, String>();
map1.put("pname", "苹果"); //注意这个key一定要和 标题名称是一致的
map1.put("price", "10$");
map1.put("address", "江南");

Map<String, String> map2 = new HashMap<String, String>();
map2.put("pname", "橘子"); //注意这个key一定要和 标题名称是一致的
map2.put("price", "15$");
map2.put("address", "杭州");

Map<String, String> map3 = new HashMap<String, String>();
map3.put("pname", "雪梨"); //注意这个key一定要和 标题名称是一致的
map3.put("price", "8$");
map3.put("address", "苏州");

listMaps.add(map1);
listMaps.add(map2);
listMaps.add(map3);
return listMaps;
}

}
主要代码:
package com.android.listviewdemo;

import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

private ListView listView;
private SimpleAdapter adapter;
private List<Map<String, String>> data = null; //作为数据源

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initComponent();
data = MyDataSource.getMaps();
adapter = new SimpleAdapter(MainActivity.this, data, R.layout.activity_main, new String[] {
"pname", "price", "address"
}, new int[] {
R.id.pname, R.id.price, R.id.address
});
listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void initComponent() {

listView = (ListView) findViewById(R.id.listview);
}
}


2. 程序执行如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐