简体   繁体   中英

How do I get variables from one class to another to be used in a wrapper

I was following the instructions found in the check marked response to the question found here: How to return 2 values from a Java method? in order to get two values back from a single function. Below is a portion of my main class where I set the two variables I need to pass to another class The variables I want are searchURL and searchURLTwo. I might be able to send them with an intent but I have only used to send data which is used in another activity not sure who to use it to just send to another class that doesn't start a new activity.

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

    searchOne.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            //make person search url1
            final StringBuilder personSearchURLOne = 
                     new StringBuilder(getName.getName1(searchOne)); 
            searchURLOne = personSearchURLOne.toString();

            return false;
        }
    });

    searchTwo.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            //make person search url2
            final StringBuilder personSearchURLTwo = 
                   new StringBuilder(getName.getName2(searchTwo));
            searchURLTwo = personSearchURLTwo.toString();

            return false;
        }
    }); 
}

I want to use searchURLOne in the code below at jSon = new JSONParser:

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

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



JSONArray results;
JSONObject jSon;
JSONObject jSon2;
public String firstID;
public String secondID;

@Override
protected Void doInBackground(String... personSearchURLOne) {
    // TODO Auto-generated method stub
    Log.d("params 0 in getIDs", personSearchURLOne[0]);
    try{
        jSon = new JSONParser().execute(personSearchURLOne[0]).get();

        results = jSon.getJSONArray(TAG_RESULTS);

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

        jSon2 = new JSONParser().execute(personSearchURLOne[1]).get();

        results = jSon2.getJSONArray(TAG_RESULTS);

        for(int j=0; j<results.length(); j++){
            JSONObject r2 = results.getJSONObject(j);
            secondID = r2.getString(TAG_ID);

            }
        }


    }catch(InterruptedException e1){
        e1.printStackTrace();
    }catch(ExecutionException e1){
        e1.printStackTrace();
    }catch(JSONException e){
        Log.e("Error", e.toString());
    }
    return null;

}
  ublic String getFirstID(){
  return firstID;
  }
  public String getSecondID(){
  return secondID;
  }


}

I update the getIDs code to show the entire code and that doInBackground does not return a value instead there are two getter methods. The right URLs are passed in at personSearchURL[0] and [1]. From my main function I call getData which contains all of the execution logic for getting the search results from the api it is in getData that I call getIDs. Below is the relevant getData code:

 public class getData extends AsyncTask<String, Void, ArrayList<String> > {

String idOne;
String idTwo;
ArrayList<String> titleOne;
ArrayList<String> titleTwo;
ArrayList<String> myCommonFilms;

public static getIDs getMyIds(){

    return new getIDs();
}

/*public static Titles getMyTitles(String title){
    return new Titles();
}*/

protected ArrayList<String> doInBackground(String... params) {
    Log.d("params in doinbackground contains", params[0]);
    Log.d("params in doinbackground contains", params[1]);

    new getIDs().execute(params[0], params[1]);

    //get ID 1 
    /*getIDs idholder = new getIDs().execute(params[0], params[1]);*/
    getIDs id1 = getMyIds();
    idOne = id1.getFirstID();

I need to know how to get the return value fo firstID from getIDs into my getData class.

You have not created instance of class getIDs anywhere. I assume it will be created on some event handler function in the same activity that has the two EditTexts. Now you already have searchURLOne and searchURLTwo in this class.

You need to call new getIDs().execute(searchURLOne,searchURLTwo);

Also you will be able to access the variables as follows

protected Void doInBackground(String... personSearchURLOne) {
    // TODO Auto-generated method stub
    try{
            String searchURLOne = personSearchURLOne[0];
            String searchURLTwo = personSearchURLOne[1];
            /*this needs to be reference to searchURLOne*/
        jSon = new JSONParser().execute().get();

       }       
}

Try out the following in your getData class http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute%28Result%29

this will give the result of execution of the AsncTask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM