码迷,mamicode.com
首页 > 其他好文 > 详细

activity间数据传递两种方式小结

时间:2015-07-30 12:55:17      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

一、通过SharedPreferences放到内存文件中。

保存的代码示例:

                // 保存选中的社区ID,以便后续查询用
                SharedPreferences sharepre = getSharedPreferences(Constant.SP_KEY_SELECT_COMMUNITY,
                        Context.MODE_PRIVATE);
                Editor edit = sharepre.edit();
                edit.putString(Constant.SELECT_COMMUNITY_ID, commuList.get(position).getId());
                edit.commit();//记得commit
                Log.v(LOG_TAG, "onItemClick select_community_id=" +  commuList.get(position).getId());
                startActivity(new Intent(CommunityActivity.this, ActListActivity.class));

 

获取参数代码示例:

    private void getActListData() {

        SharedPreferences sharepre = context.getSharedPreferences(Constant.SP_KEY_SELECT_COMMUNITY,
                Context.MODE_PRIVATE);
        String selCommuId = sharepre.getString(Constant.SELECT_COMMUNITY_ID, "");
        final String url = Config.LATESTACTIVITY + selCommuId;

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    String jsonStr = NetWorkUtil.getNetData(url);
                    Message msg = handler.obtainMessage(1);
                    msg.arg1 = 1;
                    msg.obj = jsonStr;
                    handler.sendMessage(msg);

                } catch (Exception e) {
                    Log.e(LOG_TAG, Log.getStackTraceString(e));
                }
            }
        }).start();
    }

 

 

二、在intent中保存参数

保存代码示例

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LatestActivity_ActivitylistBean act = actItemList.get(position);
                Intent intent = new Intent(context, ActInfoActivity.class);
                intent.putExtra("title", act.getTitle());
                intent.putExtra("act", act.getDescription());
                intent.putExtra("price", act.getPrice());
                intent.putExtra("pic", act.getPic());
                startActivity(intent);
            }

获取参数代码示例

        Intent intent = getIntent();
        title.setText(intent.getStringExtra("title"));
        desc.setText(intent.getStringExtra("desc"));
        price.setText(intent.getStringExtra("price"));
        BitmapUtils bitmapUtils = new BitmapUtils(this);
        bitmapUtils.display(pic, Config.IMAGE_URL+(intent.getStringExtra("pic")));

 

activity间数据传递两种方式小结

标签:

原文地址:http://www.cnblogs.com/2015android/p/4688611.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!