你好,我想用 recyclerview 列出城市及其温度

hello, I want to list the cities and their temperatures with the recyclerview

我想通过 api 在 recyclerview 中列出城市及其温度,但我看不到数据。我是新手,你能帮我吗?

我的代码:

public class MainActivity extends AppCompatActivity {
    public static String apikey = "blablabla";
    public static String lan = "55.5";
    public static String lon="37.5";
    public static String cnt="10";
    private List<CountryData> list;
    SearchView searchView;
    RecyclerView countries;
    TextView countryName, temperature;
    ImageView image;

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

        searchView = (SearchView) findViewById(R.id.searchView);
        countries = (RecyclerView) findViewById(R.id.countries);
        countryName = (TextView) findViewById(R.id.countryName);
        temperature = (TextView) findViewById(R.id.temperature);
        image = (ImageView) findViewById(R.id.image);


        list = new ArrayList<>();
        countries = findViewById(R.id.countries);
        countries.setLayoutManager(new LinearLayoutManager(this));
        countries.setHasFixedSize(true);


        Adapter adapter = new Adapter(this, list);
        countries.setAdapter(adapter);


        ApiUtilities.getApiInterface().getCountryData(lan,lon,cnt,apikey).enqueue(new Callback<List<CountryData>>(){

            @Override
            public void onResponse(Call<List<CountryData>> call, Response<List<CountryData>> response) {
                if (response.code() == 200) {
                    RetrofitModel retrofitModel = (RetrofitModel) response.body();
                    assert retrofitModel != null;
                    double temp = retrofitModel.main.temp - 273.15;
                    int tempToInt = (int) temp;

                    String country = retrofitModel.sys.country;
                    for(int i=0; i<list.size(); i++){
                        countryName.setText(country);
                    }

                    String temperatures = tempToInt + "°C";
                    temperature.setText(temperatures);

                }
            }

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

            }
        });


    }

}

API 控制器

public interface APIController {

    String BASE_URL = "https://api.openweathermap.org/data/2.5/";
    @GET("find?lat=55.5&lon=37.5&cnt=10&appid=3f8c9db425f5691cb59026f85546237e")
    //Call<List<CountryData>> getCountryData();

    Call<List<CountryData>> getCountryData(@Query("lat") String lat, @Query("lon") String lon, @Query("cnt") String cnt , @Query("APIKey") String apiKey);
}

改造模型

class RetrofitModel {

    @SerializedName("list")
    private String list;
    private String country;
    private String name;
    private String temp;
    @SerializedName("coord")
    public Coord coord;
    @SerializedName("sys")
    public Sys sys;
    @SerializedName("weather")
    public ArrayList<Weather> weather = new ArrayList<Weather>();
    @SerializedName("main")
    public Main main;

    public String getList() {
        return list;
    }

    public void setList(String list) {
        this.list = list;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    public Coord getCoord() {
        return coord;
    }

    public void setCoord(Coord coord) {
        this.coord = coord;
    }

    public Sys getSys() {
        return sys;
    }

    public void setSys(Sys sys) {
        this.sys = sys;
    }

    public ArrayList<Weather> getWeather() {
        return weather;
    }

    public void setWeather(ArrayList<Weather> weather) {
        this.weather = weather;
    }

    public Main getMain() {
        return main;
    }

    public void setMain(Main main) {
        this.main = main;
    }

    public RetrofitModel(String list, String country, String name, String temp, Coord coord, Sys sys, ArrayList<Weather> weather, Main main) {
        this.list = list;
        this.country = country;
        this.name = name;
        this.temp = temp;
        this.coord = coord;
        this.sys = sys;
        this.weather = weather;
        this.main = main;
    }
}

class Weather {
    @SerializedName("id")
    public int id;
    @SerializedName("main")
    public String main;
}

class Main {
    @SerializedName("temp")
    public float temp;
}

class Sys {
    @SerializedName("country")
    public String country;

    public Sys(String country) {
        this.country = country;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

class Coord {
    @SerializedName("lon")
    public float lon;
    @SerializedName("lat")
    public float lat;

}

国家数据

   public class CountryData {

@SerializedName("list")
private String list;
private String country;
private String name;
private String temp;

public CountryData(String list, String country, String name, String temp) {
    this.list = list;
    this.country = country;
    this.name = name;
    this.temp = temp;
}

public String getList() {
    return list;
}

public void setList(String list) {
    this.list = list;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }
}

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@drawable/weather"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="12dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginStart="30dp"
            android:textColor="@color/black"
            android:text="Country Name"
            android:textSize="15dp" ></TextView>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="30dp"
            android:text="Temperature"
            androi

d:textColor="@color/black" android:textSize="15dp" >

</RelativeLayout>

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/countries"
    tools:listitem="@layout/country_item_layout"
    app:layoutManager="LinearLayoutManager"
    />
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardElevation="3dp"
    android:layout_marginHorizontal="8dp"
    android:layout_marginTop="8dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="7dp"
        android:layout_marginHorizontal="15dp">


    <TextView
        android:id="@+id/countryName"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="30dp"
        android:textColor="@color/black"
        android:text="Türkiye"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:textSize="15dp" ></TextView>

    <TextView
        android:id="@+id/temperature"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="30dp"
        android:textColor="@color/black"
        android:text="Temperature"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:textSize="15dp" ></TextView>

    </RelativeLayout>
</com.google.android.material.card.MaterialCardView>

你好,我想通过 api 在 recyclerview 中列出城市及其温度,但我看不到数据。我是新手,你能帮我吗?

您可以在 Volley

中轻松实现
implementation 'com.android.volley:volley:1.2.0'

在Java代码中:

  queue = Volley.newRequestQueue(this);       // RequestQueue

String url = "https://api.openweathermap.org/data/2.5/find?lat=55.5&lon=37.5&cnt=10&appid=3f8c9db425f5691cb59026f85546237e";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("list");
                            for (int i = 0 ; i<jsonArray.length() ; i++){
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                String id = jsonObject.getString("id");
                                String name = jsonObject.getString("name");
                                resultText.append("ID : " + id + "\nName : " + name + "\n\n");    // TextView
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }}
        ) ;
        queue.add(request);

输出: