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 8353245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:12:03+00:00 2026-06-09T09:12:03+00:00

I am using android listview with lazyadapter to show images and textview in list.

  • 0

I am using android listview with lazyadapter to show images and textview in list.

and when user scrolls to the bottom i am using AsyncTask to add more contents but when i execute AsyncTask it kills the process.

here is my code.
mainactivity

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.NodeList;



import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.sax.Element;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AndroidJSONParsingActivity extends Activity {

    // url to make request


    // JSON Node names
    static final String TAG_CONTACTS = "comics";
    static final String TAG_ID = "id";
    static final String TAG_NAME = "title";
    static final String TAG_EMAIL = "id";
    static final String TAG_PHONE_MOBILE = "image";

    // contacts JSONArray
    JSONArray contacts = null;
    ListView list;
    LazyAdapter adapter;
    static int counter = 0;
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    list=(ListView)findViewById(R.id.list);
                    // call the function
    LoadData();

}

public void LoadData(){


      String url = "http://myurl?start=" + counter;


            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(url);

            try {
                // Getting Array of Contacts
                contacts = json.getJSONArray(TAG_CONTACTS);

                // looping through All Contacts
                for(int i = 0; i < contacts.length(); i++){
                    JSONObject c = contacts.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String email = c.getString(TAG_EMAIL);
                    String mobile = "http://www.funnydash.com/uploads/s/" + c.getString(TAG_PHONE_MOBILE);
                    // Phone number is agin JSON Object

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_EMAIL, email);
                    map.put(TAG_PHONE_MOBILE, mobile);

                    // adding HashList to ArrayList
                    contactList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


            /**
             * Updating parsed JSON data into ListView
             * */

            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, contactList);        
            list.setAdapter(adapter);



            list.setOnScrollListener(new EndlessScrollListener());

}

public class BackgroundLoadMore extends AsyncTask<Void, Void, Void> {

      @Override
        protected void onPreExecute() {
            // Showing progress dialog before sending http request


        }

    protected Void doInBackground(Void... unused) {
        LoadData();

        return (null);


    }

    protected void onPostExecute(Void unused) {
        // On completing background task
        // closing progress dialog etc,.
        list.setOnScrollListener(new EndlessScrollListener());

        }

}

      public class EndlessScrollListener implements OnScrollListener {

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

        public EndlessScrollListener() {
        }
        public EndlessScrollListener(int visibleThreshold) {
            this.visibleThreshold = visibleThreshold;
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            if (loading) {
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                    counter +=12;
                }
            }
            if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
                // I load the next page of gigs using a background task,
                // but you can call any function here.
                new BackgroundLoadMore().execute();
                loading = true;
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }
    }

}

Lazyadapter.java

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private ArrayList<HashMap<String, String>> data;

    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 



    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name

        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

        HashMap<String, String> song = new HashMap<String, String>();
        song = data.get(position);



        // Setting all values in listview

    title.setText(song.get(AndroidJSONParsingActivity.TAG_NAME));
    artist.setText(song.get(AndroidJSONParsingActivity.TAG_ID));

    imageLoader.DisplayImage(song.get(AndroidJSONParsingActivity.TAG_PHONE_MOBILE), thumb_image);
    return vi;
}

}

and this is what i get in my logcat

  • 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-09T09:12:04+00:00Added an answer on June 9, 2026 at 9:12 am

    The problem is because you’re attempting to set an adapter on your list from outside the UI thread. Views in Android can not be modified in a background thread, which in this case, means you can not modify the UI from the doInBackground method of an AsyncTask. So, your method LoadData() can’t call the following:

    adapter=new LazyAdapter(this, contactList);        
    list.setAdapter(adapter);
    list.setOnScrollListener(new EndlessScrollListener());
    

    You will need to return some data (probably contactList) from your doInBackground() method, and then move the offending code to the onPostExecute() method, which is run on the UI thread.

    The key problem noted in the stack trace is:

    CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a ListView that is setup like this: <ListView android:id=@android:id/list android:layout_width=fill_parent android:layout_height=fill_parent android:longClickable=false
I am using a list view in Android 1.5 to show a list of
I have show Array in listview. with black text color. if i using android.R.color.black
I have created a listview using LazyAdapter. When i scroll to the bottom of
I am working on an android project where I am using a ListView and
I am currently working on an Android project. I am using a ListView and
I am creating a app in android. In that i am using list view.
I have learned that when using android:entries with a ListView , it uses android.R.layout.simple_list_item_1
I am using android 2.2, phonegap 1.3, and jquery-mobile 1.0 I have a list
In ListView I can change the divider image using android:divider=image but I want to

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.