我正在尝试从天气api传递数据并使其成为recyclerView和cardView,但我不知道为什么我的活动没有显示任何内容,我不知道为什么不工作。任何人都可以帮助我,谢谢先进
********这是我的代码***********
DailyWeatherInfo.java
private Daily mDaily;
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private CustomAdapter adapter;
private List<Daily> data_list;
private double latitude;
private double longitude;
public DailyWeatherInfo() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_weather_info);
recyclerView = findViewById(R.id.recyclerView);
data_list = new ArrayList<>();
load_weather_info(37.8267,-122.4233);
layoutManager = new LinearLayoutManager(this);
adapter = new CustomAdapter(this,data_list);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
//getForcus(37.8267,-122.4233);
}
private void load_weather_info(final double latitude, final double longitude) {
//this.latitude = latitude;
//this.longitude = longitude;
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
String apiKey = "--------------------------------";
String forecastUrl = "https://api.darksky.net/forecast/" + apiKey +
"/" + latitude + "," + longitude;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
JSONObject daily = jsonObject.getJSONObject("daily");
JSONObject mDaily = daily.getJSONObject("data");
Daily daily1 = new Daily(mDaily.getString("summary"), mDaily.getString("icon"),
mDaily.getDouble("precipProbability"), mDaily.getLong("time"));
data_list.add(daily1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
};
task.execute((int) latitude, (int) longitude);
}
Daily.java
public class Daily {
private String nIcon;
private String nSummary;
private String nTimeZone;
private Long nTimeStamp;
private Double nPrecipPro;
public Daily(String nSummary, String nIcon, double nPrecipPro, long nTimeStamp ){
this.nSummary = nSummary;
this.nIcon = nIcon;
this.nPrecipPro = nPrecipPro;
this.nTimeStamp = nTimeStamp;
}
public String getnTimeZone() {
return nTimeZone;
}
public void setnTimeZone(String nTimeZone) {
this.nTimeZone = nTimeZone;
}
public String getnIcon() {
return nIcon;
}
public int getIconId(){
//clear-day, clear-night, rain, snow, sleet, wind, fog, cloudy, partly-cloudy-day, or partly-cloudy-night
int iconId = R.drawable.clear_day;
if (nIcon.equals("clear-day")){
iconId = R.drawable.clear_day;
}else if (nIcon.equals("clear-night")) {
iconId = R.drawable.clear_night;
}
else if (nIcon.equals("rain")) {
iconId = R.drawable.rain;
}
else if (nIcon.equals("snow")) {
iconId = R.drawable.snow;
}
else if (nIcon.equals("sleet")) {
iconId = R.drawable.sleet;
}
else if (nIcon.equals("wind")) {
iconId = R.drawable.wind;
}
else if (nIcon.equals("fog")) {
iconId = R.drawable.fog;
}
else if (nIcon.equals("cloudy")) {
iconId = R.drawable.cloudy;
}
else if (nIcon.equals("partly-cloudy-day")) {
iconId = R.drawable.partly_cloudy;
}
else if (nIcon.equals("partly-cloudy-night")) {
iconId = R.drawable.cloudy_night;
}
return iconId;
}
public void setnIcon(String nIcon) {
this.nIcon = nIcon;
}
public String getnSummary() {
return nSummary;
}
public void setnSummary(String nSummary) {
this.nSummary = nSummary;
}
public Long getnTimeStamp() {
return nTimeStamp;
}
public String getFormattedTime(){
SimpleDateFormat formatter = new SimpleDateFormat("h:mm a");
formatter.setTimeZone(TimeZone.getTimeZone(getnTimeZone()));
Date dateTime = new Date(getnTimeStamp()*1000);
String timeString = formatter.format(dateTime);
return timeString;
}
public void setnTimeStamp(Long nTimeStamp) {
this.nTimeStamp = nTimeStamp;
}
public Double getnPrecipPro() {
return nPrecipPro;
}
public void setnPrecipPro(Double nPrecipPro) {
this.nPrecipPro = nPrecipPro;
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context context;
private List<Daily> myDaily;
public CustomAdapter(Context context, List<Daily> myDaily){
this.context = context;
this.myDaily = myDaily;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.cSummary.setText(myDaily.get(position).getnSummary());
holder.cPrecipProability.setText(myDaily.get(position).getnPrecipPro() + "");
holder.cDate.setText(myDaily.get(position).getFormattedTime());
holder.cImage.setImageResource(myDaily.get(position).getIconId());
}
@Override
public int getItemCount() {
return myDaily.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView cDate;
public ImageView cImage;
public TextView cSummary;
public TextView cPrecipProability;
public ViewHolder(View itemView) {
super(itemView);
cDate = itemView.findViewById(R.id.txtDate);
cSummary = itemView.findViewById(R.id.txtSummary);
cPrecipProability = itemView.findViewById(R.id.txtRainChance);
cImage = itemView.findViewById(R.id.weather_icon);
}
}
}
card_view xml
我的xml文件有什么问题吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/weather_icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="15dp" />
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/weather_icon"
android:layout_marginLeft="18dp"
android:layout_marginStart="18dp"
android:layout_toEndOf="@+id/weather_icon"
android:layout_toRightOf="@+id/weather_icon"
android:text="2018/04/06, Friday"
android:textSize="15dp" />
<TextView
android:id="@+id/txtSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtDate"
android:layout_alignStart="@+id/txtDate"
android:layout_centerVertical="true"
android:text="Mostly cloudy throughout the day."
android:textSize="20dp" />
<TextView
android:id="@+id/txtRainChance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/weather_icon"
android:layout_alignLeft="@+id/txtSummary"
android:layout_alignStart="@+id/txtSummary"
android:text="Rain of Chance : 65%" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
答案 0 :(得分:2)
您忘了 setLayoutManager();
到recyclerView
检查
recyclerView.setLayoutManager(layoutManager);
修改强>
网络操作需要一些时间来执行操作,以便在从API
获取数据后设置适配器试试这个
data_list = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new CustomAdapter(this,data_list);
recyclerView.setAdapter(adapter);
load_weather_info(37.8267,-122.4233);
更改 load_weather_info
方法,如下面的代码
private void load_weather_info(final double latitude, final double longitude) {
//this.latitude = latitude;
//this.longitude = longitude;
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
String apiKey = "--------------------------------";
String forecastUrl = "https://api.darksky.net/forecast/" + apiKey +
"/" + latitude + "," + longitude;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
JSONObject daily = jsonObject.getJSONObject("daily");
JSONObject mDaily = daily.getJSONObject("data");
Daily daily1 = new Daily(mDaily.getString("summary"), mDaily.getString("icon"),
mDaily.getDouble("precipProbability"), mDaily.getLong("time"));
data_list.add(daily1);
}
adapter.notifyDataSetChanged();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
};
task.execute((int) latitude, (int) longitude);
}
答案 1 :(得分:0)
您尚未通知adapter
有关数据更改的信息。
for (int i = 0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
JSONObject daily = jsonObject.getJSONObject("daily");
JSONObject mDaily = daily.getJSONObject("data");
Daily daily1 = new Daily(mDaily.getString("summary"), mDaily.getString("icon"),
mDaily.getDouble("precipProbability"), mDaily.getLong("time"));
data_list.add(daily1);
}
adapter.notifyDataSetChanged(); //notify here
答案 2 :(得分:0)
大部分错误已经指出。只是纠正它们。
UPPER(FORMAT(yourDate, 'MMM-yyyy'))
按如下方式通知适配器。
data_list = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new CustomAdapter(this,data_list);
recyclerView.setAdapter(adapter);
load_weather_info(37.8267,-122.4233);
PS:在这里,您应该使用private void load_weather_info(final double latitude, final double longitude) {
AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... integers) {
String apiKey = "--------------------------------";
String forecastUrl = "https://api.darksky.net/forecast/" + apiKey +
"/" + latitude + "," + longitude;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastUrl)
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
JSONObject daily = jsonObject.getJSONObject("daily");
JSONObject mDaily = daily.getJSONObject("data");
Daily daily1 = new Daily(mDaily.getString("summary"), mDaily.getString("icon"),
mDaily.getDouble("precipProbability"), mDaily.getLong("time"));
data_list.add(daily1);
}
recyclerView.post(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
};
task.execute((int) latitude, (int) longitude);
}
来获取返回的数据集并处理onPostExecute()
。