简体   繁体   中英

fetch data from server using retrofit not show in listview

I am trying to pulling data from server using retrofit inside listview but not shown any data

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>

interface data from server

@GET("forecast/daily?")
Call<DailyWeatherResponse> getDailyWeather(@Query("q") String city,
                                           @Query("units") String units,
                                           @Query("cnt") String cnt,
                                           @Query("APPID") String appId
                                           );

Adapter class

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;
}

}

retrofit inside fragment

@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();

}

In Logcat nothing error is shown...Thanks in advance

you can write code like follow:

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();

It looks like you never get the listview and attach your adapter to it. In your fragment you need to do:

Listview list = (ListView) getActivity().findViewById(R.id.listView);
list.setAdapter(adapter);

I would actually do this in your weather method, when you get the data set back. It should look like this:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM