我正在尝试使用listview内部的改进从服务器中提取数据但未显示任何数据
listview xml
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipeContainer"
tools:context="com.example.prem.weatherapplication.fragment.WeatherFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rl">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_below="@id/wind"></ListView>
</RelativeLayout>
listItem xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/desc"/>
</RelativeLayout>
来自服务器的接口数据
@GET("forecast/daily?")
Call<DailyWeatherResponse> getDailyWeather(@Query("q") String city,
@Query("units") String units,
@Query("cnt") String cnt,
@Query("APPID") String appId
);
适配器类
public class DailyAdapter extends BaseAdapter{
List<DailyList> dailyLists;
Context context;
LayoutInflater inflater;
public DailyAdapter(List<DailyList> dailyLists,Context context){
this.dailyLists = dailyLists;
this.context = context;
}
@Override
public int getCount() {
return dailyLists==null?0:dailyLists.size();
}
@Override
public Object getItem(int position) {
return dailyLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null){
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dailyweather_list,parent,false);
}
TextView desc = (TextView) view.findViewById(R.id.desc);
desc.setText(dailyLists.get(position).getWeather().get(0).getDescription());
return view;
}
}
在片段内进行改造
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_current_weather, container, false);
ButterKnife.bind(this,view);
adapter = new DailyAdapter(dailyList,context);
Weather();
return view;
}
private void Weather(){
ApiInterface apiInterface = ApiClient.getRetrofit().create(ApiInterface.class);
Call<DailyWeatherResponse> dwr = apiInterface.getDailyWeather("Sydney","metric","10",getResources().getString(R.string.api_key));
dwr.enqueue(new Callback<DailyWeatherResponse>() {
@Override
public void onResponse(Call<DailyWeatherResponse> call, Response<DailyWeatherResponse> response) {
dailyList = response.body().getDailyList();
Log.d(TAG,"Number of list received "+dailyList.size());
adapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<DailyWeatherResponse> call, Throwable t) {
t.printStackTrace();
}
在Logcat中没有显示任何错误...提前致谢
答案 0 :(得分:0)
您可以编写如下代码:
public class DailyAdapter extends BaseAdapter{
public void refreshData(List<DailyList> list){
this.dailyLists = list;
notifyDataSetChanged();
}
}
ApiInterface apiInterface = ApiClient.getRetrofit().create(ApiInterface.class);
Call<DailyWeatherResponse> dwr = apiInterface.getDailyWeather("Sydney","metric","10",getResources().getString(R.string.api_key));
dwr.enqueue(new Callback<DailyWeatherResponse>() {
@Override
public void onResponse(Call<DailyWeatherResponse> call, Response<DailyWeatherResponse> response) {
dailyList = response.body().getDailyList();
Log.d(TAG,"Number of list received "+dailyList.size());
adapter.refreshData(dailyList );
}
@Override
public void onFailure(Call<DailyWeatherResponse> call, Throwable t) {
t.printStackTrace();
答案 1 :(得分:0)
看起来你永远不会得到listview并将你的适配器连接到它。在你的片段中你需要做:
Listview list = (ListView) getActivity().findViewById(R.id.listView);
list.setAdapter(adapter);
当你获得数据集时,我实际上会在你的天气方法中这样做。它应该是这样的:
dwr.enqueue(new Callback<DailyWeatherResponse>() {
@Override
public void onResponse(Call<DailyWeatherResponse> call, Response<DailyWeatherResponse> response) {
dailyList = response.body().getDailyList();
Log.d(TAG,"Number of list received "+dailyList.size());
Listview list = (ListView) getActivity().findViewById(R.id.listView);
list.setAdapter(adapter);
}
@Override
public void onFailure(Call<DailyWeatherResponse> call, Throwable t) {
t.printStackTrace();
}