Pergunta

My questions are about delay image loading using AQuery

So my app is this : i read a json, that contains various information, and links to images(this is done in the procedure doInBackground from AsyncTask). Also, after reading the links in this procedure, I also read the pics (see the code below for more info)

 class GetMessages extends AsyncTask<String, Void, String> {


    @Override
protected String doInBackground(String... params) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(uri);

            // code that reads the json .......
    Bitmap picture = null;
    try {
      URL newurl = new URL(json.getString("pic_url"));
      picture = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
    }catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

            // after this, i put all the read info, and the pics in a list of 
            // objects, that is then usen in the method onPostExecute to populate
            //  my custom adapter ...see code below 

static class ViewHolder {
ImageView picView;
 }

public class MyCustomBaseAdapter extends BaseAdapter  {

    private LayoutInflater mInflater;

    private ArrayList<Product> products;

    public ArrayList<Product> getProducts() {
        return products;
    }

    public void setProducts(ArrayList<Product> products) {
        this.products = products;
    }

    public MyCustomBaseAdapter(Context context, ArrayList<Product> products) {
         this.products = products;
         this.mInflater = LayoutInflater.from(context);
     }

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

     public Object getItem(int position) {
         return products.get(position);
     }

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

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

         ViewHolder holder;

         if (convertView == null) {
             convertView = mInflater.inflate(R.layout.product_item, null);
             holder = new ViewHolder();
             holder.picView = (ImageView) convertView.findViewById(R.id.pic_view);

             convertView.setTag(holder);

         } else {
             holder = (ViewHolder) convertView.getTag();
         }


         holder.picView.setImageBitmap(products.get(position).getPicture());



         return convertView;
     }

As you can already figure out...if i have like 15 pics...the users gets sleepy before all that information is loaded...

  1. What i want to do, is to use your Delay Image Loading...however, i can't seem to put the pieces together...how should i modify my adapter?
  2. Do i basically add the code from your wiki page, and use it instead of my method get view ? ...any help or a link to a more complete example then what is on the wiki, would help a beginner like me :)
  3. Can this framework be used in a commercial app too?

Thanks :)

Foi útil?

Solução

Here's the source of the demo that use shouldDelay:

https://github.com/androidquery/androidquery/blob/master/demo/src/com/androidquery/test/image/ImageLoadingListActivity.java https://github.com/androidquery/androidquery/blob/master/demo/src/com/androidquery/test/image/ImageLoadingList4Activity.java

Try to do it 1 step at a time.

I suggest you just replace few lines in your getView method as in the example and use aq.image to load your images with your urls.

Replace:

holder.picView.setImageBitmap(products.get(position).getPicture());

with:

AQuery aq = new AQuery(convertView);
aq.id(holder.picView).image(products.get(position).getPicture());

After that add shouldDelay to make it more efficient.

The answer belongs to Peter Liu, one of the gurus concerning this framework :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top