mstfdz
mstfdz

Reputation: 2806

Using AsyncTasnk and return value and compare with other value

I want to return a value from AsyncTask and I want to compare with the value contents and at the and if they match I want to send the value where I take it from AsyncTasnk to the other intent. I hope I explained it properly.

and this is the code :

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

    }
}

Upvotes: 0

Views: 73

Answers (1)

Vikram
Vikram

Reputation: 51571

Declare contents under Button ButtonClick; as:

String contents = "";

In onActivityResult(), replace:

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

with:

contents = intent.getStringExtra("SCAN_RESULT");

result in onPostExecute(ArrayList<Product> result) is productIdList. Change your onPostExecute() to:

@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

            }
        }
    }
}

Also, the following doesn't look correct:

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

Instead try this:

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

And declare txt as TextView txt; under Button ButtonClick;. Now, add the following line to your onPostExecute():

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

Edit 1:

To start another activity:

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

}

To retrieve this String in the other activity:

String extraString = getIntent().getStringExtra("My_Extra");

Upvotes: 1

Related Questions