安卓Adapter之SimpleAdapter

摘自:http://www.cnblogs.com/devinzhang/archive/2012/01/20/2328334.html

先在activity_main.xml中添加一个ListView,

如下:

<span style="font-family:Microsoft YaHei;"><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"></ListView>

</RelativeLayout>
</span>

接下来,需要自己写一个样式文件,定义每一项item的布局

如下:vlist.xml

<span style="font-family:Microsoft YaHei;"><?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/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5px"/>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:textSize="22sp"/>
        <TextView android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFFFF"
            android:textSize="12sp"/>
    </LinearLayout>
    

</LinearLayout>
</span>

接下来,是主程序:

<span style="font-family:Microsoft YaHei;">package com.example.simpleadaptertest;

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

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

public class MainActivity extends Activity {
	private ListView listView;
	private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        listView = (ListView)findViewById(R.id.list_view);
        //一共5个参数
        <span style="color:#ff0000;">//第一个参数:所在的activity
        //第二个参数:所要存放的数据
        //第三个参数:item的样式文件
        //第四个参数:Map的键
        //第五个参数: Map对象key对应的资源,也是layout中的各个控件的id,顺序要与第4个参数的顺序对应</span>
        simpleAdapter = new SimpleAdapter(
        		MainActivity.this, 
        		getData(), 
        		R.layout.vlist, 
        		new String[]{"img","title","info"}, 
        		new int[]{R.id.img,R.id.title,R.id.info});
        listView.setAdapter(simpleAdapter);
        
    }


    @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 List<Map<String,Object>> getData(){
    	List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
    	Map<String,Object> map = new HashMap<String, Object>();
    	map.put("img",R.drawable.a);
    	map.put("title", "title 1");
    	map.put("info", "info 1");
    	list.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put("img", R.drawable.a);
    	map.put("title", "title 2");
    	map.put("info", "info 2");
    	list.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put("img", R.drawable.a);
    	map.put("title", "title 3");
    	map.put("info", "info 3");
    	list.add(map);
    	return list;
    }
    
}
</span>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Android购物车示例,使用SimpleAdapter实现: 1. 首先,在布局文件中添加一个ListView控件,如下所示: ``` <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 在Activity中,定义商品数据和购物车数据: ``` private List<Map<String, Object>> goodsList = new ArrayList<>(); private List<Map<String, Object>> cartList = new ArrayList<>(); private double totalPrice = 0; ``` 3. 在onCreate方法中初始化商品数据和SimpleAdapter: ``` SimpleAdapter adapter = new SimpleAdapter(this, goodsList, R.layout.item_goods, new String[] {"name", "price", "add"}, new int[] {R.id.tv_name, R.id.tv_price, R.id.btn_add}); adapter.setViewBinder(new SimpleAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { if (view instanceof Button) { final Map<String, Object> item = (Map<String, Object>) view.getTag(); ((Button) view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addToCart(item); } }); return true; } return false; } }); // 初始化商品数据 Map<String, Object> item1 = new HashMap<>(); item1.put("name", "商品1"); item1.put("price", 10.0); item1.put("add", "加入购物车"); goodsList.add(item1); Map<String, Object> item2 = new HashMap<>(); item2.put("name", "商品2"); item2.put("price", 20.0); item2.put("add", "加入购物车"); goodsList.add(item2); // 绑定数据 ListView listView = findViewById(R.id.list_view); listView.setAdapter(adapter); ``` 4. 定义addToCart方法,将商品添加到购物车中: ``` private void addToCart(Map<String, Object> item) { // 判断购物车中是否已经存在该商品 boolean exist = false; for (Map<String, Object> cartItem : cartList) { if (cartItem.get("name").equals(item.get("name"))) { int count = (int) cartItem.get("count"); cartItem.put("count", count + 1); exist = true; break; } } // 如果购物车中不存在该商品,则添加新的购物车项 if (!exist) { Map<String, Object> cartItem = new HashMap<>(); cartItem.put("name", item.get("name")); cartItem.put("price", item.get("price")); cartItem.put("count", 1); cartList.add(cartItem); } // 更新购物车总价 totalPrice += (double) item.get("price"); } ``` 5. 在Activity中定义一个显示购物车信息的方法: ``` private void showCartInfo() { StringBuilder builder = new StringBuilder(); builder.append("总价:").append(totalPrice).append("\n"); for (Map<String, Object> cartItem : cartList) { builder.append(cartItem.get("name")).append(" x ") .append(cartItem.get("count")).append("\n"); } AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); dialogBuilder.setMessage(builder.toString()) .setPositiveButton("去结算", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO: 发送购物车数据到后台进行结算 } }) .setNegativeButton("取消", null) .show(); } ``` 6. 在Activity中添加一个按钮,点击该按钮可以显示购物车信息: ``` Button btnCart = findViewById(R.id.btn_cart); btnCart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showCartInfo(); } }); ``` 7. 最后,创建一个item_goods.xml布局文件,用于显示商品项: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="16sp" android:textStyle="bold" /> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" /> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:tag="" android:textSize="16sp" /> </LinearLayout> ``` 这样,就实现了一个简单的Android购物车,使用SimpleAdapter实现商品列表和购物车列表的展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值