使用Retrofit 2将JSON数据解析为Horizo​​ntalScrollView

时间:2017-06-01 16:26:08

标签: java android json retrofit2 horizontalscrollview

我需要使用Retrofit2将JSON数据解析为Horizo​​ntalScrollView。我成功地将它解析为ListView,但我还没有想象如何在Horizo​​ntalScrollView中做到这一点。谷歌无法给我正确答案。

我的代码使用下面的ListView。

P.S。我想做Horizo​​ntalScrollView而不是ListView。

Like this.

MyForecastAdapter.java

public class MyForecastAdapter extends ArrayAdapter<Forecast> {

List<Forecast> forecastList;
Context context;
private LayoutInflater inflater;

public MyForecastAdapter(Context context, List<Forecast> objects) {
    super(context, 0, objects);
    this.context = context;
    this.inflater = LayoutInflater.from(context);
    forecastList = objects;
}

@Override
public Forecast getItem(int position) {
    return forecastList.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder viewHolder;
    if (convertView == null) {
        View view = inflater.inflate(R.layout.activity_row, parent, false);
        viewHolder = ViewHolder.create((RelativeLayout) view);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Forecast item = getItem(position);

    viewHolder.textViewTemp.setText(item.getMain().getTemp());
    viewHolder.textViewHumidity.setText(item.getMain().getHumidity());

    return viewHolder.relativeLayout;
}

private static class ViewHolder {
    public final RelativeLayout relativeLayout;
    public final TextView textViewTemp;
    public final TextView textViewHumidity;

    private ViewHolder(RelativeLayout relativeLayout, TextView textViewTemp, TextView textViewHumidity) {
        this.relativeLayout = relativeLayout;
        this.textViewTemp = textViewTemp;
        this.textViewHumidity = textViewHumidity;
    }

    public static ViewHolder create(RelativeLayout relativeLayout) {
        TextView textViewTemp = (TextView) relativeLayout.findViewById(R.id.textViewTemp);
        TextView textViewHumidity = (TextView) relativeLayout.findViewById(R.id.textViewHumidity);
        return new ViewHolder(relativeLayout, textViewTemp, textViewHumidity);
    }
}

}

ForecastList.java

public class ForecastList {

@SerializedName("list")
@Expose
private ArrayList<Forecast> forecasts = new ArrayList<>();

public ArrayList<Forecast> getForecasts() {
    return forecasts;
}

public void setForecasts(ArrayList<Forecast> forecasts) {
    this.forecasts = forecasts;
}

}

Forecast.java - getters / setters。

ApiService.java

public interface ApiService {

@GET("/data/2.5/forecast?q=Kirov,ru&appid=/../")
Call<ForecastList> getMyJSON();

}

RetrofitClient.java

public class RetrofitClient {

private static final String ROOT_URL = "http://api.openweathermap.org";

private static Retrofit getRetrofitInstance() {
    return new Retrofit.Builder().baseUrl(ROOT_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
}

public static ApiService getApiService() {
    return getRetrofitInstance().create(ApiService.class);
}

}

MainActivity.java

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

    forecastList = new ArrayList<>();
    parentView = findViewById(R.id.parentLayout);

    listView = (ListView) findViewById(R.id.listView);

    ApiService apiService = RetrofitClient.getApiService();

    Call<ForecastList> call = apiService.getMyJSON();

    call.enqueue(new Callback<ForecastList>() {
        @Override
        public void onResponse(Call<ForecastList> call, Response<ForecastList> response) {
            if (response.isSuccessful()) {
                forecastList = response.body().getForecasts();

                adapter = new MyForecastAdapter(MainActivity.this, forecastList);
                listView.setAdapter(adapter);
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<ForecastList> call, Throwable t) {

        }
    });
}

1 个答案:

答案 0 :(得分:1)

您可以使用带有构造函数的LinearLayoutManager的回收视图使用地平线滚动视图 new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

使用此代码.....

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

    forecastList = new ArrayList<>();
    parentView = findViewById(R.id.parentLayout);


    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

    myList = (RecyclerView) findViewById(R.id.listView);
    myList.setLayoutManager(layoutManager);

    ApiService apiService = RetrofitClient.getApiService();

    Call<ForecastList> call = apiService.getMyJSON();

    call.enqueue(new Callback<ForecastList>() {
        @Override
        public void onResponse(Call<ForecastList> call, Response<ForecastList> response) {
            if (response.isSuccessful()) {
                forecastList = response.body().getForecasts();

                adapter = new MyForecastAdapter(MainActivity.this, forecastList);
                myList.setAdapter(adapter);
            } else {
                Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<ForecastList> call, Throwable t) {

        }
    });
}

并在XML和代码中将ListView更改为RecycleView .....