user782104
user782104

Reputation: 13555

Can not reference async task in custom adapter

The problem is , when I set the async download task at custom adapter getView function, it return

ImageLoader cannot be resolved to a type

error. The class is in different package of same project, it normally can import but in this case it only show error. How to fix the problem? Thanks for helping

LeadersAdapter.java

public class LeadersAdapter extends ArrayAdapter<Record> {


    public LeadersAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public LeadersAdapter(Context context, int resource, List<Record> items) {
        super(context, resource, items);
    }

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

        View v = convertView;

        if (v == null) {

            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.leader_record_row, null);

        }

        Record p = getItem(position);

        if (p != null) {

            ImageView pic = (ImageView) v.findViewById(R.id.profile_pic);
            TextView name = (TextView) v.findViewById(R.id.name);
            TextView pts = (TextView) v.findViewById(R.id.pts);

            new ImageLoader().execute(pic,p.url);
            name.setText(p.name);
            pts.setText(p.pts);
        }

        return v;

    }
}

ImageLoader.java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

class ImageLoader extends AsyncTask<Object, Void, Bitmap> {

    private static String TAG = "ImageLoader";
    public InputStream input;
    public ImageView view;
    public String imageURL;

    public ImageLoader(String text){

    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        try {

            view = (ImageView) params[0];
            imageURL = (String) params[1];

            URL url = new URL(imageURL);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null && view != null) {
            view.setImageBitmap(result);
        }
    }
}

Upvotes: 0

Views: 530

Answers (2)

Fate Chang
Fate Chang

Reputation: 207

I'd recommend Square's Picasso lib, which is easy to use.

Simple one line to load image from web into ImageView.

Ex:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Upvotes: 0

Yjay
Yjay

Reputation: 2737

Your ImageLoader class (which is not defined as public) is using the default access modifier, package-private, so it is only visible in it's own package.

Changing...

class ImageLoader [...]

to...

public class ImageLoader [...]

should fix your accessibility problem.

Also, I would highly recommend looking into an existing image loading library (like Universal Image Loader) instead of baking your own.

Upvotes: 1

Related Questions