Android ListView下拉和上拉
我是Android应用程序开发的新手。我想知道如何在Listview中实现下拉刷新和从服务器获取更多项目。 我怎么做? 下面是我从服务器获取项目的代码Android ListView下拉和上拉,android,listview,android-listview,pull-to-refresh,Android,Listview,Android Listview,Pull To Refresh,我是Android应用程序开发的新手。我想知道如何在Listview中实现下拉刷新和从服务器获取更多项目。 我怎么做? 下面是我从服务器获取项目的代码 private class DownloadJSONItems extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); // Creat
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;
}
}
}
私有类下载JsonItems扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//创建进度条
如果(!prefs){
progressBar.setVisibility(View.VISIBLE);
}
}
@凌驾
受保护的Void doInBackground(Void…参数){
//创建一个数组
arraylist=新的arraylist();
//从给定的URL地址检索JSON对象
jsonobject=JSONfunctions.getJSONfromURL(“服务器URL”,纬度,经度);
试一试{
//在JSON中找到数组名称
jsonarray=jsonobject.getJSONArray(“位置”);
for(int i=0;i
谢谢。有一个很好的图书馆 要刷新:
@Override
public void onRefresh() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
start = ++refreshCnt;
items.clear();
geneItems();
// mAdapter.notifyDataSetChanged();
mAdapter = new ArrayAdapter<String>(XListViewActivity.this, R.layout.list_item, items);
mListView.setAdapter(mAdapter);
onLoad();
}
}, 2000);
}
使用此库完成您的工作。你可以用,我试过很多图书馆,这一个很有魅力,谢谢!
@Override
public void onLoadMore() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
geneItems();
mAdapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}