listView和adapter的简单应用

注意:本项目可以直接下载运行,下载地址在下方,非常适合新手学习的实例,谢谢支持


完整代码下载地址:http://download.csdn.net/detail/cf8833/9283351


直接贴代码:


ListActivity.java

package com.example.zzmap;


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


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class ListActivity extends Activity {
private ListView listView;
private SimpleAdapter adapter;
private List<Map<String, String>> data = null;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);


listView = (ListView) findViewById(R.id.listview);
data = MyDataSource.getMaps();
adapter = new SimpleAdapter(ListActivity.this, data,
R.layout.list, new String[] { "pname", "price",
"address" }, new int[] { R.id.pname, R.id.price,
R.id.address });

listView.setAdapter(adapter);

listView.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long arg3) {
// TODO Auto-generated method stub
switch (position) {
case 0:
Log.e("WQR", "苹果");
break;
case 1:
Log.e("WQR", "西瓜");
break;
case 2:
Log.e("WQR", "梨子");
break;
default:
break;
}
}
});
}


}


MyDataSource.java


package com.example.zzmap;


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


public class MyDataSource {

public MyDataSource() {

}
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", "苹果");
map1.put("price", "¥2.5");
map1.put("address", "合肥");

Map<String, String> map2 = new HashMap<String, String>();
map2.put("pname", "西瓜");
map2.put("price", "¥0.9");
map2.put("address", "南京");

Map<String, String> map3 = new HashMap<String, String>();
map3.put("pname", "梨子");
map3.put("price", "¥9.5");
map3.put("address", "江西");

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

return listMaps;
}
}


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" >


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


        <TextView
            android:id="@+id/pname"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="产品名称" />


        <TextView
            android:id="@+id/price"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="产品价格" />


        <TextView
            android:id="@+id/address"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="产品产地" />
    </LinearLayout>


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


</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值