将asynctask类中的3个列表发送到另一个类

时间:2017-11-26 20:33:15

标签: java android list android-asynctask recycler-adapter

我正在尝试从AsyncTask课程向另一个课程发送3个列表。 AsyncTask类从json获取3个不同的数据并插入3个字符串列表。我必须将此列表发送到另一个类以插入回收器适配器。

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();
    }


}
}

在以下代码中,RecyclerAdapter应将该3列表作为参数。我怎样才能做到这一点。有人能帮帮我吗?

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);
}

1 个答案:

答案 0 :(得分:0)

为什么不直接从onPostExecute方法调用setupRecyclerView方法?

如果您不希望将这些列表存储在自定义类中,并将该类对象通过意图传递给其他活动。

  

1)创建一个自定义类MyLists并在此类中设置这些列表。

     

2)使用Serializable实现此类。

     

3)通过Mylists   通过对另一个活动的意图来实现类对象。