我知道这个问题问过很多次但是我找不到错误
我检查了所有事情我做错了什么但是我找不到我做错了什么。
我正在使用back4App作为后端。
我只是从中检索数据,只想在我的列表视图中显示。
我设置了日志以检查我正在检索的数据,因此它在doInbackground方法中显示完美的arraylist大小。但是当我将它返回到postexecute并且我也检查它的大小。但它在那里显示0。 不知道我做了什么错误。
我的代码如下:
public class HomeActivity extends AppCompatActivity {
ProgressDialog mProgressDialog;
ArrayList<String> items;
ListView listView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
listView = (ListView) findViewById(R.id.listview);
items=new ArrayList<>();
new GetServices().execute();
}
private class GetServices extends AsyncTask<Void, Void, ArrayList<String>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(HomeActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Parse.com Simple ListView Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected ArrayList<String> doInBackground(Void... params) {
// Locate the class table named "services" in Parse.com
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("services");
query.orderByAscending("updatedAt");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
items.clear();
Log.i("TAG", "datasize=>" + objects.size());
List<String> temp = new ArrayList<String>();
for (ParseObject services : objects) {
temp.clear();
items.add(services.getString("serviceTitle"));
temp = services.getList("serviceDetails");
items.add(Util.getHtmlText(temp));
}
Log.i("TAG", "itemsize=>" + items.size());
} else {
e.printStackTrace();
}
}
});
return items;
}
@Override
protected void onPostExecute(ArrayList<String> strings) {
super.onPostExecute(strings);
Log.i("TAG", "resultsize=>" + strings.size());
for (int i = 0; i < strings.size(); i++) {
Log.i("TAG", "==>" + strings.get(i));
}
// Pass the results into an ArrayAdapter
CustomServiceListAdapter adapter = new CustomServiceListAdapter(HomeActivity.this, R.layout.custom_fragment_service_list_item, strings);
// Binds the Adapter to the ListView
listView.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
// Capture button clicks on ListView items
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Send single item click data to SingleItemView Class
Intent i = new Intent(HomeActivity.this,
DetailsActivity.class);
// Pass data "name" followed by the position
i.putExtra("data", items.get(position));
// Open SingleItemView.java Activity
startActivity(i);
}
});
}
}}
我的Logcat显示如下:
05-20 13:10:28.661 19099-19099/com.example.apexweb I/TAG: resultsize=>0
05-20 13:10:29.409 19099-19099/com.example.apexweb I/TAG: datasize=>4
05-20 13:10:29.410 19099-19099/com.example.apexweb I/TAG: itemsize=>8
答案 0 :(得分:3)
根据文档findInBackground
从后台线程中的源检索满足此查询的ParseObject
列表,并在回调中获取结果。 doInBackground
不会等到调用带有结果的回调。这就是onPostExecute中大小仍为0的原因。由于doInBackground
已经在另一个线程上运行,因此不需要第二级异步。您可以直接致电find()
。或者,您可以摆脱AsyncTask并使用UI线程中的回调运行findInBackground