1

I am trying to implement lazy load on a listview in a tab fragment to load the elements of an array abc. I am trying to load 10 elements, then when the user scrolls down, next 10 elements will be loaded. But My app is not working as I am not able to jump to the UI thread in order to update it(runonUithread(returnRes); not working). COuld you [please take a look at the below code and suggest the right way to implement lazy load. Thank you.

Fragment Class:

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

FragmentActivity class:

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

}
Vyshakh
  • 632
  • 1
  • 11
  • 29
  • What is in line 221 of Tab1Fragment? Also throwing around dozen lines of (not well formatted) code isn't helpful... – WarrenFaith Jan 18 '13 at 13:08
  • Hi Warren, Sorry for the poor formatting. I am in a mess right now. Not able to create the app. The Content in line no. 221 is runonUithread(returnRes); Thanks – Vyshakh Jan 18 '13 at 13:10
  • In eclipse press ctrl + shift + f to get it formatted, than run again, post the complete logcat error and update the code. Then we can work on the issue. Currently your code is simply unreadable. – WarrenFaith Jan 18 '13 at 13:15
  • @WarrenFaith Hi Warren, I have pasted the formatted code. Please take a look. Thanks. – Vyshakh Jan 18 '13 at 13:29

3 Answers3

1

use Endless Scrolling ListView. Here is the link. I have implemented it. if you have any query u can ask.

here is my 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;
    }

}

here is the ActivityGroup class

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

and here is my Home Activty Class which uses the EndlessLisnter for gridview but you can use this for listview also.

public class Home extends Activity { /** Called when the activity is first created. */ static final String 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;
    }
}

here you can note that first i m filling my gridview from the Asynctask and then I have set my gridview as endless listner as

gridView.setOnScrollListener(new EndlessScrollListener());

next time i m calling the asynctask from the Endless listner class.

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

you have to do your all task from there. where i have call this above line in the endless listner.

Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124
  • @Quadir Hi Qadir, Is it possible to implement endless listview in 'tabs with fragments'. I tried but was not successful. Could you please give sample code for the same. – Vyshakh Jan 18 '13 at 13:16
  • sorry but i hav'nt used the fragments tabs. I have used the ActivtyGroup class and TabActivty. If you still need the code i can paste it on my answer – Qadir Hussain Jan 18 '13 at 13:18
  • @Quadir: yes Quadir. That will be helpful. Thanks – Vyshakh Jan 18 '13 at 13:24
  • @Quadir: Thanks a lot. I wanted to see how to implement endless adapter. Now I will try to implement this also in my code. – Vyshakh Jan 18 '13 at 13:54
0

Try out this way:

 @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;
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0
Activity act = this.getActivity();

This as a member variable will get your null, which results in the NPE at

act.runOnUiThread(returnRes);

Instead of the member variable, try to ask on runtime for the activity:

getActivity().runOnUiThread(returnRes);
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • Your suggestion worked like a charm. A million thanks. One more request. Now my listview is showing all the 50 entries at once. And when I scroll down to the last element, It starts diplaying from the beginning. I want to display only 10 elements and after that when I scroll down, It should show the next 10 elements. Could you please take a look if possible. Thanks. – Vyshakh Jan 18 '13 at 13:43
  • I would try the link @QadirHussain as posted. As long as you don't want to learn how to implement lazy loading by yourself, you should use an existing library for that. Otherwise you should use the debugger and go through your code step by step. It is a bit too complex at the moment... – WarrenFaith Jan 18 '13 at 13:48
  • @WarenFaith: I will give it a try. But the problem is I am not comfortable with tabs and fragments. Anyways, I will try to implement it. Thanks once again for your suggestions and effort. – Vyshakh Jan 18 '13 at 13:52