I am trying to send 3 list from a AsyncTask
class to another. AsyncTask
class takes 3 different datas from json and inserts 3 string list. I have to send this list to another class to insert recycler adapter.
public class FetchNewsData extends AsyncTask<String, String, JSONObject> {
static List<String> title = new ArrayList<>();
static List<String> body = new ArrayList<>();
static List<String> imageUrl = new ArrayList<>();
public List<String> getTitle() {
return title;
}
public List<String> getBody() {
return body;
}
public List<String> getImageUrl() {
return imageUrl;
}
private static String url = "https://content.guardianapis.com/search?format=json&show-fields=thumbnail,trailText&api-key=f627bebe-3c4f-4fee-a2fc-3368a3b098c7";
private static final String TAG_RESPONSE = "response";
private static final String TAG_RESULTS = "results";
private static final String TAG_TITLE = "webTitle";
private static final String TAG_DATE = "webPublicationDate";
private static final String TAG_SECTION_NAME = "sectionName";
private static final String TAG_FIELDS = "fields";
private static final String TAG_TEXT = "trailText";
private static final String TAG_IMG_URL = "thumbnail";
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try {
// Getting JSON Array
JSONObject response = json.getJSONObject(TAG_RESPONSE);
JSONArray result = response.getJSONArray(TAG_RESULTS);
for (int i = 0; i < result.length(); i++) {
JSONObject resultsObjects = result.getJSONObject(i);
JSONObject fields = resultsObjects.getJSONObject(TAG_FIELDS);
// Storing JSON item in a Variable
if (resultsObjects.has(TAG_TITLE)) {
String header = resultsObjects.getString(TAG_TITLE);
title.add(header);
} else title.add(null);
if (fields.has(TAG_TEXT)) {
String text = fields.getString(TAG_TEXT);
body.add(text);
} else body.add(null);
if (fields.has(TAG_IMG_URL)) {
String imgUrl = fields.getString(TAG_IMG_URL);
imageUrl.add(imgUrl);
} else imageUrl.add(null);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
In the following code the RecyclerAdapter
should take that 3 list as parameter. How can I do this. Can someone help me please?
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
List<String> itemList;
itemList = createItemList();
List<String> titleList=new ArrayList<>(itemData.getTitle());
String[] itemlistToArray = new String[itemList.size()];
itemlistToArray = itemList.toArray(itemlistToArray);
String[] asd= {"a","b","c"};
String[] titlelistToArray = new String[titleList.size()];
titlelistToArray = titleList.toArray(titlelistToArray);
recyclerAdapter = new RecyclerAdapter(asd, itemlistToArray, itemlistToArray);
recyclerView.setAdapter(recyclerAdapter);
}
Why dont you call your setupRecyclerView method directly from your onPostExecute method ??
If you do not want that you should store these list within a custom class and pass that class object through intent to other activity.
1) Create a custom class MyLists and set these lists within this class.
2) Implement this class with Serializable.
3) Pass Mylists class object through intent to another activity.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.