我想從AsyncTask
返回一個值,我想與值contents
進行比較,如果它們匹配,我想將值從AsyncTasnk
發送到另一個意圖。我希望我解釋得很好。使用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();
}
}
}
你試圖從'AsyncTa返回什麼值sk'? – Vikram
productIdList,我想將productIdList與內容進行比較 – mstfdz
請解釋如何比較'ArrayList'和'String'。 – Vikram