1

Je suis en train de mettre en œuvre la charge paresseux sur un listview dans un fragment d'onglet pour charger les éléments d'un abc de tableau. J'essaie de charger 10 éléments, puis lorsque l'utilisateur défile, les 10 éléments suivants seront chargés. Mais mon application ne fonctionne pas car je ne suis pas en mesure de passer au thread d'interface utilisateur afin de le mettre à jour (runonUithread (returnRes), ne fonctionne pas). COuld vous [s'il vous plaît jeter un oeil sur le code ci-dessous et suggérer la bonne façon de mettre en œuvre la charge paresseuse. Je vous remercie.erreur Lazy mise en œuvre de la charge

Fragment Classe:

package com.example.abe; 

import java.util.ArrayList; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Build; 
import android.os.Bundle; 
import android.support.v4.app.ListFragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AbsListView; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class Tab1Fragment extends ListFragment { 
    ListView lv; 
    Activity act = this.getActivity(); 
    Context ct = this.getActivity(); 
    XMLGettersSetters data; 
    boolean loadingMore = false; 
    String abc[] = new String[50]; 
    static int count = 0; 
    int itemsPerPage = 10; 
    ArrayList<String> myListItems; 
    ArrayList<String> myListItems2; 
    ArrayAdapter<String> adapter; 
    static int size = 0; 

    @SuppressWarnings("unused") 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
     myListItems2 = new ArrayList<String>(); 
     adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), 
       android.R.layout.simple_list_item_1, myListItems2); 
     View root = inflater.inflate(R.layout.tab_frag1_layout, container, 
       false); 
     lv = (ListView) root.findViewById(android.R.id.list); 
     lv.setAdapter(adapter); 
     return root; 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     myListItems = new ArrayList<String>(); 
     View footerView = ((LayoutInflater) this.getActivity() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
       R.layout.listfooter, null, false); 
     this.getListView().addFooterView(footerView); 
     this.setListAdapter(adapter); 
     for (int i = 0; i < 50; i++) { 
      abc[i] = "ab" + i; 
     } 
     this.getListView().setOnScrollListener(new OnScrollListener() { 
      @Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 
      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, 
        int visibleItemCount, int totalItemCount) { 
       System.out.println(abc.length); 
       System.out.println(count); 
       int lastInScreen = firstVisibleItem + visibleItemCount; 
       System.out.println(lastInScreen); 
       if ((lastInScreen == totalItemCount) && !(loadingMore)) { 
        try { 
         Thread thread = new Thread(null, loadMoreListItems); 
         thread.start(); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     }); 
     try { 
      Thread thread = new Thread(null, loadMoreListItems); 
      thread.start(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private Runnable loadMoreListItems = new Runnable() { 
     @Override 
     public void run() { 
      loadingMore = true; 
      myListItems = new ArrayList<String>(); 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
      } 
      size = abc.length; 
      for (int i = 0; i < itemsPerPage; i++) { 
       if (count < size) { 
        myListItems.add(abc[count].toString()); 
        count = count + 1; 
       } else { 
        break; 
       } 
      } 
      Log.i("a", "b"); 
      try { 
       act.runOnUiThread(returnRes); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Log.i("a", "b"); 
     } 
    }; 
    private Runnable returnRes = new Runnable() { 
     @Override 
     public void run() { 
      if (myListItems != null && myListItems.size() > 0) { 
       for (int i = 0; i < myListItems.size(); i++) 
        adapter.add(myListItems.get(i)); 
      } 
      lv.setAdapter(adapter); 
      Log.i("a", "b"); 
      adapter.notifyDataSetChanged(); 
      Log.i("a", "b"); 
      loadingMore = false; 
     } 
    }; 
} 

Logcat:

01-18 18:59:21.606: I/System.out(971): 50 
01-18 18:59:21.606: I/System.out(971): 0 
01-18 18:59:21.606: I/System.out(971): 0 
01-18 18:59:21.926: I/System.out(971): 50 
01-18 18:59:21.926: I/System.out(971): 0 
01-18 18:59:21.926: I/System.out(971): 1 
01-18 18:59:22.257: I/System.out(971): 50 
01-18 18:59:22.257: I/System.out(971): 0 
01-18 18:59:22.257: I/System.out(971): 1 
01-18 18:59:22.676: I/a(971): b 
01-18 18:59:22.676: W/System.err(971): java.lang.NullPointerException 
01-18 18:59:22.715: W/System.err(971): at com.example.abe.Tab1Fragment$1.run(Tab1Fragment.java:113) 
01-18 18:59:22.936: W/System.err(971): at java.lang.Thread.run(Thread.java:1019) 
01-18 18:59:22.936: I/a(971): b 

classe FragmentActivity:

package com.example.abe; 

import java.util.HashMap; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTransaction; 
import android.view.View; 
import android.widget.TabHost; 
import android.widget.TabHost.TabContentFactory; 

public class TabsFragmentActivity extends FragmentActivity implements 
     TabHost.OnTabChangeListener { 

    private TabHost mTabHost; 
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsFragmentActivity.TabInfo>(); 
    private TabInfo mLastTab = null; 

    private class TabInfo { 
     private String tag; 
     private Class<?> clss; 
     private Bundle args; 
     private Fragment fragment; 

     TabInfo(String tag, Class<?> clazz, Bundle args) { 
      this.tag = tag; 
      this.clss = clazz; 
      this.args = args; 
     } 

    } 

    class TabFactory implements TabContentFactory { 

     private final Context mContext; 

     public TabFactory(Context context) { 
      mContext = context; 
     } 

     public View createTabContent(String tag) { 
      View v = new View(mContext); 
      v.setMinimumWidth(0); 
      v.setMinimumHeight(0); 
      return v; 
     } 

    } 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabs_layout); 
     initialiseTabHost(savedInstanceState); 
     if (savedInstanceState != null) { 
      mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); // set 
                       // the 
                       // tab 
                       // as 
                       // per 
                       // the 
                       // saved 
                       // state 
     } 
    } 

    protected void onSaveInstanceState(Bundle outState) { 
     outState.putString("tab", mTabHost.getCurrentTabTag()); // save the tab 
                   // selected 
     super.onSaveInstanceState(outState); 
    } 

    private void initialiseTabHost(Bundle args) { 
     mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(); 
     TabInfo tabInfo = null; 
     TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost 
       .newTabSpec("Tab1").setIndicator("ST"), (tabInfo = new TabInfo(
       "Tab1", Tab1Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost 
       .newTabSpec("Tab2").setIndicator("CSI"), 
       (tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args))); 
     this.mapTabInfo.put(tabInfo.tag, tabInfo); 
     this.onTabChanged("Tab1"); 
     mTabHost.setOnTabChangedListener(this); 
    } 

    private static void addTab(TabsFragmentActivity activity, TabHost tabHost, 
      TabHost.TabSpec tabSpec, TabInfo tabInfo) { 
     tabSpec.setContent(activity.new TabFactory(activity)); 
     String tag = tabSpec.getTag(); 
     tabInfo.fragment = activity.getSupportFragmentManager() 
       .findFragmentByTag(tag); 
     if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) { 
      FragmentTransaction ft = activity.getSupportFragmentManager() 
        .beginTransaction(); 
      ft.detach(tabInfo.fragment); 
      ft.commit(); 
      activity.getSupportFragmentManager().executePendingTransactions(); 
     } 
     tabHost.addTab(tabSpec); 
    } 

    public void onTabChanged(String tag) { 
     TabInfo newTab = this.mapTabInfo.get(tag); 
     if (mLastTab != newTab) { 
      FragmentTransaction ft = this.getSupportFragmentManager() 
        .beginTransaction(); 
      if (mLastTab != null) { 
       if (mLastTab.fragment != null) { 
        ft.detach(mLastTab.fragment); 
       } 
      } 
      if (newTab != null) { 
       if (newTab.fragment == null) { 
        newTab.fragment = Fragment.instantiate(this, 
          newTab.clss.getName(), newTab.args); 
        ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag); 
       } else { 
        ft.attach(newTab.fragment); 
       } 
      } 
      mLastTab = newTab; 
      ft.commit(); 
      this.getSupportFragmentManager().executePendingTransactions(); 
     } 
    } 

} 
+0

Ce qui est dans la ligne 221 de Tab1Fragment? jetant aussi autour douzaine de lignes de code (pas bien formaté) n'est pas utile ... – WarrenFaith

+0

Salut Warren, Désolé pour la mise en forme pauvre. Je suis dans un désordre en ce moment. Impossible de créer l'application Le contenu de la ligne no.221 est runonUithread (returnRes); Merci – Vyshakh

+0

En éclipse, appuyez sur ctrl + shift + f pour le mettre en forme, puis réexécutez, publiez l'erreur logcat complète et mettez à jour le code. Ensuite, nous pouvons travailler sur la question. Actuellement, votre code est simplement illisible. – WarrenFaith

Répondre

0
Activity act = this.getActivity(); 

Ce en tant que variable membre va obtenir votre null, ce qui se traduit par la NPE à

act.runOnUiThread(returnRes); 

Au lieu de l'organe variables, essayez de demander à l'exécution de l'activité:

getActivity().runOnUiThread(returnRes); 
+0

Votre suggestion a fonctionné comme un charme. Un million de merci. Encore une demande. Maintenant, ma liste affiche toutes les 50 entrées à la fois. Et quand je descends au dernier élément, ça commence à être diffusé depuis le début. Je veux afficher seulement 10 éléments et après cela quand je défile vers le bas, il devrait montrer les 10 éléments suivants. Pourriez-vous s'il vous plaît jeter un oeil si possible. Merci. – Vyshakh

+0

Je voudrais essayer le lien @QadirHussain tel que posté. Tant que vous ne voulez pas apprendre à implémenter le chargement différé par vous-même, vous devez utiliser une bibliothèque existante pour cela. Sinon, vous devez utiliser le débogueur et parcourir votre code étape par étape. C'est un peu trop complexe en ce moment ... – WarrenFaith

+0

@WarenFaith: Je vais essayer. Mais le problème est que je ne suis pas à l'aise avec les onglets et les fragments. De toute façon, je vais essayer de l'implémenter. Merci encore pour vos suggestions et vos efforts. – Vyshakh

1

utilisation sans fin Scrolling ListView. Here is the link. Je l'ai mis en œuvre. Si vous avez des questions, vous pouvez demander.

ici est mon TabActivity

import android.app.TabActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 
import android.widget.TabHost.TabSpec; 

public class MainActivity extends TabActivity { 

    Context context = MainActivity.this; 

    TabHost tabHost; 
    TabSpec spec; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tabHost = getTabHost(); 
     // Android tab 
     tabHost.addTab(tabHost 
       .newTabSpec("Home") 
       .setIndicator("Home", 
         getResources().getDrawable(R.drawable.home)) 
       .setContent(
         new Intent(this, MyActivityGroup.class) 
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 

     tabHost.addTab(tabHost 
       .newTabSpec("Now Reading") 
       .setIndicator("Now Reading", 
         getResources().getDrawable(R.drawable.now_reading)) 
       .setContent(
         new Intent(this, NowReadingActivityGroup.class) 
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 

     tabHost.addTab(tabHost 
       .newTabSpec("Favorites") 
       .setIndicator("Favorites", 
         getResources().getDrawable(R.drawable.favorites)) 
       .setContent(
         new Intent(this, FavoriteActivityGroup.class) 
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 

     tabHost.addTab(tabHost 
       .newTabSpec("Profile") 
       .setIndicator("Profile", 
         getResources().getDrawable(R.drawable.profile)) 
       .setContent(
         new Intent(this, ProfileActivityGroup.class) 
           .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 

     tabHost.setCurrentTabByTag("Home"); 

     tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

      @Override 
      public void onTabChanged(String tabId) { 

      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 

ici est la classe ActivityGroup

import java.util.Stack; 
import android.app.Activity; 
import android.app.ActivityGroup; 
import android.app.LocalActivityManager; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.Parcelable; 
import android.util.Log; 
import android.view.Window; 

public class MyActivityGroup extends ActivityGroup { 
    private Stack<String> stack; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (stack == null) { 
      stack = new Stack<String>(); 
     } 

     push("1stStackActivity", new Intent(this, Home.class)); 

    } 

    @Override 
    public void finishFromChild(Activity child) { 
     pop(); 
    } 

    @Override 
    public void onBackPressed() { 

     pop(); 

    } 

    public void push(String id, Intent intent) { 
     Window window = getLocalActivityManager().startActivity(id, 
       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
     if (window != null) { 
      stack.push(id); 
      setContentView(window.getDecorView()); 
     } 
    } 

    public void pop() { 
     if (stack.size() == 1) { 

      Home.BookTitle = null; 
      Home.BookCoverPhotos = null; 
      Home.BookAuther = null; 
      Home.BookPublishDate = null; 
      Home.language = "all"; 
      Home.bitmapArray = null; 
      Home.BookIDs = null; 
      Home.BookRating = null; 
      Home.BookDescription = null; 
      Home.BookCode = null; 
      Home.userFName = null; 
      Home.gridViewState = null; 
      Home.initialIndex = 0; 
      Home.CustomerID = -1; 
      Home.checkBookCount = true; 
      Home.str = null; 
      Home.FlagSharedPreferences = false; 

      BookSearchResultActivity.BookTitle = null; 
      BookSearchResultActivity.BookCoverPhotos = null; 
      BookSearchResultActivity.BookAuther = null; 
      BookSearchResultActivity.BookPublishDate = null; 
      BookSearchResultActivity.ImageByte = null; 
      BookSearchResultActivity.bitmapArray = null; 
      BookSearchResultActivity.BookIDs = null; 
      BookSearchResultActivity.BookRating = null; 
      BookSearchResultActivity.BookDescription = null; 

      BookSearchResultActivity.startIndex = 0; 
      BookSearchResultActivity.endIndex = 14; 
      BookSearchResultActivity.listViewState = null; 

      finish(); 
     } 

     LocalActivityManager manager = getLocalActivityManager(); 
     manager.destroyActivity(stack.pop(), true); 
     if (stack.size() > 0) { 
      Intent lastIntent = manager.getActivity(stack.peek()).getIntent() 
        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      Window newWindow = manager.startActivity(stack.peek(), lastIntent); 
      setContentView(newWindow.getDecorView()); 
     } 
    } 
} 

et voici ma maison activty classe qui utilise le EndlessLisnter pour gridview mais vous pouvez l'utiliser pour listview aussi.

Accueil public class activité { /** Appelée lorsque l'activité est créée. */ static final Chaîne URL = "http://www.shiaislamiclibrary.com/requesthandler.ashx";

static final String KEY_ITEM = "Book"; // parent node 
static final String KEY_BOOKAUTHOR = "book_author"; 
static final String KEY_BOOKRATING = "BookRating"; 
static final String KEY_BOOKID = "BookID"; 
static final String KEY_BOOKDESC = "BookDescription"; 
static final String KEY_BOOKDATEPUBLISHED = "DatePublished"; 
static final String KEY_BOOKTITLE = "BookTitle"; 
static final String KEY_BOOKCODE = "BookCode"; 
static final String KEY_BOOKIMAGE = "BookImage"; 

static ArrayList<String> BookTitle = null; 
static ArrayList<Integer> BookRating = null; 
static ArrayList<String> BookDescription = null; 
static ArrayList<String> BookCoverPhotos = null; 
static ArrayList<String> BookAuther = null; 
static ArrayList<String> BookIDs = null; 
static ArrayList<String> BookCode = null; 
static ArrayList<String> BookPageCount = null; 
static ArrayList<String> BookPublishDate = null; 
static ArrayList<Bitmap> bitmapArray = null; 
static String str = null; 


Context ctx = this; 
Activity act = this; 
Context context = Home.this; 
URL bookImageURL = null; 
Bitmap bitMapImage = null; 
LayoutInflater inflater = null; 

GridView gridView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // setContentView(R.layout.home_activity); 
    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(
      R.layout.home_activity, null); 
    this.setContentView(viewToLoad); 

    imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

    gridView = (GridView) findViewById(R.id.gridview); 
    gridView.setOnScrollListener(new EndlessScrollListener()); 

    if (gridViewState != null) { 
     gridView.onRestoreInstanceState(gridViewState); 
    } 

public void checkConnection() { 

    // Check Flight mode ON/OFF 

    if (Settings.System.getInt(context.getContentResolver(), 
      Settings.System.AIRPLANE_MODE_ON, 0) == 0) { 

     if (cm.getActiveNetworkInfo() != null 
       && cm.getActiveNetworkInfo().isAvailable() 
       && cm.getActiveNetworkInfo().isConnected()) { 
      // Avoid to reload the page again and again 
      if (str == null) { 
       str = "TempString"; 
       BookTitle = new ArrayList<String>(); 
       BookRating = new ArrayList<Integer>(); 
       BookDescription = new ArrayList<String>(); 
       BookIDs = new ArrayList<String>(); 
       BookCode = new ArrayList<String>(); 
       BookCoverPhotos = new ArrayList<String>(); 
       BookAuther = new ArrayList<String>(); 
       BookPublishDate = new ArrayList<String>(); 
       BookPageCount = new ArrayList<String>(); 
       bitmapArray = new ArrayList<Bitmap>(); 
       new UIThread().execute(URL, initialIndex + ""); 
      } else { 

       ImageAdapter adapter = new ImageAdapter(context, act); 
       gridView.setAdapter(adapter); 
       if (gridViewState != null) { 
        gridView.onRestoreInstanceState(gridViewState); 
       } 
       btnLanguage.setText(language); 

      } 
     } else { 

      connectionFailureDialog("CONNECTION FAILURE...!", 
        "No internet connection found"); 
     } 
    } else { 
     new DialogClass(getParent()).airplaneDialog("AIRPLANE MODE...!", 
       "Please make sure Airplane mode is Turned Off", "Exit"); 
    } 

} 


@Override 
protected void onStart() { 

    checkConnection(); 

    gridView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int pos, 
       long arg3) { 

      Intent bookOverViewIntent = new Intent(); 
      bookOverViewIntent.setClass(getParent(), BookOverView.class); 
      bookOverViewIntent.putExtra("BITMAP", bitmapArray.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_TITLE", BookTitle.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_AUTHOR", BookAuther.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_PUBLISH_DATE", 
        BookPublishDate.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_ID", BookIDs.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_RATING", BookRating.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_DESC", 
        BookDescription.get(pos)); 

      bookOverViewIntent.putExtra("PAGE_NO", "1"); 
      bookOverViewIntent.putExtra("BOOK_CODE", BookCode.get(pos)); 
      bookOverViewIntent.putExtra("BOOK_PAGE_COUNT", 
        BookPageCount.get(pos)); 

      bookOverViewIntent.putExtra("ACTIVITY", "Home"); 

      MyActivityGroup activityStack = (MyActivityGroup) getParent(); 
      activityStack.push("BookOverView", bookOverViewIntent); 
      gridViewState = gridView.onSaveInstanceState(); 

     } 
    }); 

private class listAdapter extends BaseAdapter { 

    int size; 

    @Override 
    public int getCount() { 

     if (btnFlag.equals("btnLanguage")) { 
      size = getResources().getStringArray(
        R.array.spnr_language_array).length; 
     } else if (btnFlag.equals("btnBrowseBy")) { 
      size = getResources().getStringArray(R.array.spnr_browse_array).length; 
     } 
     return size; 
    } 

    @Override 
    public Object getItem(int position) { 

     return null; 
    } 

    @Override 
    public long getItemId(int position) { 

     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View row = null; 
     LayoutInflater inflater; 
     if (btnFlag.equals("btnLanguage")) { 
      inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.itemslist, parent, false); 
      TextView listText = (TextView) row.findViewById(R.id.textView2); 
      listText.setText(getResources().getStringArray(
        R.array.spnr_language_array)[position]); 

     } else if (btnFlag.equals("btnBrowseBy")) { 
      inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.itemslist, parent, false); 
      TextView listText = (TextView) row.findViewById(R.id.textView2); 
      listText.setText(getResources().getStringArray(
        R.array.spnr_browse_array)[position]); 
     } 

     return row; 
    } 

} 

private class EndlessScrollListener implements OnScrollListener { 

    private int visibleThreshold = 0; 
    private int currentPage = 0; 
    private int previousTotal = 0; 
    private boolean loading = true; 

    public EndlessScrollListener() { 
    } 

    @Override 
    public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 
     if (loading) { 
      if (totalItemCount > previousTotal) { 
       loading = false; 
       previousTotal = totalItemCount; 
       currentPage++; 
      } 
     } 
     if (!loading 
       && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) { 

      if (checkBookCount) { 
       new UIThread().execute(URL, initialIndex + ""); 
       Log.i("Reached If", "End"); 
      } 

      loading = true; 
      gridViewState = gridView.onSaveInstanceState(); 

     } 
    } 


private class UIThread extends AsyncTask<String, Integer, String> { 

    ProgressDialog progressDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = ProgressDialog.show(getParent(), 
       "Acumlating Books from server...", 
       "This may Take a few seconds.\nPlease Wait..."); 

    } 

    @Override 
    protected String doInBackground(String... params) { 

     String URL = params[0]; 
     int lIndex = Integer.valueOf(params[1]); 
     XMLParser parser = new XMLParser(); 
     String XMLString = null; 
     XMLString = parser.getXmlFromUrl_FeaturedBooks(URL, lIndex, 
       language); 
     // Log.i("language = ", language); 
     // Log.i("XMLString = ", XMLString); 

     if (XMLString != null) { 

      initialIndex = lIndex + 14; 

      Document doc = parser.getDomElement(XMLString); 
      NodeList nlBooksLimit = doc 
        .getElementsByTagName(KEY_ITEM_BOOKs_LIMIT); 
      Element eLimit = (Element) nlBooksLimit.item(0); 

      totalBookCount = 0; 
      try { 

       totalBookCount = Integer.valueOf(parser.getValue(eLimit, 
         KEY_ITEM_TOTAL_BOOKS)); 
       checkBookCount = true; 
      } catch (NumberFormatException e) { 

       checkBookCount = false; 
       totalBookCount = totalBookCount + 0; 
      } 

      NodeList nl = doc.getElementsByTagName(KEY_ITEM); 

      // looping through all item nodes <item> 

      Bitmap imageNotFound = BitmapFactory.decodeResource(
        getResources(), R.drawable.defaultcoverphoto); 

      for (int i = 0; i < nl.getLength(); i++) { 

       Element e = (Element) nl.item(i); 

       try { 
        BookRating.add(Integer.valueOf(parser.getValue(e, 
          KEY_BOOKRATING))); 

       } catch (Exception e2) { 
        BookRating.add(0); 
       } 

       BookDescription.add(parser.getValue(e, KEY_BOOKDESC)); 
       BookTitle.add(parser.getValue(e, KEY_BOOKTITLE)); 
       BookCoverPhotos 
         .add("http://shiaislamicbooks.com/books_Snaps/" 
           + parser.getValue(e, KEY_BOOKCODE) 
           + "/1_thumb.jpg"); 
       int tempCount = BookCoverPhotos.size() - 1; 
       BookAuther.add(parser.getValue(e, KEY_BOOKAUTHOR)); 
       BookPublishDate.add(parser.getValue(e, 
         KEY_BOOKDATEPUBLISHED)); 
       BookIDs.add(parser.getValue(e, KEY_BOOKID)); 
       BookCode.add(parser.getValue(e, KEY_BOOKCODE)); 
       BookPageCount.add(parser.getValue(e, KEY_PAGE_COUNT)); 

       try { 
        bookImageURL = new URL(BookCoverPhotos.get(tempCount)); 
       } catch (MalformedURLException e1) { 
        // e1.printStackTrace(); 
       } 

       try { 
        bitMapImage = BitmapFactory.decodeStream(bookImageURL 
          .openConnection().getInputStream()); 
        bitmapArray.add(bitMapImage); 

       } catch (IOException e2) { 
        // e2.printStackTrace(); 
        bitmapArray.add(imageNotFound); 
       } 
       publishProgress(i + 1); 
      } 

     } else { 
      publishProgress(5000); 
     } 

     return null; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
     progressDialog.setMessage(values[0] 
       + " Book(s) found \nPlease wait..."); 
     if (values[0] == 5000) { 
      Toast.makeText(context, 
        "Rrequest Time out!\nNo or Slow Internet Connection!", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 
     progressDialog.dismiss(); 
     ImageAdapter adapter = new ImageAdapter(context, act); 
     gridView.setAdapter(adapter); 
     if (gridViewState != null) { 
      gridView.onRestoreInstanceState(gridViewState); 
     } 

    } 
} 


class ViewHolder { 
    TextView txt_BooksTitle; 
    ImageView img_BookCoverPhoto; 
    } 
} 

ici vous pouvez noter que d'abord je suis remplir mon gridview du AsyncTask et j'ai mis mon gridview comme listner sans fin que

gridView.setOnScrollListener(new EndlessScrollListener()); 

prochaine fois que je suis d'appeler la AsyncTask de la classe Endless listner.

new UIThread().execute(URL, initialIndex + ""); 

vous devez effectuer toutes les tâches à partir de là. où j'ai appelé cette ligne ci-dessus dans l'écouteur sans fin.

+0

@Quadir Salut Qadir, Est-il possible d'implémenter une liste infinie dans les 'onglets avec fragments'. J'ai essayé mais je n'ai pas réussi. Pourriez-vous s'il vous plaît donner un exemple de code pour le même. – Vyshakh

+0

désolé mais je n'ai pas utilisé les onglets de fragments. J'ai utilisé la classe ActivtyGroup et TabActivty. Si vous avez encore besoin du code je peux le coller sur ma réponse –

+0

@Quadir: oui Quadir. Cela sera utile. Merci – Vyshakh

0

Essayez de cette façon:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    if (container == null) { 
     myListItems2 = new ArrayList<String>(); 
     adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, myListItems2); 
     View root = inflater.inflate(R.layout.tab_frag1_layout, 
       container, false); 
     lv = (ListView) root.findViewById(android.R.id.list); 
     lv.setAdapter(adapter);   
    return root; 
} 
+0

et maintenant ce code ne compile même pas ... – WarrenFaith