2013-07-26 3 views
1

J'ai une base de données avec environ 12000 enregistrements que je veux charger dans un ListView. J'utilise BaseAdapter. Lorsque je charge des éléments dans ListView, cela prend tellement de temps.
Y at-il un moyen de réduire ce temps, par exemple j'ai vu une application qui ne charge qu'un nombre limité d'éléments jusqu'à ce que la barre de défilement atteigne la fin de ListView, puis il charge à nouveau plus d'éléments.chargement d'une grande base de données dans la vue liste

+0

fixer une limite de votre requête comme vous avez dit http://stackoverflow.com/questions/1407442/android-sqlite-and-huge-data-sets – tyczj

Répondre

0

Vous devez définir onScrollListener dans votre liste et garder une trace de la visibilité des articles et du décalage. Je vous montre un exemple ici:

// Adapter for the custom list 
adapter = new Adapter(this, activityList); 
setListAdapter(adapter); 
registerForContextMenu(getListView()); 
getListView().setOnScrollListener(new OnScrollListener(){ 
    public void onScroll(AbsListView lw, final int firstVisibleItem, 
    final int visibleItemCount, final int totalItemCount) { 

     switch(lw.getId()) { 
      case android.R.id.list:  

       // Make your calculation stuff here. You have all your 
       // needed info from the parameters of this function. 

       // Sample calculation to determine if the last 
       // item is fully visible. 
       final int lastItem = firstVisibleItem + visibleItemCount; 
       if(lastItem == totalItemCount) { 
        // Last item is fully visible. 
        Log.i("a", "last item fully visible..."); 

        try { 
         if(offset > 0){ 
          int newLimit; 
          int oldOffset = offset; 
          if(offset >= limit){ 
          newLimit = limit; 
          offset = offset - limit; 
         } 
         else{ 
          newLimit = length; 
          offset = 0; 
         } 
         for (int i=0; i < newLimit; i++) 
         { 
          JSONObject item = jFeed.getJSONObject(i + length - oldOffset); 
          // Pulling items from the array 

          // Get list info 
          String sInfo = item.getString(TAG_INFO); 
          Log.i(MainActivity.class.getName(), "Info: " + sInfo); 

          // Populate the dynamic custom list 
          HashMap<String, String> map = new HashMap<String, String>(); 
          map.put(KEY_INFO, sInfo); 

          activityList.add(map); 
         } 
         adapter.notifyDataSetChanged(); 

        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
     } 
    } 

    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // TODO Auto-generated method stub 
     if(scrollState == 0) 
      Log.i("a", "scrolling stopped..."); 
    } 
}); 
Questions connexes