我一直在尝试显示从RecyclerView中的API后端获取的JSON数据,虽然似乎无法显示它但是没有出现错误,虽然后端通知操作已成功但不会出现
MapAndList.java
private void loadJSON() {
Log.d("sample","Help");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterfaceList request = retrofit.create(RequestInterfaceList.class);
Call<JSONResponse> call = request.getJSON();
call.enqueue(new Callback<JSONResponse>() {
@Override
public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
Log.d("sample","success to");
JSONResponse jsonResponse = response.body();
mArrayList = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
mAdapter = new DataAdapter(mArrayList);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onFailure(Call<JSONResponse> call, Throwable t) {
Log.d("sample","failure");
Log.d("Error", t.getMessage());
}
});
}
LocationData.java
package com.floodwatchv1.knightdev.floodwatchv1.Retrofit;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
/**
* Created by knightdev on 5/24/17.
*/
public class LocationData {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("zip")
@Expose
private String zip;
@SerializedName("latitude")
@Expose
private Double latitude;
@SerializedName("longitude")
@Expose
private Double longitude;
@SerializedName("device")
@Expose
private Device device;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
}
Level.java
package com.floodwatchv1.knightdev.floodwatchv1.Retrofit;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Level {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("device_id")
@Expose
private Integer deviceId;
@SerializedName("height")
@Expose
private Integer height;
@SerializedName("status")
@Expose
private String status;
@SerializedName("notify")
@Expose
private Boolean notify;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getDeviceId() {
return deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
public Integer getHeight() {
return height;
}
public void setHeight(Integer height) {
this.height = height;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Boolean getNotify() {
return notify;
}
public void setNotify(Boolean notify) {
this.notify = notify;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
Device.java
package com.floodwatchv1.knightdev.floodwatchv1.Retrofit;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Device {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("level")
@Expose
private Level level;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Level getLevel() {
return level;
}
public void setLevel(Level level) {
this.level = level;
}
}
JSONResponse.java
package com.floodwatchv1.knightdev.floodwatchv1.Retrofit;
/**
* Created by knightdev on 5/24/17.
*/
public class JSONResponse {
private LocationData[] android;
public LocationData[] getAndroid() {
return android;
}
}
RequestInterfaceList.java
package com.floodwatchv1.knightdev.floodwatchv1.Retrofit;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
/**
* Created by knightdev on 5/24/17.
*/
public interface RequestInterfaceList {
@GET("api/water_level")
Call<JSONResponse> getJSON();
}
DataAdapter.java
package com.floodwatchv1.knightdev.floodwatchv1.Retrofit;
/**
* Created by knightdev on 5/24/17.
*/
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.floodwatchv1.knightdev.floodwatchv1.R;
import java.util.ArrayList;
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> implements Filterable{
private ArrayList<LocationData> mArrayList;
private ArrayList<LocationData> mFilteredList;
public DataAdapter(ArrayList<LocationData> arrayList) {
mArrayList = arrayList;
mFilteredList = arrayList;
}
@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(DataAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_name.setText(mFilteredList.get(i).getName());
viewHolder.tv_version.setText(mFilteredList.get(i).getLatitude().toString());
viewHolder.tv_api_level.setText(mFilteredList.get(i).getLongitude().toString());
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name,tv_version,tv_api_level;
public ViewHolder(View view) {
super(view);
tv_name = (TextView)view.findViewById(R.id.tv_name);
tv_version = (TextView)view.findViewById(R.id.tv_version);
tv_api_level = (TextView)view.findViewById(R.id.tv_api_level);
}
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = mArrayList;
} else {
ArrayList<LocationData> filteredList = new ArrayList<>();
for (LocationData androidVersion : mArrayList) {
if (androidVersion.getName().toLowerCase().contains(charString) || androidVersion.getLongitude().toString().contains(charString) || androidVersion.getLatitude().toString().toLowerCase().contains(charString)) {
filteredList.add(androidVersion);
}
}
mFilteredList = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredList = (ArrayList<LocationData>) filterResults.values;
notifyDataSetChanged();
}
};
}
}
返回JSON的结构如下:
[
{
"id": 1,
"name": "Parang, Marikina",
"zip": "1900",
"latitude": 14.66,
"longitude": 121.113,
"device": {
"id": 1,
"name": "ARD_0001",
"level": {
"id": 1,
"device_id": 1,
"height": 5,
"status": "MONITOR",
"notify": true,
"created_at": "2017-05-24T08:01:08.000Z",
"updated_at": "2017-05-24T08:01:08.000Z"
}
}
},
{
"id": 2,
"name": "Fortune, Marikina",
"zip": "1901",
"latitude": 14.6589,
"longitude": 121.128,
"device": {
"id": 2,
"name": "ARD_0002",
"level": {
"id": 2,
"device_id": 2,
"height": 21,
"status": "EVACUATE",
"notify": true,
"created_at": "2017-05-26T03:16:49.000Z",
"updated_at": "2017-06-07T03:30:07.000Z"
}
}
},
{
"id": 3,
"name": "San Jose del Monte, Bulacan",
"zip": "",
"latitude": 14.8206,
"longitude": 121.102,
"device": {
"id": 3,
"name": "ARD_0003",
"level": {
"id": 3,
"device_id": 3,
"height": 16.3,
"status": "EVACUATE",
"notify": true,
"created_at": "2017-06-02T04:46:35.000Z",
"updated_at": "2017-06-02T05:10:38.000Z"
}
}
},
{
"id": 4,
"name": "Mambugan, Antipolo",
"zip": "",
"latitude": 14.6176,
"longitude": 121.138,
"device": {
"id": 4,
"name": "ARD_0004",
"level": {
"id": 4,
"device_id": 4,
"height": 16.3,
"status": "EVACUATE",
"notify": true,
"created_at": "2017-06-05T02:16:53.000Z",
"updated_at": "2017-06-05T02:16:53.000Z"
}
}
},
{
"id": 5,
"name": "Malolos,Bulacan",
"zip": "",
"latitude": 14.8527,
"longitude": 120.816,
"device": {
"id": 5,
"name": "ARD_0005",
"level": {
"id": 5,
"device_id": 5,
"height": 15,
"status": "EVACUATE",
"notify": true,
"created_at": "2017-06-06T06:35:27.000Z",
"updated_at": "2017-06-07T03:29:47.000Z"
}
}
}
]
答案 0 :(得分:0)
最好在loadJSON()本身中过滤arraylist,然后将数据设置为适配器,而不是在适配器中过滤。