在doinbackground
方法的异步任务中,我获得了null
List
FeedItems
的{{1}}返回值。
以下是我的asynctask
班级代码。如何归还List
FeedItems
?我已经在for循环中向List<FeedItem>
添加了项目。如何从for循环内部(或任何其他方式)返回List的值?
private class AsynFetchTask extends AsyncTask<Void, FeedItem, List<FeedItem>> {
private int count = 0;
@Override
protected List<FeedItem> doInBackground(Void... params) {
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("NewsAndUpdates");
parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> list, ParseException e) {
if (e==null){
List<FeedItem> feedItems= new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
final FeedItem item = new FeedItem();
item.setTitle(list.get(i).getString("Title"));
item.setDescription(list.get(i).getString("Description"));
item.setTime(list.get(i).getDate("createdAt"));
ParseFile file = list.get(i).getParseFile("File");
file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, ParseException e) {
if (e==null){
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
item.setThumbnail(bmp);
publishProgress(item);
}else {
Toast.makeText(MainActivity.this, R.string.No_Internet,Toast.LENGTH_LONG).show();
}
}
});
if (item != null) {
feedItems.add(item);
}
}
}else {
Toast.makeText(MainActivity.this, R.string.No_Internet,Toast.LENGTH_LONG).show();
}
}
});
return null;
}
@Override
protected void onProgressUpdate(FeedItem... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(List<FeedItem> feedItems) {
super.onPostExecute(feedItems);
adapter=new RecyclerViewAdapter(MainActivity.this,feedItems);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}
}
答案 0 :(得分:0)
不要在doInBackground中返回null,而是返回这样的feedItem:
private class AsynFetchTask extends AsyncTask<Void, FeedItem, List<FeedItem>> {
private int count = 0;
@Override
protected List<FeedItem> doInBackground(Void... params) {
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("NewsAndUpdates");
parseQuery.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(final List<ParseObject> list, ParseException e) {
if (e==null){
List<FeedItem> feedItems= new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
final FeedItem item = new FeedItem();
item.setTitle(list.get(i).getString("Title"));
item.setDescription(list.get(i).getString("Description"));
item.setTime(list.get(i).getDate("createdAt"));
ParseFile file = list.get(i).getParseFile("File");
file.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, ParseException e) {
if (e==null){
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
item.setThumbnail(bmp);
publishProgress(item);
}else {
Toast.makeText(MainActivity.this, R.string.No_Internet,Toast.LENGTH_LONG).show();
}
}
});
if (item != null) {
feedItems.add(item);
}
}
}else {
Toast.makeText(MainActivity.this, R.string.No_Internet,Toast.LENGTH_LONG).show();
}
}
});
return feedItems;
}
@Override
protected void onProgressUpdate(FeedItem... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(List<FeedItem> feedItems) {
super.onPostExecute(feedItems);
adapter=new RecyclerViewAdapter(MainActivity.this,feedItems);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
}
}