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

Parse JSON data into HorizontalScrollView with Retrofit 2

我需要使用 Retrofit2 将 JSON 数据解析为 Horizo​​ntalScrollView。解析成ListView成功了,想不出来怎么解析成Horizo​​ntalScrollView。 Google 不能给我正确答案。

下面是我的 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) {

        }
    });
}

您可以使用带构造函数的 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 ......