Issue
I currently working on a weather app. I think I have successfully parsed the results using GSON but now my recyclerview is empty. I have logs stating that there is something being extracted from the server but nothing is showing up. It looks like my code is correct but I know I'm missing something. Can someone let me know what I am missing?
Here is my fragment:
@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;
}
Here is my 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?
}
}
}
And here is my RecyclerView Adapter:
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();
}
}
and here is my 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
Solution
You need to send your list to recyclerview adapter when data is available in post execute method of async task.
Something like i.e.
adapter.setList(result);
Answered By - Jeel Vankhede
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.