因此,我正在使用Volley从Thinkspeak.com API获取数据,该API具有将结果显示为JSON格式的有限结果。
这是Thinkspeak.com API的结果:
{
"channel": {
"id": "channel_id",
"name": "SISTEM FDRS",
"latitude": "lat",
"longitude": "long",
"field1": "Field Label 1",
"field2": "Field Label 2",
"field3": "Field Label 3",
"field4": "Field Label 4",
"field5": "Field Label 5",
"field6": "Field Label 6",
"created_at": "2019-01-20T02:01:36Z",
"updated_at": "2019-06-27T08:06:29Z",
"last_entry_id": 115
},
"feeds": [{
"created_at": "2019-07-05T10:36:02Z",
"entry_id": 106,
"field1": "31.20",
"field2": "64.30",
"field3": "0.00",
"field4": "2.95",
"field5": "86",
"field6": "2"
},
{
"created_at": "2019-07-05T10:36:50Z",
"entry_id": 107,
"field1": "31.20",
"field2": "64.67",
"field3": "0.00",
"field4": "2.41",
"field5": "86",
"field6": "2"
},
/* ... and so on .. */
]
}
由于可以通过使用addOnScrollListener方法达到RecyclerView的结尾,因此可以增加限制。
但是RecyclerView中显示的数据是重复的,我不知道为什么。
这是活动代码:
public class FFMCActivity extends AppCompatActivity {
private List < Feed > feedList;
private RecyclerView recyclerView;
private String url = "https://api.thingspeak.com/channels/id/feeds.json?api_key=api_key&results=";
private int load_results = 1;
AdapterFFMC adapterFFMC;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ffmc);
recyclerView = findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
feedList = new ArrayList < > ();
adapterFFMC = new AdapterFFMC(feedList);
recyclerView.setAdapter(adapterFFMC);
getData(load_results);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (recyclerView.getAdapter().getItemCount() != 0) {
int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1) {
getData(load_results++);
Toast.makeText(FFMCActivity.this, "Data loaded: " + load_results, Toast.LENGTH_SHORT).show();
}
}
}
});
}
private void getData(int results) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Request.Method.GET, url + results,
null,
new Response.Listener < JSONObject > () {
@Override
public void onResponse(JSONObject response) {
JSONArray Jarray = null;
try {
Jarray = response.getJSONArray("feeds");
for (int i = 0; i < Jarray.length(); i++) {
JSONObject feed = Jarray.getJSONObject(i);
feedList.add(new Feed(
feed.getString("created_at"),
feed.getString("entry_id"),
feed.getString("field1"),
feed.getString("field2"),
feed.getString("field3"),
feed.getString("field4"),
feed.getString("field5"),
feed.getString("field6")
));
}
Collections.sort(feedList, new Comparator < Feed > () {
@Override
public int compare(Feed feed1, Feed feed2) {
if (Integer.parseInt(feed1.getEntry_id()) > Integer.parseInt(feed2.getEntry_id())) {
return -1;
} else {
return 1;
}
}
});
adapterFFMC.notifyDataSetChanged();
} catch (JSONException e) {
Log.e("VolleyError", "JSON Parsing Error: " + e.getMessage());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VolleyError", "JSON Response Error: " + error.getMessage());
}
}
);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
}
这是适配器代码:
public class AdapterFFMC extends RecyclerView.Adapter < AdapterFFMC.ViewHolder > {
private List < Feed > dataFFMC;
public AdapterFFMC(List < Feed > dataFFMC) {
this.dataFFMC = dataFFMC;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ffmc, parent, false);
ViewHolder holder = new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Feed feed = dataFFMC.get(position);
holder.created_at.setText(feed.getCreated_at());
holder.created_day.setText(feed.getCreated_at());
holder.entry_id.setText(feed.getEntry_id());
holder.field_1.setText(feed.getField_1());
holder.field_2.setText(feed.getField_2());
holder.field_3.setText(feed.getField_3());
holder.field_4.setText(feed.getField_4());
holder.field_5.setText(feed.getField_5());
holder.field_6.setText(feed.getField_6());
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
return dataFFMC.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView created_at, created_day, entry_id, field_1, field_2, field_3, field_4, field_5, field_6;
public ViewHolder(View itemView) {
super(itemView);
created_at = itemView.findViewById(R.id.created_at);
created_day = itemView.findViewById(R.id.created_day);
entry_id = itemView.findViewById(R.id.entry_id);
field_1 = itemView.findViewById(R.id.field_1);
field_2 = itemView.findViewById(R.id.field_2);
field_3 = itemView.findViewById(R.id.field_3);
field_4 = itemView.findViewById(R.id.field_4);
field_5 = itemView.findViewById(R.id.field_5);
field_6 = itemView.findViewById(R.id.field_6);
}
}
}
如何防止上面的代码重复结果?
任何帮助将不胜感激 谢谢。
答案 0 :(得分:0)
将代码的顺序更改为此。
feedList = new ArrayList < > ();
getData(load_results);
adapterFFMC = new AdapterFFMC(feedList);
recyclerView.setAdapter(adapterFFMC);
设置阵列适配器后,您正在初始化阵列。