我没有使用retrofit客户端从recyclerview获取来自服务器的数据。并且没有在" Log.d中显示任何值(TAG,"收到的每小时天气数据数量" + list.size());"
Recyclerview适配器
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);
}
}
}
片段文件
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();
}
});
}
}
接口文件
@GET("forecast?")
Call<HourlyWeather> getHourlyWeather(@Query("q") String city,
@Query("units") String units,
@Query("APPID") String appId);
改造客户
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文件
<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>
数据列表xml文件
<?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>
答案 0 :(得分:0)
此问题是您在填充列表之前设置了适配器。您可以在从列表中获得响应之前创建适配器。您必须记住,当您提出改装请求时,您必须等待响应。否则,您只是向适配器发送一个空列表。
你应该做更多这样的事情:
@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);
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();
adapter = new HourlyAdapter(list,getActivity().getApplicationContext());
Log.d(TAG,"Number of hourly weather data received "+list.size());
recyclerView.setAdapter(adapter);
}
答案 1 :(得分:0)
试试这个,创建一个回调接口
String.Replace()
然后
public interface callbackInterface {
public void onSuccess(yourList);
}
在hourlyWeatherMethod
hourlyWeather(new callbackInterface() {
@Override
public void onSuccess(yourList) {
//Set your adapter here.
}
}
答案 2 :(得分:0)
我建议使用Android SELECT
r.id AS review_id,
COALESCE(c.name, 'N/A') AS name
FROM reviews r
LEFT JOIN customers c
ON c.id = r.customer_id;
&amp ;;处理Retrofit网络通话。 LoaderManager
,而不是来自AsyncTaskLoader
。
随意尝试这种方法:
1)更改onCreateView
HourlyFragment
2)添加新的import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
public class HourlyFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<HourlyList>> {
final List<HourlyList> list = new ArrayList<>();
HourlyAdapter adapter;
.....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
......
adapter = new HourlyAdapter(this.list, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
return view;
}
@Override
public void onResume() {
super.onResume();
getLoaderManager().initLoader(0, null, this);
}
@Override
public Loader<List<HourlyList>> onCreateLoader(int arg0, Bundle arg1) {
return new HourlyWeatherLoader(this.getActivity());
}
@Override
public void onLoadFinished(Loader<List<HourlyList>> arg0, List<HourlyList> datas) {
this.list.clear();
this.list.addAll(datas);
// this shall update recyclerview to show newly added data
this.adapter.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<List<HourlyList>> arg0) {
}
类来处理Retrofit网络呼叫
HourlyWeatherLoader
希望这有帮助!