Android TabLayout并使用LoaderCallbacks填充片段

时间:2016-10-20 01:05:05

标签: java android

所以我正在完成一项任务,我必须创建一个代表不同类别新闻的TabLayout,其中使用Bing搜索API检索新闻并解析JSON并用于填充三个中的ListView构成TabLayout的碎片。我也使用了ViewPager。

我的问题是,出于某种原因,所有三个片段的内容都是相同的......相同的文章结果。为什么是这样?我使用Loader ID,并在onActivityCreated()方法中初始化加载器。有什么方法可以在用户滑动到该选项卡时加载与当前片段相关的文章吗?

以下是我的碎片的相关方法。它们在每个片段中几乎完全相同,但LOADER_ID和CATEGORY_NAME值除外。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    // Get a reference to the ConnectivityManager to check state of network connectivity
    ConnectivityManager connMgr = (ConnectivityManager)
            getContext().getSystemService(Context.CONNECTIVITY_SERVICE);

    // Get details on the currently active default data network
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    // If there is a network connection, fetch data
    if (networkInfo != null && networkInfo.isConnected()) {
        // Get a reference to the LoaderManager, in order to interact with loaders.
        LoaderManager loaderManager = getLoaderManager();

        // Initialize the loader. Pass in the int ID constant defined above and pass in null for
        // the bundle. Pass in this activity for the LoaderCallbacks parameter (which is valid
        // because this activity implements the LoaderCallbacks interface).
        loaderManager.initLoader(WorldFragment.LOADER_ID, null, this);
    } else {
        // Otherwise, display error
        // First, hide loading indicator so error message will be visible
        View loadingIndicator = getActivity().findViewById(R.id.loading_indicator);
        loadingIndicator.setVisibility(View.GONE);

        // Update empty state with no connection error message
        mEmptyStateTextView.setText(R.string.no_internet_connection);
    }
}

@Override
public Loader<List<Article>> onCreateLoader(int id, Bundle args) {
    return new ArticleLoader(this.getContext(), WorldFragment.CATEGORY_NAME);
}

@Override
public void onLoadFinished(Loader<List<Article>> loader, List<Article> articles) {
    // Hide loading indicator because the data has been loaded
    View loadingIndicator = getActivity().findViewById(R.id.loading_indicator);
    loadingIndicator.setVisibility(View.GONE);

    // Set empty state text to display "No articles found."
    mEmptyStateTextView.setText(R.string.no_articles);

    // Clear the adapter of previous earthquake data
    adapter.clear();

    // If there is a valid list of {@link Earthquake}s, then add them to the adapter's
    // data set. This will trigger the ListView to update.
    if (articles != null && !articles.isEmpty()) {
        adapter.addAll(articles);
    }
}

@Override
public void onLoaderReset(Loader<List<Article>> loader) {
    adapter.clear();
}

这是我的ArticleLoader类的源代码。 &#34; fetchArticleData&#34; call是通过将JSON解析为Article对象来检索Article对象的。

public class ArticleLoader extends AsyncTaskLoader<List<Article>> {
    private static final String LOG_TAG = ArticleLoader.class.getName();

    private String category;

    public ArticleLoader(Context context, String category) {
        super(context);
        this.category = category;
    }

    @Override
    protected void onStartLoading() {
        forceLoad();
    }

    @Override
    public List<Article> loadInBackground() {
        if (category == null) {
            return null;
        }

        // Perform the network request, parse the response, and extract a list of earthquakes.
        List<Article> articles = QueryUtils.fetchArticleData(category);
        return articles;
    }
}

根据请求,这是QueryUtils类。

public class QueryUtils {
    private static final String LOG_TAG = QueryUtils.class.getSimpleName();
    private static final String REQUEST_BASE_URL = "https://api.cognitive.microsoft.com/bing/v5.0/news/";
    private static final String API_KEY = "redacted";

    /**
     * Create a private constructor because no one should ever create a {@link QueryUtils} object.
     * This class is only meant to hold static variables and methods, which can be accessed
     * directly from the class name QueryUtils (and an object instance of QueryUtils is not needed).
     */
    private QueryUtils() {
    }

    /**
     * Query the USGS dataset and return a list of {@link Article} objects.
     */
    public static List<Article> fetchArticleData(String category) {
        // Create URL object
        HttpURLConnection conn = createUrlConnection(category);

        // Perform HTTP request to the URL and receive a JSON response back
        String jsonResponse = null;
        try {
            jsonResponse = makeHttpRequest(conn);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Problem making the HTTP request.", e);
        }

        // Extract relevant fields from the JSON response and create a list of {@link Article}s
        List<Article> articles = extractFeatureFromJson(jsonResponse);

        // Return the list of {@link Article}s
        return articles;
    }

    /**
     * Returns new URL object from the given string URL.
     */
    private static HttpURLConnection createUrlConnection(String category) {
        URL url = null;
        HttpURLConnection conn = null;
        try {
            url = new URL(QueryUtils.REQUEST_BASE_URL);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Category", category);
            conn.setRequestProperty("Ocp-Apim-Subscription-Key", QueryUtils.API_KEY);
            conn.setDoInput(true);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Problem building the URL connection ", e);
        }
        return conn;
    }

    /**
     * Make an HTTP request to the given URL and return a String as the response.
     */
    private static String makeHttpRequest(HttpURLConnection conn) throws IOException {
        String jsonResponse = "";

        // If the URL is null, then return early.
        if (conn == null) {
            return jsonResponse;
        }

        InputStream inputStream = null;
        try {
            conn.connect();

            // If the request was successful (response code 200),
            // then read the input stream and parse the response.
            if (conn.getResponseCode() == 200) {
                inputStream = conn.getInputStream();
                jsonResponse = readFromStream(inputStream);
            } else {
                Log.e(LOG_TAG, "Error response code: " + conn.getResponseCode());
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Problem retrieving the article JSON results.", e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
            if (inputStream != null) {
                // Closing the input stream could throw an IOException, which is why
                // the makeHttpRequest(URL url) method signature specifies than an IOException
                // could be thrown.
                inputStream.close();
            }
        }
        return jsonResponse;
    }

    /**
     * Convert the {@link InputStream} into a String which contains the
     * whole JSON response from the server.
     */
    private static String readFromStream(InputStream inputStream) throws IOException {
        StringBuilder output = new StringBuilder();
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String line = reader.readLine();
            while (line != null) {
                output.append(line);
                line = reader.readLine();
            }
        }
        return output.toString();
    }

    /**
     * Return a list of {@link Article} objects that has been built up from
     * parsing the given JSON response.
     */
    private static List<Article> extractFeatureFromJson(String articleJSON) {
        // If the JSON string is empty or null, then return early.
        if (TextUtils.isEmpty(articleJSON)) {
            return null;
        }

        // Create an empty ArrayList that we can start adding articles to
        List<Article> articles = new ArrayList<>();

        // Try to parse the JSON response string. If there's a problem with the way the JSON
        // is formatted, a JSONException exception object will be thrown.
        // Catch the exception so the app doesn't crash, and print the error message to the logs.
        try {
            // Create a JSONObject from the JSON response string
            JSONObject baseJsonResponse = new JSONObject(articleJSON);

            // Extract the JSONArray associated with the key called "features",
            // which represents a list of features (or articles).
            JSONArray articleArray = baseJsonResponse.getJSONArray("value");

            // For each article in the articleArray, create an {@link Article} object
            for (int i = 0; i < articleArray.length(); i++) {

                // Get a single article at position i within the list of articles
                JSONObject currentArticle = articleArray.getJSONObject(i);

                // Extract the value for the key called "name"
                String articleName = currentArticle.getString("name");

                // Extract the value for the key called "url"
                String articleSource = currentArticle.getString("url");

                // Extract the value for the key called "image"
                JSONObject imageObject = currentArticle.getJSONObject("image");
                String imageSource = imageObject.getJSONObject("thumbnail").getString("contentUrl");

                // Create a new {@link Article} object with the name, url, and image
                // from the JSON response.
                Article article = new Article(articleName, articleSource, imageSource);

                // Add the new {@link Article} to the list of articles.
                articles.add(article);
            }

        } catch (JSONException e) {
            // If an error is thrown when executing any of the above statements in the "try" block,
            // catch the exception here, so the app doesn't crash. Print a log message
            // with the message from the exception.
            Log.e("QueryUtils", "Problem parsing the article JSON results", e);
        }

        // Return the list of articles
        return articles;
    }
}

有谁知道我做错了什么?

0 个答案:

没有答案