我有一个带有分页方案的API,如:
-meta: {
-pagination: {
total: 100
count: 20
per_page: 20
current_page: 1
total_pages: 5
-links: {
next: "http://dev.app/api/posts?page=2"
}
}
}
我试图找到一种方法在我的Android应用程序上使用凌空和无限滚动来实现它,应用程序必须从API读取json分页并向下滚动到20项,然后自动加载到下一页问题是当我到达" page = 2"然后我再次按下LoadMore按钮它仍然是第2页而不是第3页,我的意思是,当前页面总是1,我不知道为什么会发生这种情况,我试图做的是:
public class MainActivity extends ListActivity {
ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();
//Progress Dialog
private ProgressDialog pDialog;
//make json parser Object
JSONParser jsonParser = new JSONParser();
ArrayList<HashMap<String, String>> restaurant_list;
//Restaurant Json array
JSONArray restaurants = null;
JSONObject pagination = null;
JSONObject link = null;
private String url_posts = "http://dev.app/api/posts";
//Flag for current page
private String nPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cd = new ConnectionDetector(getApplicationContext());
//Check for Internet Connection
if (!cd.isConnectingToInternet()) {
//Internet connection not present
alert.showAlertDialog(MainActivity.this, "Internet Connection Error", "Please Check Your Internet Connection");
//stop executing code by return
return;
}
restaurant_list = new ArrayList<HashMap<String, String>>();
new LoadRestaurants().execute();
Button btnLoadMore = new Button(MainActivity.this);
btnLoadMore.setText("Show More");
getListView().addFooterView(btnLoadMore);
btnLoadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
url_posts = nPage;
new LoadRestaurants().execute();
}
});
}
class LoadRestaurants extends AsyncTask<String, String, String> {
//Show Progress Dialog
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading All Restaurants...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
protected String doInBackground(String... arg) {
//building parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jsonParser.makeHttpRequest(url_posts, "GET", params);
try {
restaurants = json.getJSONArray("data");
JSONObject meta = json.getJSONObject("meta");
pagination = meta.getJSONObject("pagination");
link = pagination.getJSONObject("links");
nPage = link.getString("next");
if (restaurants != null) {
//loop through all restaurants
for (int i = 0; i < restaurants.length(); i++) {
JSONObject c = restaurants.getJSONObject(i);
//Creating New Hashmap
HashMap<String, String> map = new HashMap<String, String>();
//adding each child node to Hashmap key
map.put("id", c.getString("id"));
map.put("look1", c.getString("look1"));
map.put("look2", c.getString("look2"));
map.put("comment", c.getString("comment"));
//adding HashList to ArrayList
restaurant_list.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
//dismiss the dialog
pDialog.dismiss();
//Updating UI from the Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, restaurant_list,
R.layout.feed_item, new String[]{
"id", "look1", "look2", "comment"}, new int[]{
R.id.profilePic, R.id.name, R.id.txtUrl});
setListAdapter(adapter);
ListView lv = getListView();
int currentPosition = lv.getFirstVisiblePosition();
lv.setSelectionFromTop(currentPosition + 1, 1);
}
});
}
}
}