gpt4 book ai didi

java - onPostExecute 似乎没有被调用

转载 作者:行者123 更新时间:2023-12-01 07:28:29 26 4
gpt4 key购买 nike

当我运行我的应用程序时,onPostExecute似乎没有被调用。它没有像应有的那样填充到 UI 中。另外,关于DoInBackground ,任何超过 for 循环的日志消息:

for (int i = 0; i < businesses.length(); i++) { }

不显示该特定 for 循环中的日志消息。例如,第二个 for 循环 for(int j = 0; j < businessNames.size(); j++) { } 中的日志消息由于某种原因没有显示。这是时间问题还是我错过了什么?

但总而言之,我的 onPostExecute 中的 UI没有被击中(据我所知)。

这是我的代码

public class CoffeeResultActivity extends Activity{

ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> businessNames = new ArrayList<String>();
List<String> businessInfo = new ArrayList<String>();
HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();

private int lastExpandedPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

expListView = (ExpandableListView) findViewById(R.id.lvExp);

//Calling AsyncTask
new RetreiveSearchResults().execute("coffee");

// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {

@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});

expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) {
expListView.collapseGroup(lastExpandedPosition);
}

lastExpandedPosition = groupPosition;

}
});

// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
businessNames.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();

}
});

// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
businessNames.get(groupPosition)
+ " : "
+ listDataChild.get(
businessNames.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});

}

class RetreiveSearchResults extends AsyncTask<String, Void, Void> {


@Override
protected Void doInBackground(String... terms) {

// Some example values to pass into the Yelp search service.
String category = "coffee";

// Execute a signed call to the Yelp service.
OAuthService service = new ServiceBuilder().provider(YelpApi2.class).apiKey("key").apiSecret("key").build();
Token accessToken = new Token("key", "key");
OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
request.addQuerystringParameter("location", "Waterfront, Boston, MA");
request.addQuerystringParameter("category", category);
service.signRequest(accessToken, request);
Response response = request.send();
String rawData = response.getBody();



try {
JSONObject json = new JSONObject(rawData);
JSONArray businesses;
businesses = json.getJSONArray("businesses");

for (int i = 0; i < businesses.length(); i++) {
JSONObject business = businesses.getJSONObject(i);
//Log.d("FOO FOO", "FOO FOO FOO" + business.toString());
businessNames.add(business.get("name").toString());

//The following log message gets displayed
Log.d("FOO FOO", "FOO FOO " + businessNames.get(i));

businessInfo.add(business.get("rating").toString());

//The following log message gets displayed
Log.d("FOO FOO", "FOO FOO " + businessInfo.get(i));
}
//The following log message gets displayed
Log.d("FOO FOO", "SIZE" + businessNames.size());
for(int j = 0; j < businessNames.size(); j++) {

//The following log message DOES NOT GET DISPLAYED. But it does run through this for loop in debugger.
Log.d("FOO FOO", "FOO FOO ##### Get Here?);

//In Debugger, listDataChild does get populated.
listDataChild.put(businessNames.get(j), businessInfo);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

protected void onPostExecute() {

//Does not enter onPostExecute in Debugger nor on a Regular run.

//Log message does NOT get printed
Log.d("FOO FOO", "FOO FOO Get in POST?");

listAdapter = new ExpandableListAdapter(CoffeeResultActivity.this, businessNames, listDataChild);
listAdapter.notifyDataSetChanged();

// setting list adapter
expListView.setAdapter(listAdapter);


}
}

}

最佳答案

onPostExecute 的签名错误。应该是这样的

protected void onPostExecute(Void result) {
}

关于java - onPostExecute 似乎没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636974/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com