How to set data in Activity and get in java class

2019-03-22 08:07发布

问题:

Updated :

I have build a image cropping app its running fine, but now I want to save cropped image name as textbox value.

In short I am trying to set textbox value in object and get object value in java Class. I have tried several techniques, recently I am trying to get,set data by using interface technique and the image is saved as ".jpg"only.

I would love to know where am I going wronk?

Following is the code I have tried.

MainActivity

public class TestActivity extends AppCompatActivity implements CropHandler, View.OnClickListener {

    public static final String TAG = "TestActivity";

    ImageView mImageView;
    EditText formnumber;

    String formid;

    CropParams mCropParams;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mCropParams = new CropParams(this);
        mImageView = (ImageView) findViewById(R.id.image);
        formnumber =(EditText)findViewById(R.id.FormNumber);


        findViewById(R.id.bt_crop_capture).setOnClickListener(this);
        findViewById(R.id.bt_crop_gallery).setOnClickListener(this);


    }

    @Override

    public void onClick(View v) {
        mCropParams.refreshUri();
        formid=formnumber.getText().toString();

//        Intent i = new Intent(TestActivity.this, CropHelper.class);
//        i.putExtra("Id",formid);

if(formid.matches(""))
{
    Toast.makeText(getApplicationContext(),"Please Enter Application Id",Toast.LENGTH_SHORT).show();
}
else
{
    switch (v.getId()) {
        case R.id.bt_crop_capture: {
            mCropParams.enable = true;
            mCropParams.compress = false;

            Intent intent = CropHelper.buildCameraIntent(mCropParams);

            startActivityForResult(intent, CropHelper.REQUEST_CAMERA);

        }
        break;
        case R.id.bt_crop_gallery: {
            mCropParams.enable = true;
            mCropParams.compress = false;
            Intent intent = CropHelper.buildGalleryIntent(mCropParams);
            startActivityForResult(intent, CropHelper.REQUEST_CROP);
        }
        break;

    }
    }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        CropHelper.handleResult(this, requestCode, resultCode, data);
        if (requestCode == 1) {
            Log.e(TAG, "");
        }
    }

@Override
public void onTaskComplete(String response) {
    onTaskComplete(this.formid);
}
}

CropHelper Class

    public class CropHelper {
    public static final String TAG = "CropHelper";
    /**
     * request code of Activities or Fragments
     * You will have to change the values of the request codes below if they conflict with your own.
     */
    public static final int REQUEST_CROP = 127;
    public static final int REQUEST_CAMERA = 128;
    public static final int REQUEST_PICK = 129;

    public static String AppId;

    public static final String CROP_CACHE_FOLDER = "PhotoCropper";

    public static Uri generateUri() {
        File cacheFolder = new File(Environment.getExternalStorageDirectory() + File.separator + CROP_CACHE_FOLDER);
        if (!cacheFolder.exists()) {
            try {
                boolean result = cacheFolder.mkdir();

                Log.d(TAG, "generateUri " + cacheFolder + " result: " + (result ? "succeeded" : "failed"));
            } catch (Exception e) {
                Log.e(TAG, "generateUri failed: " + cacheFolder, e);
            }
        }

//        String name = String.format("image-%d.jpg", System.currentTimeMillis());
        String name = String.format(AppId.toString()+".jpg",System.currentTimeMillis());
        return Uri
                .fromFile(cacheFolder)
                .buildUpon()
                .appendPath(name)
                .build();
    }

@Override
public void onTaskComplete(String response) {
    AppId=response;
}
}

Interface

    public interface CropHandler
{

    void onPhotoCropped(Uri uri);

    void onCompressed(Uri uri);

    void onTaskComplete(String response);

    void onCancel();

    void onFailed(String message);

    void handleIntent(Intent intent, int requestCode);

    CropParams getCropParams();
}

回答1:

Set formid to EditText value and get the return value in your CropHelper class.

public static String formid=null;
formid=formnumber.getText().toString();

Now create an object of your Activity in a class where you want to call formid value.

MainActivity my_objec= new MainActivity();
    String id= my_objec.formid;
    String name = String.format(""+id+".jpg",System.currentTimeMillis());

thats all you need to do.



回答2:

Implement this with your class and get return back your value in interface

public interface onTaskComplete {
      void onComplete(String response);
}


回答3:

Normally what i do is create different class which holds/save all data and values which can used across differnt classes in app.



回答4:

For example:

// your activity
private CropHelper cropHelper;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CropHelper.REQUEST_CROP) {
        cropHelper.onReceivedImageData(data.get...)
    }
}

public interface DataCallBack {
    public void OnReceivedImageData(Data data);
}

....

// your crop helper
public class CropHelper implements YourActivity.DataCallBack {

    @Override
    public void OnReceivedImageData(Data data) {
        // doing anything with data
    }
}


回答5:

Best Approach for this is using interface try to do as : Create Interface

   public interface MyListener {
    // you can define any parameter as per your requirement
    public void callback(View view, String result);
}

public class MyActivity extends Activity implements MyListener {
   @override        
   public void onCreate(){
        MyButton m = new MyButton(this);
   }

    // method invoke when mybutton will click
    @override
    public void callback(View view, String result) {   
        // do your stuff here
    }
}

public class MyButton {
    MyListener ml;

    // constructor
    MyButton(MyListener ml) {
        this.ml = ml;
    }

    public void MyLogicToIntimateOthere() {
        ml.callback(this, "success");
    }
}

for more Go to this link:

Using Interface



回答6:

Pass data through arguments in constructor..,

For example.. Create Constructor in your class.

public class CropHelper {

    private Context context;
    private String msg;

    public CropHelper(Context context, String msg) {
        this.context = context;
        this.msg = msg;

        if (msg != null) {
            showMsg(msg);
        }
    }

    //Replace with your logic
    void showMsg(String msg) {
        //Perform your operation
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}

And then simple call it from any Activity by Creating instance of that class..

Like..

new CropHelper(this, "Hello from Activity");