将AsyncTask与RecyclerView一起使用:无结果,但asynctask似乎正在工作

时间:2018-08-26 21:54:53

标签: android android-recyclerview android-asynctask gson

我目前正在使用气象应用程序。我想我已经使用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;
}

这是我的Asynctask:

   @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

1 个答案:

答案 0 :(得分:0)

当异步任务的后执行方法中有可用数据时,您需要将列表发送到recyclerview适配器。

类似的东西

adapter.setList(result);