1
我想做的事:填充列表視圖與站名稱,我將從API檢索json形式。如何顯示列表視圖中的JSON數據與改造2
問題:我在使用站名填充列表視圖時遇到問題,不確定要採取的步驟。當我按下按鈕時,什麼都沒有發生。任何推動到正確的方向將不勝感激。以下是我的代碼到目前爲止。
我站模型
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;
}
}
我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);
}
}
然後我的界面
public interface StationClient {
@GET("station")
Call<List<Station>> listStations();
}
我的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());
}
});
}
}
好吧我得到的名單信息,但不完全是我期待的形式。它看起來像「[email protected]」,而不是隻顯示站名稱前「亨寧斯站」 – Olay
然後你的問題是不改造。您的問題在您的適配器中,您沒有編碼以正確顯示電臺的名稱。 – ClarkXP
感謝您的幫助! – Olay