将 AsyncTask 与 RecyclerView 一起使用:没有结果,但 asynctask 似乎正在工作
Posted
技术标签:
【中文标题】将 AsyncTask 与 RecyclerView 一起使用:没有结果,但 asynctask 似乎正在工作【英文标题】:Using AsyncTask with RecyclerView: no results but asynctask appears to be working 【发布时间】:2019-02-01 11:21:15 【问题描述】:我目前正在开发一个天气应用程序。我想我已经使用 GSON 成功解析了结果,但现在我的 recyclerview 是空的。我有日志说明从服务器中提取了某些内容,但没有显示任何内容。看起来我的代码是正确的,但我知道我遗漏了一些东西。有人可以让我知道我错过了什么吗?
这是我的片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_weather_app, container, false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));
Log.d("debugMode", "The application stopped after this");
adapter = new RecyclerViewAdapter(items, mRecyclerView.getContext());
mRecyclerView.setAdapter(adapter);
new GetWeatherAync(this, mStatusView, api_key).execute();
return view;
这是我的异步任务:
@Override
protected List<ForecastWeatherList> doInBackground(Context...params)
try
Log.d("debugMode", "The application is in doInBackground");
URL url = new URL(serviceUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200)
Log.e(TAG, "Response code:" + urlConnection.getResponseCode());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
Log.e("response",bufferedReader.toString());
Gson gson = new Gson();
if (!bufferedReader.equals(""))
ForecastWeatherListWrapper weatherWrapper = gson.fromJson(bufferedReader, ForecastWeatherListWrapper.class);
Log.e("something", weatherWrapper.getforecastWeatherLists().size() + "");
List<ForecastWeatherList> forecastWeatherLists = weatherWrapper.getforecastWeatherLists();
return forecastWeatherLists;
else
Log.e(TAG, "Error response code: " + urlConnection.getResponseCode());
catch (Exception e)
Log.e("catch","error");
System.out.println(e.getMessage());
return null;
@Override
protected void onPostExecute(List<ForecastWeatherList> result)
super.onPostExecute(result);
if (result != null)
Log.e(TAG, "populate UI recycler view with gson converted data");
listener.afterSearch(result);
else
Log.e(TAG, "Result is null");
// check if this Log shows up?
这是我的 RecyclerView 适配器:
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ForecastRecycler>
List<ForecastWeatherList> mForecastWeatherDataList;
public static class ForecastRecycler extends RecyclerView.ViewHolder
public TextView currentTemp;
public TextView currentHumidity;
public TextView currentDescription;
public ImageView currentIcon;
public ForecastRecycler (View view)
super (view);
currentTemp = (TextView) view.findViewById(R.id.current_temperature);
currentHumidity = (TextView) view.findViewById(R.id.current_humidity);
currentDescription = (TextView) view.findViewById(R.id.current_weather_description);
currentIcon = (ImageView) view.findViewById(R.id.current_weather_icon);
public RecyclerViewAdapter(List<ForecastWeatherList> mForecastWeatherDataList, Context mContext)
this.mForecastWeatherDataList = mForecastWeatherDataList;
@Override
public ForecastRecycler onCreateViewHolder(ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
final ForecastRecycler currentRecycler = new ForecastRecycler(view);
return currentRecycler;
@Override
public void onBindViewHolder( ForecastRecycler holder, int position)
final ForecastWeatherList currentRecycler = mForecastWeatherDataList.get(position);
holder.currentTemp.setText((Double.toString(currentRecycler.getMain().getTemp())));
holder.currentHumidity.setText(currentRecycler.getMain().getHumidity());
holder.currentDescription.setText(currentRecycler.getWeather().getDescription());
Picasso.with(holder.currentIcon.getContext()).load(currentRecycler.getWeather().getIcon());
@Override
public int getItemCount()
return mForecastWeatherDataList.size();
这是我的 LogCat:
ksburneytwo.weathertest E/GetWeatherAync: Response code:200
ksburneytwo.weathertest E/response: java.io.BufferedReader@3d54b14
ksburneytwo.weathertest E/something: 40
ksburneytwo.weathertest E/GetWeatherAync: populate UI recycler view with gson converted data
【问题讨论】:
什么是listener.afterSearch(result);
【参考方案1】:
当异步任务的后执行方法中有数据可用时,您需要将列表发送到 recyclerview 适配器。
类似的东西
adapter.setList(result);
【讨论】:
我可以将适配器传递给 postexecute 方法,但由于某种原因,我无法将结果传递给适配器。以上是关于将 AsyncTask 与 RecyclerView 一起使用:没有结果,但 asynctask 似乎正在工作的主要内容,如果未能解决你的问题,请参考以下文章