0

別のクラスの asynctask からメイン アクティビティにデータを戻すのに問題があります。

私の主な活動は次のとおりです。

public class Main extends Activity {
EditText searchOne;
EditText searchTwo;

Button findMovies;

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

    //set the UI elements
    searchOne = (EditText) findViewById(R.id.searchOne);
    searchTwo = (EditText) findViewById(R.id.searchTwo);

    findMovies = (Button) findViewById(R.id.findMovies);


}
public void displayResults(View view){
    //do something in response to button
    Intent intent = new Intent(this, DisplayResultsActivity.class);

    //make person search url1
    final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne)); 
    final String searchURLOne = personSearchURLOne.toString();
    Log.d("searchurlone", searchURLOne.toString());

    //make person search url2
    final StringBuilder personSearchURLTwo = new StringBuilder(getName.getName2(searchTwo));
    final String searchURLTwo = personSearchURLTwo.toString();

    //get ID 1 this needs to get data back from asynctask in the type string to be passed to buildCreditURL
    new getFirstID().execute(searchURLOne);

    //get ID 2
    new getFirstID().execute(searchURLTwo);

    //make credit search url1
    final StringBuilder creditURLOne = new StringBuilder(buildCreditURL.getCreditURLOne(firstID));

AsyncTask を含む別のクラス ファイルを次に示します。

package com.tot.tipofthetongue;

import android.os.AsyncTask;

import android.util.Log;





import org.json.JSONArray;
import org.json.JSONException;

import org.json.JSONObject;

public class getFirstID extends AsyncTask<String, Void, String> {

public static String TAG_ID = "id";
public static String TAG_RESULTS = "results";


String personOne = null;
static String firstID = null;


@Override
protected String doInBackground(String... personSearchURLOne) {
    Log.d("personSearchURLOne in getFirstID containts", personSearchURLOne[0]);

    JSONArray results = null;

    JSONParser jParser = new JSONParser();

    JSONObject jSon = jParser.doInBackground(personSearchURLOne[0]);

    try{
        results = jSon.getJSONArray(TAG_RESULTS);

        for(int i = 0; i < results.length(); i++){
            JSONObject r = results.getJSONObject(i);
            firstID = r.getString(TAG_ID);

        }


    }
    catch(JSONException e){
        Log.e("Error", e.toString());
    }
            /need this value as string to be passed back to the main activity
    return firstID;


}




}

最初に文字列としてメイン アクティビティに ID アウトする方法は、別の別のクラス ファイルに属する別のメソッドで使用できます。

更新された getFirstID:

package com.tot.tipofthetongue;


import android.os.AsyncTask;

import android.util.Log;





import org.json.JSONArray;
import org.json.JSONException;

import org.json.JSONObject;

 public class getFirstID extends AsyncTask<String, Void, String> {

public static String TAG_ID = "id";
public static String TAG_RESULTS = "results";


String personOne = null;
static String firstID = null;


@Override
protected String doInBackground(String... personSearchURLOne) {

    Log.d("personSearchURLOne in getFirstID containts", personSearchURLOne[0]);

    JSONArray results = null;

    JSONParser jParser = new JSONParser();

    JSONObject jSon = jParser.doInBackground(personSearchURLOne[0]);

    try{
        results = jSon.getJSONArray(TAG_RESULTS);

        for(int i = 0; i < results.length(); i++){
            JSONObject r = results.getJSONObject(i);
            firstID = r.getString(TAG_ID);

        }


    }
    catch(JSONException e){
        Log.e("Error", e.toString());
    }
    return firstID;


}
@Override
public void onPostExecute(String firstID){

}




}
4

3 に答える 3

2

非常に簡単な方法は、これを行うことです。

public class getFirstID extends AsyncTask<String, Void, Pair<String, String>> {
    public static interface MyAsyncCallback {
        /**
         * Called when the AsyncTask is finished processing the url.
         * @param url the url that the firstId was from
         * @param firstId the id that was parsed out of the json object
         */
        public void setFirstId(String url, String firstId);
    }

    private final MyAsyncCallback mCallback;

    public getFirstID(MyAsyncCallback callback) {
       mCallback = callback;
    }

   public Pair<String,String> doInBackground(String... params) {
       String firstID = parseJsonForID(params); // I am not going to copy your alg into here.
       return Pair.create(params[0], firstId);
   }


   public void onPostExecute(Pair<String,String> urlAndFirstId) {
     mCallback.setFirstId(urlAndFirstId.first, urlAndFirstId.second);
   }
}

public class Main extends Activity implements MyAsyncCallback {
  ....

 public void setFirstId(String url, String firstID) {
      // Example
      Intent intent = new Intent(this, OtherActivity.class);
      intent.putExtra("firstId", firstId);
      intent.putExtra("url", url);
      startActivity(intent);
 }
}
于 2012-09-16T19:06:58.547 に答える
2

クラスでメソッドをオーバーライドonPostExecute(String result)します。getFirstID

@Override protected void onPostExecute(String result) {
    // Invoke method on Main activity or do other action.
}

このメソッドは UI スレッドによって実行され、ビューを更新できます。コンストラクターで渡すことにより、クラスMainActivityのフィールドに設定することにより、への参照を追加できます。getFirstID

それが役に立てば幸い!

于 2012-09-16T19:06:14.787 に答える
1

インテントを使用して別のアクティビティに送信できます。

Intent intent = new Intent(this, YourActivity.class);
intent.putExtra("name", string);

ここでそれについて読んでください

または、アクティビティ クラス内に AsyncTask クラスを配置することもできます。

public class Main extends Activity {

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


}

private class Download extends AsyncTask <Void, Void, String> {

    protected String doInBackground(Void...params) {

    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {

    }

}

}
于 2012-09-16T19:05:55.553 に答える