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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:08:37+00:00 2026-06-17T03:08:37+00:00

ok im fetching images from server and showing them in a gridview with title

  • 0

ok im fetching images from server and showing them in a gridview with title … im using baseadapter for gridview … everything is working good and fine … i have just one problem that i need to show a progressdialog when data is fetched from server and populated on the gridview …
im using AsyncTask right now for showing progress dialog but it freezes for few (like 10) seconds and then gridview shows…

this is my baseadapter class:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = listInflater.inflate(R.layout.grid_adapter_view, null);
        holder = new ViewHolder();
        holder.gridImg = (ImageView) convertView
                .findViewById(R.id.grid_item_image);
        holder.gridTitle = (TextView) convertView
                .findViewById(R.id.grid_item_label);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.gridTitle.setText(catItems.get(position).getName());
    try {
        URL url = new URL(catItems.get(position).getImg());
        Bitmap img = BitmapFactory.decodeStream(url.openConnection()
                .getInputStream());
        holder.gridImg.setImageBitmap(img);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        holder.gridImg.setImageResource(R.drawable.no_image);
        e.printStackTrace();
    }
    return convertView;
}

public class ViewHolder {
    ImageView gridImg;
    TextView gridTitle;

}

and here is asynctask im using:

mytask = new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected void onPostExecute(Boolean result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            main_grid.setAdapter(adater);
            if (progress.isShowing()) {
                progress.dismiss();
            }

        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            progress.show();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            JSONArray jArray = request.getCategories();
            try {
                for (int i = 0; i < jArray.length(); i++) {
                    CategoriesDetail catDetail = new CategoriesDetail();
                    JSONObject jobj = jArray.getJSONObject(i);
                    catDetail.setId(jobj.getString("id").toString());
                    catDetail.setName(jobj.getString("name").toString());
                    catDetail.setImg(jobj.getString("img").toString());
                    catDetail.setOrder(jobj.getString("order").toString());
                    items.add(catDetail);
                }
                adater = new CatGridAdapter(MainActivity.this, items);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                progress.dismiss();
                Toast.makeText(MainActivity.this,
                        "Error: " + e.getMessage(), Toast.LENGTH_LONG)
                        .show();
                return false;
            }

        }

    };

please tell me where and what im doing wrong …

  • 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-17T03:08:38+00:00Added an answer on June 17, 2026 at 3:08 am

    You are doing a network operation on the UI thread.

    BitmapFactory.decodeStream(url.openConnection().getInputStream());
    

    Take a look at this: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

    EDIT:
    Instead of decoding the stream in getView() only set the default image to the gridImg and trigger loadBitmap(catItems.get(position).getImg(), holder.gridImg).

    public void loadBitmap(String url, ImageView imageView) {
        new BitmapWorkerTask(imageView).execute(url);
    }
    
    class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
    
        public BitmapWorkerTask(ImageView imageView) {
            // Use a WeakReference to ensure the ImageView can be garbage collected
            imageViewReference = new WeakReference<ImageView>(imageView);
        }
    
        // Decode image in background.
        @Override
        protected Bitmap doInBackground(String... params) {
            return BitmapFactory.decodeStream(params[0].openConnection().getInputStream());
        }
    
        // Once complete, see if ImageView is still around and set bitmap.
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        }
    }
    

    This is all described in the link above. You should really read it to get a better understanding of threading in android.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using async task for fetching images from Web on my main view controller.
I am fetching number of images and text related to it from server Now
I have used both Gallery and ViewPager widgets, I am fetching images from web
I am using a image gallery in WordPress, images are fetching from database and
I'm working on fetching data from wiki pages. I'm using a combination of php
I've written a MIDlet that does several advanced things: fetching images from the web,
I am fetching some data from a site using its API-key and some other
I'm fetching some data from an MSSQL table using the mssql_fetch_object, but the text
I am fetching a lot of thumbnails in my application from a remote server
I have a php application getting x and y position constantly from the server.

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.