Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:26:58+00:00 2026-06-17T11:26:58+00:00

I am trying to implement lazy load on a listview in a tab fragment

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:26:58+00:00Added an answer on June 17, 2026 at 11:26 am
    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to implement a lazy load more items when the user gets to
I'm trying to implement a lazy sequence (meaning that the next item is only
So I am trying to implement a basic image lazy load feature to speed
I'm having a hard time trying to implement a lazy load for a group
I'm trying to implement the Lazy Load plugin into my jQueryMobile site to help
I am trying to implement Lazy Singleton with Generics and Inheritance. I have created
I have created a dynamic listview with lazy loading. I am trying to show
I am trying to implement lazy loading within my primefaces datatable setup. Currently, this
I'm trying to implement lazy loading on a blogger blog, but I'm running into
I am trying to implement lazy partitioning of an iterator object that yields slices

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.