简体   繁体   English

如何将变量从一个类传递到另一个类以在包装器中使用

[英]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? 我正在按照对以下问题的复选标记响应中的说明进行操作: 如何从Java方法返回2个值? 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. 下面是我的主类的一部分,我设置了两个变量我需要传递给另一个类我想要的变量是searchURL和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: 我想在jSon = new JSONParser的以下代码中使用searchURLOne:

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. 我更新getIDs代码显示整个代码和doInBackground不返回值,而不是有两个getter方法。 The right URLs are passed in at personSearchURL[0] and [1]. 正确的URL在personSearchURL [0]和[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. 从我的主要功能我称之为的getData其中包含了从API它是在的getData,我称之为getIDs,让搜索结果中的所有执行逻辑的。 Below is the relevant getData code: 以下是相关的getData代码:

 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. 我需要知道如何将getIDs的返回值fo firstID获取到我的getData类中。

You have not created instance of class getIDs anywhere. 您尚未在任何地方创建类getID的实例。 I assume it will be created on some event handler function in the same activity that has the two EditTexts. 我假设它将在具有两个EditTexts的同一活动中的某些事件处理函数上创建。 Now you already have searchURLOne and searchURLTwo in this class. 现在,您已经在此类中具有searchURLOne和searchURLTwo。

You need to call new getIDs().execute(searchURLOne,searchURLTwo); 您需要调用新的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 在您的getData类http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute%28Result%29中尝试以下操作

this will give the result of execution of the AsncTask 这将给出执行AsncTask的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取从一个类到另一个Action Listener类的变量信息? - How do I get information of the variables from one class to another Action Listener class? 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class? 如何从另一个类中的Java类调用变量 - How do I call variables from a Java class in another Class 如何将变量从Java类调用到另一个类? - How do I call variables from a Java class into another Class? 如何从一个 GUI class 获取变量传递给另一个? - How to get variables from one GUI class passed into another? 如何在Java中将变量从一类传递到另一类? - How can I pass variables from one class to another in Java? Java-如何获取从一个类中的方法生成到另一个类中的变量? - Java - How do I get a variable generated from within a method in one class to another class? 如何从一个类中的JtextField输入到另一个类中的ArrayList的输入。 爪哇 - How do I get input from a JtextField in one class to an ArrayList in another class. Java 如何从 Java 中的另一个类访问变量? - How do I access variables from another class in Java? 如何从另一个 class 调用一个 class 的方法? - How do I call a method of one class from another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM