I tried to create a list of elements using JSON and RecyclerView but I got some errors and I dont know how to fix them
I got this error ‘LinearLayoutManager(android.content.Context, int, boolean)’ in ‘androidx.recyclerview.widget.LinearLayoutManager’ cannot be applied to ‘(WeatherActivity.GetUrlData, int, boolean)’
public class WeatherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather);
int District = getIntent().getIntExtra("District", -1);
String latitude="";
String longitude="";
switch (District) {
case 0:
latitude = "55.753600";
longitude = "37.621184";
break;
case 1:
latitude = "55.838390";
longitude = "37.525774";
break;
case 2:
latitude = "55.854875";
longitude = "37.632565";
break;
case 3:
latitude = "55.787715";
longitude = "37.775631";
break;
case 4:
latitude = "55.692019";
longitude = "37.754592";
break;
case 5:
latitude = "55.622014";
longitude = "37.678065";
break;
case 6:
latitude = "55.662735";
longitude = "37.576187";
case 7:
latitude = "55.778003";
longitude = "37.443533";
break;
case 8:
latitude = "55.829370";
longitude = "37.451555";
break;
case 9:
latitude = "55.987583";
longitude = "37.194250";
break;
case 10:
latitude = "55.355802";
longitude = "37.146999";
break;
case 11:
latitude = "55.594351";
longitude = "37.371452";
break;
default:
break;
}
String url = "https://api.open-meteo.com/v1/forecast?latitude=" + latitude + "&longitude=" + longitude + "&hourly=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation_probability,snowfall,cloud_cover,wind_speed_10m&wind_speed_unit=ms&timezone=Europe%2FMoscow&forecast_days=1";
new GetUrlData().execute(url);
}
private class GetUrlData extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... strings) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url1 = new URL(strings[0]);
connection =(HttpURLConnection) url1.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine())!=null){
buffer.append(line).append("n");
}
return buffer.toString();
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if(connection!=null){
connection.disconnect();
}
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}return null;
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
if(result!=null) {
try {
JSONObject object = new JSONObject(result);
JSONArray timeArray = object.getJSONObject("hourly").getJSONArray("time");
JSONArray temperatureArray = object.getJSONObject("hourly").getJSONArray("temperature_2m");
JSONArray humidityArray = object.getJSONObject("hourly").getJSONArray("relative_humidity_2m");
JSONArray precipitationArray = object.getJSONObject("hourly").getJSONArray("precipitation_probability");
JSONArray cloudArray = object.getJSONObject("hourly").getJSONArray("cloud_cover");
JSONArray windSpeedArray = object.getJSONObject("hourly").getJSONArray("wind_speed_10m");
JSONArray snowArray = object.getJSONObject("hourly").getJSONArray("snowfall");
for (int i = 0; i < timeArray.length(); i++) {
try {
String time = timeArray.getString(i);
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
SimpleDateFormat outputFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm");
Date date = inputFormat.parse(time);
String outputString = outputFormat.format(date);
String temperature = temperatureArray.getString(i);
String humidity = humidityArray.getString(i);
double precipitation = precipitationArray.getDouble(i);
double cloud = cloudArray.getDouble(i);
String windSpeed = windSpeedArray.getString(i);
String snowfall = snowArray.getString(i);
ArrayList<Condition> conditions = new ArrayList<>();
conditions.add(new Condition(outputString, temperature, humidity, precipitation, cloud, windSpeed, snowfall));
RecyclerView rcView = findViewById(R.id.recyclerView2);
WeatherAdapter adapter = new WeatherAdapter(conditions);
rcView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
rcView.setAdapter(adapter);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
public class Condition {
private String time;
private String temperature;
private String humidity;
private double precipitation;
private double cloud;
private String windSpeed;
private String snowfall;
public Condition(String time, String temperature, String humidity, double precipitation, double cloud, String windSpeed, String snowfall) {
this.time = time;
this.temperature = temperature;
this.humidity = humidity;
this.precipitation = precipitation;
this.cloud = cloud;
this.windSpeed = windSpeed;
this.snowfall = snowfall;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getHumidity() {
return humidity;
}
public void setHumidity(String humidity) {
this.humidity = humidity;
}
public double getPrecipitation() {
return precipitation;
}
public void setPrecipitation(double precipitation) {
this.precipitation = precipitation;
}
public double getCloud() {
return cloud;
}
public void setCloud(double cloud) {
this.cloud = cloud;
}
public String getWindSpeed() {
return windSpeed;
}
public void setWindSpeed(String windSpeed) {
this.windSpeed = windSpeed;
}
public String getSnowfall() {
return snowfall;
}
public void setSnowfall(String snowfall) {
this.snowfall = snowfall;
}
}
public class WeatherAdapter extends RecyclerView.Adapter<WeatherAdapter.ViewHolder>{
private ArrayList<Condition> conditions;
public WeatherAdapter(ArrayList<Condition> conditions) {
this.conditions = conditions;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
private final TextView dateView;
private final TextView tempView;
private final TextView humidView;
private final TextView precView;
private final ImageView imaView;
private final TextView cloudView;
private final TextView windView;
ViewHolder(View view){
super(view);
dateView = view.findViewById(R.id.Date);
tempView = view.findViewById(R.id.Temperature);
humidView = view.findViewById(R.id.Humidity);
precView = view.findViewById(R.id.Precipitation);
imaView = view.findViewById(R.id.WeatherImage);
cloudView= view.findViewById(R.id.Cloud);
windView = view.findViewById(R.id.WindSpeed);
}
}
@NonNull
@Override
public WeatherAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.weather_cardview,parent,false);
return new WeatherAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull WeatherAdapter.ViewHolder holder, int position) {
Condition condition = conditions.get(position);
holder.dateView.setText(String.format("%s",condition.getTime()));
holder.tempView.setText(String.format("Температура воздуха: %s℃",condition.getTemperature()));
holder.humidView.setText(String.format("Относительная влажность: %s ",condition.getHumidity()));
holder.precView.setText(String.format("Вероятность осадков: %s процентов",condition.getPrecipitation()));
holder.cloudView.setText(String.format("Облачность: %s процентов",condition.getCloud()));
holder.windView.setText(String.format("Скорость ветра: %s м/с",condition.getWindSpeed()));
if (condition.getPrecipitation()>=50 && condition.getSnowfall().equals("0,00") && condition.getCloud()>=50){
holder.imaView.setImageResource(R.drawable.rainy);
}
else if (condition.getPrecipitation()>=50 && condition.getSnowfall().equals("0,00") && condition.getCloud()<50){
holder.imaView.setImageResource(R.drawable.rain);
}
else if (condition.getPrecipitation()<50 && condition.getSnowfall().equals("0,00") && condition.getCloud()>=50){
holder.imaView.setImageResource(R.drawable.cloudy);
}
else if (condition.getPrecipitation()<50 && condition.getSnowfall().equals("0,00") && condition.getCloud()<50 && condition.getCloud()>10){
holder.imaView.setImageResource(R.drawable.cloudy_sunny);
}
else if (condition.getPrecipitation()<50 && condition.getSnowfall().equals("0,00") && condition.getCloud()<=10){
holder.imaView.setImageResource(R.drawable.sunny);
}
else if (condition.getPrecipitation()>=50 && !condition.getSnowfall().equals("0,00") && condition.getCloud()>=50){
holder.imaView.setImageResource(R.drawable.snowy);
}
else{
holder.imaView.setImageResource(R.drawable.cloudy_sunny);
}
}
@Override
public int getItemCount() {
return conditions.size();
}
}
New contributor
Никита Стрыгин is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.