如何从另一项活动中获取价值?

时间:2014-06-16 05:22:10

标签: android

我想在Resigter模型中设置值。我想在四步中创建一个SignUp Activity。我想知道如何在Register Model中设置值。我必须从任何地方获得这个价值。

这是我的代码所有值都放在一个Activity中。我想做四步

public void UploadData(final String link) {

    Response = "";

    try {

        HttpResponse response;

        Log.d("pre_link", "pre_link = " + link);

        final HttpClient httpclient = new DefaultHttpClient();

        final HttpPost httppost = new HttpPost(link);

        /*httppost.addHeader("Authorization", "Basic "
                + Base64.encodeToString(("username" + ":"
                        + "password").getBytes(), Base64.NO_WRAP));*/

        MultipartEntity mpEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        String FullName = fullName.getText().toString();
        String UserName = userName.getText().toString();
        String DateOfBirth = dob.getText().toString();
        String Age = age.getText().toString();

        String Sex = gender.getText().toString();
        String InterestedIn = interestIn.getText().toString();
        String ToMeet = "both";//toMeet.getText().toString();

        String Email = email.getText().toString();
        String Password = pwd.getText().toString();
        String Lat = String.valueOf(latitude);
        String Long = String.valueOf(longitude);

        mpEntity.addPart("fullName", new StringBody(FullName));
        mpEntity.addPart("userName", new StringBody(UserName));
        mpEntity.addPart("dob", new StringBody(DateOfBirth));
        mpEntity.addPart("age", new StringBody(Age));
        mpEntity.addPart("gender", new StringBody(Sex));
        mpEntity.addPart("interestIn", new StringBody(InterestedIn));
        mpEntity.addPart("toMeet", new StringBody(ToMeet));
        mpEntity.addPart("email", new StringBody(Email));
        mpEntity.addPart("pwd", new StringBody(Password));

        mpEntity.addPart("latitude", new StringBody(Lat));
        mpEntity.addPart("longitude", new StringBody(Long));

        if (bab1 != null) {
            mpEntity.addPart("uploaded_file", bab1);
        }

        httppost.setEntity(mpEntity);

        createCancelProgressDialog("Uploading Image", "Please wait...", "Cancel");

        new Thread() {
            public void run() {
                try {
                    HttpResponse response;
                    Message msg = new Message();

                    msg.what = 1;

                    try {
                        response = httpclient.execute(httppost);
                        HttpEntity resEntity = response.getEntity();
                        if (resEntity != null) {
                            Response = EntityUtils.toString(resEntity)
                                    .trim();

                            Log.d("Response", "Response = " + Response);

                            Message msg2 = new Message();
                            msg2.what = 1;
                            UpdateHandler.sendMessage(msg2);

                        }
                        if (resEntity != null) {
                            resEntity.consumeContent();
                        }
                    } catch (ClientProtocolException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }

            }
        }.start();

    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

public Handler UpdateHandler = new Handler() {
    public void handleMessage(Message msg) {

        switch (msg.what) {
        case 1:
            try {

                cancelDialog.dismiss();
                cancelDialog.hide();

                Log.d("Response", "Response = " + Response);

                Toast.makeText(SignUp.this,"you are Success", Toast.LENGTH_SHORT).show();
                RegisterModel register =new RegisterModel();
                //register.setfullName();

                          Intent i = new Intent(getApplicationContext(),SignupSuccessfully.class);

                         // i.putExtra("pwd",pwsd);
                          startActivity(i);
                          finish();
                        //flag=1;




                //String read_data = ReadDataFromAppCache(MainActivity.this, "file_name");
                //StoreDataToAppCache(MainActivity.this, "file data", "file_name");                 
            } catch (Exception e) {
                // TODO: handle exception
            }
            super.handleMessage(msg);
        }
    }
};

ProgressDialog cancelDialog = null;

private void createCancelProgressDialog(String title, String message,
        String buttonText) {
    cancelDialog = new ProgressDialog(SignUp.this);
    cancelDialog.setTitle(title);
    cancelDialog.setMessage(message);
    cancelDialog.setCanceledOnTouchOutside(false);
    // cancelDialog2.setIcon(R.drawable.icon);

    /*cancelDialog.setButton(buttonText,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    cancelDialog.dismiss();
                    cancelDialog.hide();
                    return;
                }
            });*/
    cancelDialog.show();
}

public Bitmap setBitmap(String _path) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inTempStorage = new byte[16*1024];
    options.inPurgeable = true;
    //options.inJustDecodeBounds = true;
    Bitmap bitmap = null;

    ExifInterface exif;
    try {
        bitmap = BitmapFactory.decodeFile(selectedImagePath, options);

        exif = new ExifInterface(_path);
        int exifOrientation = exif
                .getAttributeInt(ExifInterface.TAG_ORIENTATION,
                        ExifInterface.ORIENTATION_NORMAL);

        int rotate = 0;

        switch (exifOrientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;

        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        }

        //Log.d("image_rotation", "image_rotation = " + rotate);

        if (rotate != 0) {
            int w = bitmap.getWidth();
            int h = bitmap.getHeight();

            // Setting pre rotate
            Matrix mtx = new Matrix();
            mtx.preRotate(rotate);

            // Rotating Bitmap & convert to ARGB_8888, required by tess
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
            bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

        } 

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return bitmap;
}

public String ReadDataFromAppCache(Context context, String file_name) {

    String output = "";
    Log.d("file name", "file name = " + file_name);
    try {
        int ch;
        File f = new File(context.getFilesDir() + "/" + file_name);
        //Log.d("file path", "" + f.getAbsolutePath());

        StringBuffer strContent = new StringBuffer("");
        FileInputStream fin = null;

        try {
            fin = new FileInputStream(f);

            while ((ch = fin.read()) != -1)
                strContent.append((char) ch);

            fin.close();
        } catch (FileNotFoundException e) {
            //Log.d("File " + f.getAbsolutePath(), " could not be found on filesystem");
            output = "null";
            return output;
        } catch (IOException ioe) {
            //Log.d("Exception while reading the file", "Exception while reading the file" + ioe);
        }

        try {
            output = URLDecoder.decode(strContent.toString(), "UTF-8");
        } catch (UnsupportedEncodingException uee) { }

        //Log.d("This is xml", "This is xml" + strContent);

        //output = strContent.toString();

    } catch (Exception e) {
        // TODO: handle exception
    }
    return output;
}

public void StoreDataToAppCache(Context con, String fileData, String file_name) {
    try {
        String encodedValue = "";
        try {
            encodedValue = URLEncoder.encode(fileData, "UTF-8");
        } catch (UnsupportedEncodingException uee) {
        }

        //encodedValue = sBody;
        //Log.d("store text", "store_text = " + encodedValue);
        File f = new File(con.getFilesDir() + "/" + file_name);

        FileWriter writer = new FileWriter(f);
        writer.append(encodedValue);
        writer.flush();
        writer.close();
        Log.d("save complete", "save complete");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

你能告诉我如何在寄存器中设置这些值, 然后我将创建四步注册。

3 个答案:

答案 0 :(得分:3)

您可以使用以下方法之一

执行此操作
  1. 使用“共享首选项”保存数据,您可以从应用程序的任何位置访问它(推荐)。
  2. 如果您要存储更多数据,我建议使用数据库。
  3. 您可以使用intent.putExtra()方法将值从一个活动传递到另一个活动。但是你必须为所有新活动
  4. 这样做
  5. 另一种简单的方法是使变量公共静态,并使用项目中任何位置的静态引用访问数据。 (不推荐)。
  6. 希望这会对你有所帮助。 :)

答案 1 :(得分:0)

选项1:只需传递数据

如果您只想将数据从一个活动传递到另一个活动而不是像这样做

startActivity(new Intent(Context,Activity_name.class).putExtra("Key",Value));

然后进入下一个活动

Intent intent = getIntent();
variable = = intent.getXXXExtra("Key");

选项2将数据保存到SharedPreferences

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putXXX("Key", Value);
editor.commit();

并从中获取数据

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Variable = getResources().getInteger("Key");

答案 2 :(得分:0)

有很多方法可以将值从一个活动传递到另一个活动,如共享首选项,包,意图putextra,数据库......对于初学者,您可以使用共享首选项..我希望这会对您有所帮助..

1.以下教程可以帮助您:

Basic Tutorial for shared preference(Login and register form)

Simple example for shared preference with source code