我正在研究一个示例with listview
,其中我想在后台线程上执行网络请求。在我的应用中,我正在使用a recyclerview
。 Android Studio声明无法解析方法.clear
和。addAll
。这样做是不是我使用的是recyclerview而不是listview?还是其他的东西?
根据我的示例,这是我在MainActivity中拥有的内容:
package com.example.android.guardiannews;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.example.android.guardiannews.AdapterClass.NewsAdapter;
import com.example.android.guardiannews.AdapterModel.ItemsModel;
import com.example.android.guardiannews.Utils.QueryUtils;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
/**
* Tag for the log messages
*/
public static final String LOG_TAG = MainActivity.class.getSimpleName();
/**
* URL to query the Guardian's API dataset for articles
*/
private static final String GUARDIAN_REQUEST_URL =
"http://content.guardianapis.com/search?from-date=2019-03-04&to-date=2019-03-04&order-by=newest&show-tags=contributor&show-fields=trailText,thumbnail&page-size=4&api-key=982f19d8-023e-4aa0-842c-ba1970ae9c46";
/**
* Adapter for the list of articles
*/
private NewsAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find a reference to the {@link RecyclerView} in the layout
RecyclerView recyclerView = findViewById(R.id.recycler_view);
// Create a new adapter that takes the list of articles as input
mAdapter = new NewsAdapter(new ArrayList<ItemsModel>(), this);
//Set the adapter
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
// Kick off an {@link ArticleAsyncTask} to perform the network request
ArticleAsyncTask task = new ArticleAsyncTask();
task.execute(GUARDIAN_REQUEST_URL);
}
/**
* {@link AsyncTask} to perform the network request on a background thread, and then
* update the UI with the first article in the response.
*/
private class ArticleAsyncTask extends AsyncTask<String, Void, List<ItemsModel>> {
/**
* This method runs on a background thread and performs the network request.
* We should not update the UI from a background thread, so we return a list of
* {@link ItemsModel}s as the result.
*/
@Override
protected List<ItemsModel> doInBackground(String... urls) {
// Don't perform the request if there are no URLs, or the first URL is null
if (urls.length < 1 || urls[0] == null) {
return null;
}
List<ItemsModel> result = QueryUtils.fetchArticleData(urls[0]);
return result;
}
/**
* This method runs on the main UI thread after the background work has been
* completed. This method receives as input, the return value from the doInBackground()
* method. First we clear out the adapter, to get rid of articles data from a previous
* query. Then we update the adapter with the new list of articles,
* which will trigger the ListView to re-populate its list items.
*/
@Override
protected void onPostExecute(List<ItemsModel> data) {
// Clear the adapter of previous articles data
mAdapter.clear();
// If there is a valid list add them to the adapter's
// data set. This will trigger the RecyclerView to update.
if (data != null && !data.isEmpty()) {
mAdapter.addAll(data);
}
}
}
}