[英]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.