[英]How to display json data in listview with retrofit 2
我想做什么:用Station的名稱填充List視圖,我將從api中以json格式檢索。
問題:我在用工作站名稱填充列表視圖時遇到問題,不確定要采取什么步驟。 當我按下按鈕時,什么也沒有發生。 向正確方向的任何推動將不勝感激。 下面是到目前為止的代碼。
我的車站模型
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;
}
}
我的服務生成器
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);
}
}
然后我的界面
public interface StationClient {
@GET("station")
Call<List<Station>> listStations();
}
我的主要活動
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());
}
});
}
}
要獲取您的列表,您必須調用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
為了在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;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.