使用AsyncTasnk并返回值并与其他值进行比较

时间:2013-08-02 08:35:46

标签: android multithreading android-intent android-asynctask

我想从AsyncTask返回一个值,我希望与值contents进行比较,并且如果它们匹配,我想发送值{{1}另一个意图。我希望我能正确解释。

这是代码:

AsyncTasnk

1 个答案:

答案 0 :(得分:1)

contents下声明Button ButtonClick;为:

String contents = "";

onActivityResult()中,替换:

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

使用:

contents = intent.getStringExtra("SCAN_RESULT");
result中的{p> onPostExecute(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");