how to show drawable folder images in lazy list view

918 Views Asked by At

I am using an example to show a list view with an image and text. It is taking too much time when the list is shown. So what I want is to use the this list as a lazy image list so that all of the images are not loaded right away, but are loaded as users scroll down.

public class SetImageText {
public int icon;
public String title;
public SetImageText()
     {
 super();
 }

public SetImageText(int icon,String title)
{
    super();
    this.icon=icon;
    this.title=title;
}
      }

 //Main Class 
         package com.example.customizearrayadapter;
         import android.R.layout;
         public class MainActivity extends Activity {
         private ListView listView1;
      @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         SetImageText data[]=new SetImageText[]
        {
         new SetImageText(R.drawable.limage,"Cloudy"),
         new SetImageText(R.drawable.limage,"Showers"),
         new SetImageText(R.drawable.limage,"snow"),
         new SetImageText(R.drawable.limage,"Storm"),
         new SetImageText(R.drawable.limage,"Sunny"),
         new SetImageText(R.drawable.cow, "kriss"),
         new SetImageText(R.drawable.dog,"wazid"),
         new SetImageText(R.drawable.hen, "anuj"),
         new SetImageText(R.drawable.horse, "abhay"),
         new SetImageText(R.drawable.kbc, "abid"),
         new SetImageText(R.drawable.mj ,"aman"),
         new SetImageText(R.drawable.penguine ,"ali"),
        new SetImageText(R.drawable.rat, "hemant"),
        new SetImageText(R.drawable.pizza, "jai"),
        new SetImageText(R.drawable.tortoise ,"kapil")
     };

       SetImageTextArrayAdapter adapter=new        SetImageTextArrayAdapter(this,R.layout.listview_row_item,data);
    listView1=(ListView)findViewById(R.id.listView1);
    View header=(View)getLayoutInflater().inflate(R.layout.listview_header,null);
    listView1.addHeaderView(header);
    listView1.setAdapter(adapter);
}


  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
}


     package com.example.customizearrayadapter;
     import java.util.Set;
     import android.app.Activity;
     import android.content.Context;

    public class SetImageTextArrayAdapter  extends ArrayAdapter<SetImageText>{

Context context;
int layoutResourceId;
SetImageText data[]=null;

public SetImageTextArrayAdapter(Context context, int layoutResourceId,
        SetImageText[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId=layoutResourceId;
    this.context=context;
    this.data=data;
    // TODO Auto-generated constructor stub
}
public View getView(int position,View convertView,ViewGroup parent)
{
     View row = convertView;
        SetImageTextHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new SetImageTextHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (SetImageTextHolder)row.getTag();
        }
        SetImageText setimagetext = data[position];
        holder.txtTitle.setText(setimagetext.title);
        holder.imgIcon.setImageResource(setimagetext.icon);

        return row;



}



static class SetImageTextHolder
{
    ImageView imgIcon;

    TextView txtTitle;
}

       }
3

There are 3 best solutions below

1
Navaneeth On

First, put some dummy image in the image view. And You can use Async task for loading the images once it is ready.

0
DarwinLouis On

You can use a library such as Picasso Library to load the images from the drawable folder.

I'm assuming that you're using a Custom Adapter to load the list of images and texts. Use the code snippet below in the getView() method of your adapter.

Picasso.with(context).load(R.drawable.name_of_your_image).into(imageView1);

Hope you'll find this helpful.

Best regards!

0
Gorgeous_DroidVirus On

In that situation you need the volley library to work of that any contect like FILE,DRAWBLE etc...

Press link..this example solve issue of drawable