简体   繁体   中英

How to display json data in listview with retrofit 2

What I want to do: Populate the List view with names of the Station which i will retrieve in json form from api.

Problem: I'm having problems populating the list view with the Station Names, Not sure what steps to take. When i hit the button nothing happens. Any push to the right direction would be greatly appreciated. Below is my code thus far.

My Station model

public class Station {
    @SerializedName("id")
    int mStationId;
    @SerializedName("name")
    String mStationName;
    @SerializedName("lat")
    double mStationLat;
    @SerializedName("long")
    double mStationLong;

public Station(int mStationId, String mStationName, double mStationLat, double mStationLong) {
    this.mStationId = mStationId;
    this.mStationName = mStationName;
    this.mStationLat = mStationLat;
    this.mStationLong = mStationLong;
}
}

My ServiceGenerator

public class ServiceGenerator {

public static final String API_BASE_URL = "http://keantrolley.bbapi.co/stations/";

private static OkHttpClient httpClient = new OkHttpClient();
private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

public static  <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient).build();
    return retrofit.create(serviceClass);
}
} 

Then my interface

public interface StationClient {
@GET("station")
Call<List<Station>> listStations();
}

My MainActivity

public class MainActivity extends AppCompatActivity {

private ListView stationsListView;
private ArrayAdapter<Station> stationListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    stationsListView = (ListView) findViewById(android.R.id.list);

    stationListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new LinkedList<Station>());
    stationsListView.setAdapter(stationListAdapter);
}

public void getStations(View view) {
    stationListAdapter.clear();
    stationListAdapter.notifyDataSetChanged();

    StationClient stationClient = ServiceGenerator.createService(StationClient.class);
    Call<List<Station>> call = stationClient.listStations();
    call.enqueue(new Callback<List<Station>>() {
        @Override
        public void onResponse(Response<List<Station>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                stationListAdapter.addAll(); //Here is where i believe is the root of the problem
            } else {
                stationListAdapter.addAll();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

}

To get your list you must call to response.body() :

StationClient stationClient = ServiceGenerator.createService(StationClient.class);
    Call<List<Station>> call = stationClient.listStations();
    call.enqueue(new Callback<List<Station>>() {
        @Override
        public void onResponse(Response<List<Station>> response, Retrofit retrofit) {
            if (response.isSuccess()) {
                stationListAdapter.addAll(response.body()); //Here is where i believe is the root of the problem
            } else {
                //Here is when the status code isn't 2XX (200,201,etc)
            }
        }

        @Override
        public void onFailure(Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });

UPDATE

For show properly the station name in your Listview:

ArrayAdapter<Station> adapter = new ArrayAdapter<Station>(this,
           android.R.layout.simple_list_item_1                
            , new LinkedList<Station>()) {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {   

            ((TextView) convertView).setText(getItem(position).mStationName);
            return convertView;
        }            
    };

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