0

我想从中返回一个值,AsyncTask并且我想与该值进行比较,contents如果它们匹配,我想将我从中获取的值发送AsyncTasnk到另一个意图。我希望我解释得当。

这是代码:

public class MainActivity extends Activity {

    Button ButtonClick;

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

        final TextView txt = (TextView) findViewById(R.id.textView1);
        ButtonClick = (Button) findViewById(R.id.qrReader);
        ButtonClick.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(
                        "com.google.zxing.client.android.SCAN");
                startActivityForResult(intent, 0);
                MainActivity.this.runOnUiThread(new Runnable() {

                    public void run() {
                        GetProduts GP = new GetProduts();
                        txt.setText(GP.execute().toString());
                    }

                });
                // Intent i = new Intent(MainActivity.this,IndexActivity.class);
                // startActivity(i);

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {

        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                final String contents = intent.getStringExtra("SCAN_RESULT");
                //I want to compare this value

            }
        } else if (resultCode == RESULT_CANCELED) {
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public class GetProduts extends AsyncTask<ArrayList<Product>, Void, ArrayList<Product>> {

        Product p;
        final ArrayList<Product> productIdList = new ArrayList<Product>();

        @Override
        protected ArrayList<Product> doInBackground(ArrayList<Product>... params) {
            HttpClient httpclient = new DefaultHttpClient();
            GeneralConstans GC = new GeneralConstans();
            // Products will be stated in memory
            HttpPost httpget = new HttpPost(GC.UrlProducts);
            HttpResponse response;
            String result = null;
            try {

                HttpContext ctx = new BasicHttpContext();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        2);
                httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,
                        "UTF-8"));

                response = httpclient.execute(httpget, ctx);
                HttpEntity resEntity = response.getEntity();

                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity);
                    JSONArray arr = new JSONArray(result);
                    Gson gson = new Gson();
                    if (arr.length() > 0) {
                        for (int j = 0; j < arr.length(); j++) {
                            p = gson.fromJson(arr.getString(j), Product.class);
                            productIdList.add(p);
                        }

                    }

                    return productIdList;

                }

            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                /*
                 * if (checkAbortStatus(e.getMessage()) == true) {
                 * handler.sendEmptyMessage(0); }
                 */
            } catch (IOException e) {
                /*
                 * if (checkAbortStatus(e.getMessage()) == true) {
                 * handler.sendEmptyMessage(0); }
                 */
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(ArrayList<Product> result) {


            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

    }
}
4

1 回答 1

1

声明contents如下Button ButtonClick;

String contents = "";

onActivityResult()中,替换:

final String contents = intent.getStringExtra("SCAN_RESULT");

和:

contents = intent.getStringExtra("SCAN_RESULT");

resultonPostExecute(ArrayList<Product> result)productIdList。将您更改onPostExecute()为:

@Override
protected void onPostExecute(ArrayList<Product> result) {
    super.onPostExecute(result);

    if (result != null) {
        for (int i = 0; i < result.size(); i++) {
            if(result.get(i).toString().equals(contents) {

                // Do something

            }
        }
    }
}

此外,以下内容看起来不正确:

public void run() {
    GetProduts GP = new GetProduts();
    txt.setText(GP.execute().toString());
}

而是试试这个:

public void run() {
    new GetProduts().execute();
}

并声明txt如下TextView txt;Button ButtonClick;现在,将以下行添加到您的onPostExecute():

txt.setText(....); // Replace .... with whatever you are trying to set.

编辑1:

要开始另一个活动:

if(result.get(i).toString().equals(contents) {

    // Change "OtherActivity" to the name of the activity you want to start
    Intent newIntent = new Intent(MainActivity.this, OtherActivity.class); 
    newIntent.putExtra("My_Extra", result.get(i).toString());
    startActivity(newIntent);

}

要在其他活动中检索此字符串:

String extraString = getIntent().getStringExtra("My_Extra");
于 2013-08-02T09:05:07.347 回答