user3709878
user3709878

Reputation: 45

How To Get Value From another Activity?

I want to Set value in a Resigter Model. I want to create a SignUp Activity in four Step. I want to know how to set value in Register Model. And I have to Get that value from anywhere.

Here is my code All Values are placed in one Activity. And I want to make Four Step

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();
    }
}

}

Can you Please tell me how to set these Value in Registers, then I will create Four Step Of Signup.

Upvotes: 1

Views: 187

Answers (3)

prabhakaran
prabhakaran

Reputation: 678

There are many way to pass value from one activity to another activity like shared preference, bundle,intent putextra, database.. for a beginner you can use the shared preference.. i hope this will help you..

1.Following tutorial may help you:

Basic Tutorial for shared preference(Login and register form)

Simple example for shared preference with source code

Upvotes: 0

MilapTank
MilapTank

Reputation: 10076

Option 1: Just pass the data

if you want to just pass data from one activity to another than you can do like this

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

and get in next Activity like

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

Option 2 Save data in to SharedPreferences

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

and get data from it

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

Upvotes: 0

Anu
Anu

Reputation: 1914

You can do this using one of the following methods

  1. Use Shared Preferences to save your data and you can access it from anywhere in the application (recommended).
  2. If you have more data to be stored, I recommend using database.
  3. You can pass the values from one activity to another using intent.putExtra() method. But you will have to do this for all new Activities
  4. Another simple method is to make your variables public static and access the data using static reference from any where in your project. (not recommended).

Hope this will help you. :)

Upvotes: 3

Related Questions