繁体   English   中英

如何使用改造2在ListView中显示JSON数据

[英]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.

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