从一个方法获取字符串并在另一个方法中使用它(相同类)

时间:2016-03-24 11:28:50

标签: java android methods get set

我看到很多不同的帖子有同样的问题,但我无法弄清楚我的情况。

第一部分

 AsyncTask.execute(new Runnable() {
                @Override
                public void run() {
                    String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("eu", "", destination.getAbsolutePath(), openAlprConfFile, 10);

                    Log.d("-", result);

                    try {
                        final Results results = new Gson().fromJson(result, Results.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (results == null || results.getResults() == null || results.getResults().size() == 0) {
                                    Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
                                    resultTextView.setText("It was not possible to detect the licence plate.");
                                } else {
                                    resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                                            // Trim confidence to two decimal places
                                            + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                                            // Convert processing time to seconds and trim to two decimal places
                                            + " Processing time: " + String.format("%.2f", ((results.getProcessing_time_ms() / 1000.0) % 60)) + " seconds");


                                    String str = results.getResults().get(0).getPlate();
                                    EditText editText = (EditText) findViewById(R.id.edit_text);
                                    editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
                                    editText.setText(str, TextView.BufferType.EDITABLE);

                                    plateNumberEdited = editText.toString();


                                }
                            }


                        });

我想在这里使用“ plateNumberEdited ”将值发送给另一个活动,并根据此值显示结果

Button GoToNewActivity = (Button)findViewById(R.id.send);
        GoToNewActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, DVLAresult.class);
                intent.putExtra("dvlaNumber", plateNumberEdited);
                startActivity(intent);
            }
        });

位于onCreate方法

这是完整的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(R.mipmap.ic_launcher);

    Button GoToNewActivity = (Button)findViewById(R.id.send);
    GoToNewActivity.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MainActivity.this, DVLAresult.class);
            intent.putExtra("dvlaNumber", plateNumberEdited);
            startActivity(intent);
        }
    });


    ANDROID_DATA_DIR = this.getApplicationInfo().dataDir;

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkPermission();
        }
    });
    resultTextView = (TextView) findViewById(R.id.textView);
    imageView = (ImageView) findViewById(R.id.imageView);

    resultTextView.setText("Welcome to CarAnalyser ");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
        final ProgressDialog progress = ProgressDialog.show(this, "Loading", "Parsing result...", true);
        final String openAlprConfFile = ANDROID_DATA_DIR + File.separatorChar + "runtime_data" + File.separatorChar + "openalpr.conf";
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 10;

        // Picasso requires permission.WRITE_EXTERNAL_STORAGE
        Picasso.with(MainActivity.this).load(destination).fit().centerCrop().into(imageView);
        resultTextView.setText("Processing");

        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                String result = OpenALPR.Factory.create(MainActivity.this, ANDROID_DATA_DIR).recognizeWithCountryRegionNConfig("eu", "", destination.getAbsolutePath(), openAlprConfFile, 10);

                Log.d("OPEN ALPR", result);

                try {
                    final Results results = new Gson().fromJson(result, Results.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (results == null || results.getResults() == null || results.getResults().size() == 0) {
                                Toast.makeText(MainActivity.this, "It was not possible to detect the licence plate.", Toast.LENGTH_LONG).show();
                                resultTextView.setText("It was not possible to detect the licence plate.");
                            } else {
                                resultTextView.setText("Plate: " + results.getResults().get(0).getPlate()
                                        // Trim confidence to two decimal places
                                        + " Confidence: " + String.format("%.2f", results.getResults().get(0).getConfidence()) + "%"
                                        // Convert processing time to seconds and trim to two decimal places
                                        + " Processing time: " + String.format("%.2f", ((results.getProcessing_time_ms() / 1000.0) % 60)) + " seconds");


                                String str = results.getResults().get(0).getPlate();
                                EditText editText = (EditText) findViewById(R.id.edit_text);
                                editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
                                editText.setText(str, TextView.BufferType.EDITABLE);

                                plateNumberEdited = editText.toString();


                            }
                        }


                    });

                } catch (JsonSyntaxException exception) {
                    final ResultsError resultsError = new Gson().fromJson(result, ResultsError.class);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            resultTextView.setText(resultsError.getMsg());
                        }
                    });
                }

                progress.dismiss();
            }


        });
    }
}

0 个答案:

没有答案