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

一、通过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")));

 

posted @ 2015-07-30 10:54  2015android  阅读(197)  评论(0编辑  收藏  举报