【发布时间】:2014-11-26 07:31:26
【问题描述】:
我是 Android 应用程序开发的新手。我想知道如何实现下拉刷新并在Listview 中从服务器上拉获取更多项目。 我该怎么做? 我的代码从服务器获取项目如下。
private class DownloadJSONItems extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressbar
if (!prefs) {
progressBar.setVisibility(View.VISIBLE);
}
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions.getJSONfromURL("Server URL",latitude, longitude);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("places");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
double convertString = Double.parseDouble(jsonobject.getString("distance"));
double convertKMToMile = convertString * 0.6124;
String convertedValue = String.format("%.2f",convertKMToMile);
// Retrive JSON Objects
map.put("places_name", jsonobject.getString("places_name"));
map.put("distance", convertedValue);
map.put("places_icon", jsonobject.getString("places_icon"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
//Toast.makeText(getActivity(), getString(jsonarray.length()), 6000).show();
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressbar
progressBar.setVisibility(View.GONE);
if (prefs) {
prefs = false;
}
}
}
谢谢。
【问题讨论】:
-
使用这个库来完成你的工作。 github.com/chrisbanes/ActionBar-PullToRefresh
标签: android listview android-listview pull-to-refresh