您的位置:首页 > Web前端

使用SharedPreferences实现加入购物车功能实现

2014-11-04 00:50 483 查看
使用SharedPreferences来传递数据

最后发现 因为我们传递的数据每一次都是不同的商品所以 我们的键值对每一次都是不同的,于是乎传递到购物车页面的数据每次都会被前一个覆盖,这样的话就失败了,虽然我在商品页面使用的是一个类似于适配器的效果,就是说我商品页面只有一个视图框架,里面的控件的内容我是根据商品列表里面的不同商品触发的我这个商品详细页面,所以我是可以判断这是那一款商品,我可以根据不同商品来设置sharepreference里面的键值对,但是这个sharepreference本来的协议就是需要其他activity知道键值对的键才能获取数据,所以就是败了,这里把
sharepreference 交流的代码贴上来 以防以后回来用到又要查资料 ,

商品详情页面 不要在乎我的ui有多烂 不要在乎这些细节 我们现在不说ui





/*



* 这个是商品详情页面







* */

public class BuyActivity extends Activity {

    //声明这些控件    

    TextView buy_shangpinmingzi,buy_shangpinshichangjia,

                buy_shangpinhuiyuanjia,buy_shangpinkucun,buy_shangpinjieshao ;  

    Button buy_jiarugouwucheanniu ;

    //我们使用 SharePreferences 来保存购物车里面的商品信息

    SharedPreferences preferences;

    //这个editor 是用来编辑 preferences里面内容的  因为它本身没有提供方法来编辑里面的内容

    SharedPreferences.Editor editor;    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.buy_activity);  

        //获取文本控件

        buy_shangpinmingzi = (TextView)findViewById(R.id.buy_shangpinmingzi) ;

        buy_shangpinshichangjia = (TextView)findViewById(R.id.buy_shangpinshichangjia) ;

        buy_shangpinhuiyuanjia = (TextView)findViewById(R.id.buy_shangpinhuiyuanjia) ;

        buy_shangpinkucun = (TextView)findViewById(R.id.buy_shangpinkucun) ;

        buy_shangpinjieshao = (TextView)findViewById(R.id.buy_shangpinjieshao) ;

        //获取按钮

        buy_jiarugouwucheanniu = (Button)findViewById(R.id.buy_jiarugouwucheanniu) ;        

        // 获取只能被本应用程序读、写的SharedPreferences对象

        //第一个参数是存储时的名称,第二个参数则是文件的打开方式

        preferences = getSharedPreferences("kaixinshop", MODE_WORLD_READABLE);

        editor = preferences.edit();

        //设置商品加入购物车按钮监听 用于获取数据 传递至购物车界面

        buy_jiarugouwucheanniu.setOnClickListener(new OnClickListener()

        {

            public void onClick(View arg0)

            {

//获取商品信息保存至 下面的键值对 的键是以 name jiage组成的 

                //那么在获取数据的那边 我们要以上面的 键来找需要获取的数据

                editor.putString("name",buy_shangpinmingzi.getText().toString()) ;

                editor.putString("jiage",buy_shangpinhuiyuanjia.getText().toString()) ;

                //提交数据

                editor.commit(); 

                //使用Toast提示信息

                Toast.makeText(BuyActivity.this , 

                    "商品已经成功加入购物车!" , Toast.LENGTH_SHORT)

                    .show();

            }

        });

    }

复制代码

上面是商品详细界面

下面的是它的xml文件

  

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <horizontalscrollview

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" >

        <linearlayout

            android:layout_width="fill_parent"

            android:layout_height="fill_parent" 

            android:orientation="horizontal">

            <imageview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:id="@+id/buy_img1"

                android:background="@drawable/shangpin_cover_default2" >

            

            <imageview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:id="@+id/buy_img2"

                android:background="@drawable/shangpin_cover_default2" >

            

            <imageview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

             
1826b
  android:id="@+id/buy_img3"

                android:background="@drawable/shangpin_cover_default2" >

            

        </imageview

</imageview

</imageview

</linearlayout

</horizontalscrollview

    

    <textview

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="" >

    

    <linearlayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical" >

        <textview

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:id="@+id/buy_shangpinmingzi"

            android:text="   50元话费充值券" >

        

        <textview

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="" >

        

        <linearlayout

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal" >

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="   " >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:id="@+id/buy_shangpinshichangjia"

                android:text="市场价:50元" >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="   " >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:id="@+id/buy_shangpinhuiyuanjia"

                android:text="会员价:48元" >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="   " >

            

        

        <textview

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="" >

        

        <linearlayout

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal"             

            android:focusable="true"  

            android:focusableInTouchMode="true">

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:id="@+id/buy_shangpinkucun"

                android:text="   库存:99件" >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="   " >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="购买:" >

            

            <textview

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="   " >

            

            <edittext

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:id="@+id/buy_shangpingoumaishu"

                android:digits="0123456789."

                android:text="1" >

            

        

        <textview

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="   " >

        

        <textview

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/buy_shangpinjieshao"

            android:text="  五十元手机话费充值券,您需要在这里下单,之后到我们的实体店告知服务员您的会员账号,待服务员查询无误,便帮助您以最优惠的价格充值话费券指定话费." >

        

    

    <textview

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="   " >

    

    <button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:id="@+id/buy_jiarugouwucheanniu"

        android:text="    加入购物车    " >

   

复制代码

下面是 获取数据的页面 别在意我这里用的是listview 我们现在讲的是使用sharepreference获取数据



/*





* 购物车





* */

  public class ShopCarActivity extends ListActivity {

      protected void onCreate(Bundle savedInstanceState) {

            // TODO Auto-generated method stub

            super.onCreate(savedInstanceState);

            setContentView(R.layout.shopcar_listview_activity);

            SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.shopcar_activity,

                    new String[]{"shopcar_shangpingjiage","shopcar_shangpingmingzi"},               

                    new int[]{R.id.shopcar_shangpingjiage,R.id.shopcar_shangpingmingzi});           

            setListAdapter(adapter);            

      }

      private List<map> getData() {

            List<map> list = new ArrayList<map>();

            Map map = new HashMap();

            //获取从商品信息界面传过来的数据  填入储存时的名称 kaixinshop

            SharedPreferences sharedata = getSharedPreferences("kaixinshop", 0);   

            //将数据赋给我们的控件

             //文本赋值 后面的获取失败是 当没有获取到数据的时候 显示的字符串

            map.put("shopcar_shangpingjiage", sharedata.getString("jiage", "获取失败"));

            map.put("shopcar_shangpingmingzi", sharedata.getString("name", "获取失败"));

            list.add(map);               

            return list;

        }

  }

复制代码

购物车 界面 xml文件

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    

    <imageview

        android:id="@+id/shopcar_shangpintupian"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:background="@drawable/list_item_divide_operate" />

    <linearlayout

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal" >

        <imageview

            android:id="@+id/imageView1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@drawable/video_cover_default" />

        <linearlayout

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:layout_gravity="center_vertical"

            android:orientation="vertical" >

            <imageview

                android:id="@+id/imageView1"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:background="@drawable/list_item_divide_operate" />

            <textview

                android:id="@+id/shopcar_shangpingmingzi"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="话费充值券" />

            <imageview

                android:id="@+id/imageView1"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:background="@drawable/list_item_divide_operate" />

            <textview

                android:id="@+id/shopcar_shangpingjiage"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_gravity="center"

                android:text="售价48元" />

            <imageview

                android:id="@+id/imageView1"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:background="@drawable/list_item_divide_operate" />

        </imageview

</textview

</imageview

</textview

</imageview

</linearlayout

</imageview

</linearlayout

</imageview

    

    <imageview

                android:id="@+id/imageView1"

                android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:background="@drawable/list_item_divide_operate" />

复制代码

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