2014-11-26 3 views
-1

Я новичок в разработке приложений для Android. Я хочу знать, как реализовать Выдвиньте обновление и потяните вверх, чтобы получить больше предметов с сервера в Listview. Как я могу это сделать? Мой код получает элементы с сервера ниже.Android ListView Pull Down и Pull Up

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; 
     } 
    } 
} 

спасибо.

+0

Используйте эту библиотеку, чтобы получить работу. https://github.com/chrisbanes/ActionBar-PullToRefresh –

+0

Вы можете использовать. https://github.com/erikwt/PullToRefresh-ListView, https://github.com/johannilsson/android-pulltorefresh и http://stackoverflow.com/questions/4583484/how-to-implement-android-pull- to-refresh – Jaydeep

ответ

3

Существует хорошая библиотека для этого https://github.com/Maxwin-z/XListView-Android

Для Refresh:

@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); 
} 

enter image description here

+0

Я пробовал много библиотек, и этот работает как шарм, спасибо! –

+0

@ Умберто Кастаньда вы приветствуете – Gattsu

Смежные вопросы