我按照检查中的说明标记回复此处找到的问题: How to return 2 values from a Java method?以便从单个函数中获取两个值。下面是我的主类的一部分,我设置了我需要传递给另一个类的两个变量 我想要的变量是searchURL和searchURLTwo。我可能能够以意图发送它们,但我只是用来发送在另一个活动中使用的数据,不确定将其用于发送到另一个不启动新活动的类。
@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;
}
});
}
我想在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;
}
}
我更新了getIDs代码以显示整个代码,并且doInBackground没有返回值,而是有两个getter方法。正确的URL在personSearchURL [0]和[1]传递。从我的main函数中我调用getData,其中包含用于从api获取搜索结果的所有执行逻辑,它位于我调用getIDs的getData中。以下是相关的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();
我需要知道如何从getIDs获取firstID的返回值到我的getData类中。
答案 0 :(得分:0)
您尚未在任何位置创建类getID的实例。我假设它将在具有两个EditTexts的同一活动中的某个事件处理函数上创建。现在你已经在这个类中有了searchURLOne和searchURLTwo。
你需要调用新的getIDs()。execute(searchURLOne,searchURLTwo);
您还可以按如下方式访问变量
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();
}
}
答案 1 :(得分:0)
在getData类中尝试以下内容 http://developer.android.com/reference/android/os/AsyncTask.html#onPostExecute%28Result%29
这将给出执行AsncTask
的结果