Ich bekomme nicht die Daten in Recyclerview holen Daten vom Server mit Retrofit-Client. Und keinen Wert in "Log.d (TAG," Anzahl der stündlichen empfangenen Wetterdaten "+ list.size()) anzeigen;"Recyclerview zeigt keine Daten
Recyclerview Adapter
public class HourlyAdapter extends RecyclerView.Adapter<HourlyAdapter.HourlyViewHolder> {
List<HourlyList> hourlist;
Context context;
public HourlyAdapter(List<HourlyList> hourlist,Context context){
this.hourlist = hourlist;
this.context = context;
}
@Override
public HourlyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hourly_list,parent,false);
return new HourlyViewHolder(view);
}
@Override
public void onBindViewHolder(HourlyViewHolder holder, int position) {
Date date = new Date(hourlist.get(position).getDt()*1000L);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String formateDate = sdf.format(date);
holder.hour.setText(formateDate);
}
@Override
public int getItemCount() {
Log.d("datasize", String.valueOf(hourlist.size()));
return hourlist == null ? 0 : hourlist.size();
}
public class HourlyViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.hour)
TextView hour;
public HourlyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
}
Fragmentdatei
public class HourlyFragment extends Fragment {
@BindView(R.id.recyclerview)
RecyclerView recyclerView;
List<HourlyList> list;
HourlyAdapter adapter;
public HourlyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_hourly, container, false);
ButterKnife.bind(this,view);
recyclerView.setHasFixedSize(true);
LinearLayoutManager lm = new LinearLayoutManager(getActivity());
lm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(lm);
adapter = new HourlyAdapter(list,getActivity().getApplicationContext());
hourlyWeather();
return view;
}
public void hourlyWeather(){
ApiInterface apiInterface = ApiClient.getRetrofit().create(ApiInterface.class);
Call<HourlyWeather> call = apiInterface.getHourlyWeather("London","metric",getResources().getString(R.string.api_key));
call.enqueue(new Callback<HourlyWeather>() {
@Override
public void onResponse(Call<HourlyWeather> call, Response<HourlyWeather> response) {
list = response.body().getList();
Log.d(TAG,"Number of hourly weather data received "+list.size());
recyclerView.setAdapter(adapter);
}
@Override
public void onFailure(Call<HourlyWeather> call, Throwable t) {
t.printStackTrace();
}
});
}
}
Schnittstellendatei
@GET("forecast?")
Call<HourlyWeather> getHourlyWeather(@Query("q") String city,
@Query("units") String units,
@Query("APPID") String appId);
Retrofit Client
public class ApiClient {
public static final String BASE_URL="http://api.openweathermap.org/data/2.5/";
public static Retrofit retrofit =null;
public static Retrofit getRetrofit(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
XML-Datei
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.prem.weatherapp.fragment.HourlyFragment">
<android.support.v7.widget.RecyclerView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/recyclerview"
android:scrollbars="none"
android:clipToPadding="false">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Liste der Daten XML-Datei ist
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="@android:color/transparent"
android:elevation="0dp"
app:cardPreventCornerOverlap="false"
app:contentPadding="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/date"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/hour"
android:layout_below="@id/date"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
haben Sie den StackTrace auf Fehler überprüft? – MadScientist
können Sie Ihre Antwortstruktur teilen? –
list = response.body(). GetList(); -> Sie aktualisieren den Verweis auf die Liste in HourlyFragment nicht im Adapter. – dtx12