关闭

listView和adapter的简单应用

标签: android
183人阅读 评论(0) 收藏 举报
分类:

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


完整代码下载地址: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>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4733次
    • 积分:196
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类