看完一篇教程后,我创建了一个永无止境的列表视图适配器视图。我成功实现了它。但是所提出的问题假设最初我的数据库中只有14个列表。那么我将如何处理呢?在排序中,我不想重复我的列表。在我目前的情况下,我知道我在这里丢失了一些东西。但是我在这里想念的地方找不到它吗?
我的代码是:
public void getUserList(final SwipeMenuListView listView){
final ProgressDialog loading = ProgressDialog.show(this, "", "Downloading...", false, false);
StringRequest stringRequest = new StringRequest(Request.Method.GET, ServerLinks.USERLIST+"/" + userID, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
int responseCode = object.getInt("code");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
fview = li.inflate(R.layout.footer_view,null);
mhandler = new myHandler();
ArrayList<Model> POIListUser = new ArrayList<>();
if (responseCode == 200) {
JSONArray obj = object.getJSONArray("data");
indexMapping = new HashMap<>();
idMapping = new HashMap<>();
int ListLength = obj.length();
for (int k = 0; k < ListLength; k++) {
String jsonList = obj.getString(k);
JSONObject objValues = new JSONObject(jsonList);
String POIId = objValues.getString("id");
String POIName = objValues.getString("name");
String POIAddress = objValues.getString("address");
String POILat = objValues.getString("latitude");
String POILong = objValues.getString("longitude");
String POIImage = objValues.getString("image");
String POICategory = objValues.getString("category");
String POICreatedDate = objValues.getString("created_at");
indexMapping.put(k,POIId);
idMapping.put(POIId,POIName);
POIListUser.add(new Model(R.drawable.sample,POIName,POIAddress,POILat,POILong,POICategory,POICreatedDate));
}
POIListAdapter = new ListAdaper(POIListActivity.this,POIListUser);
loading.dismiss();
listView.setAdapter(POIListAdapter);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(view.getLastVisiblePosition() == totalItemCount - 1 && listView.getCount() >= 10 && !isLoading){
Thread thread = new ThreadGetMoreData();
thread.start();
}
}
});
} else if(responseCode == 600){
preferences.edit().clear().apply();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.dismiss();
Log.d(TAG, "onErrorResponse: " + error.getMessage());
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
params.put("token", OAuth);
params.put("x-api-key", ServerLinks.API_KEY);
return params;
}
};;
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
AND要在线程中获取更多数据,
public class myHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 0:
listView.addFooterView(fview);
break;
case 1:
((ListAdaper) POIListAdapter).addListIntemAdapter((ArrayList<Model>)msg.obj);
listView.removeFooterView(fview);
isLoading = false;
break;
default:
break;
}
super.handleMessage(msg);
}
}
private ArrayList<Model> getMoreData() {
ArrayList<Model> lst = new ArrayList<>();
lst.add(new Model(R.drawable.sample,"Some Company","Some Address","89.86876","78.876478","Bar","24-874-7873"));
return lst;
}
public class ThreadGetMoreData extends Thread {
@Override
public void run() {
mhandler.sendEmptyMessage(0);
ArrayList<Model> lstResult = getMoreData();
try {
Thread.sleep(7000);
} catch (Exception e){
Log.e(TAG,e.getMessage());
}
Message msg = mhandler.obtainMessage(1,lstResult);
mhandler.sendMessage(msg);
super.run();
}
}
任何帮助将不胜感激。