tags:

views:

21556

answers:

7

Hi, I am new to android. I want to display the list of images using the ListView.I am dowloading the image at run time.The total number of images is not fix.

+11  A: 

I'd start with something like this (and if there is something wrong with my code, I'd of course appreciate any comment):

public class ItemsList extends ListActivity {

private ItemsAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 setContentView(R.layout.items_list);

 this.adapter = new ItemsAdapter(this, R.layout.items_list_item, ItemManager.getLoadedItems());
 setListAdapter(this.adapter);
}

private class ItemsAdapter extends ArrayAdapter<Item> {

 private Item[] items;

 public ItemsAdapter(Context context, int textViewResourceId, Item[] items) {
  super(context, textViewResourceId, items);
  this.items = items;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  View v = convertView;
  if (v == null) {
   LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         v = vi.inflate(R.layout.items_list_item, null);
  }

  Item it = items[position];
  if (it != null) {
   ImageView iv = (ImageView) v.findViewById(R.id.list_item_image);
   if (iv != null) {
    iv.setImageDrawable(it.getImage());
   }
  }

  return v;
 }
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
 this.adapter.getItem(position).click(this.getApplicationContext());
}
}

E.g. extending ArrayAdapter with own type of Items (holding information about your pictures) and overriden getView() method, that prepares view for items within list. There is also method add() on ArrayAdapter to add items to the end of the list.

R.layout.items_list is simple layout with "ListView"

R.layout.items_list_item is layout representing one item in list

Doesn't `findViewById` find the same view every time?
Casebash
A: 

In my case i would be retrieving values from sqlite database and have to display it in a list view...So how should i pass the images out from the database into the list view.. I am able to save and retrieve images individually and display it on the screen.But how do i pass a bitmap and display it in a list view.can you help me with some sample codes please...

Rahul
Rahul: If you want to ask a separate question, please start a new question instead of using an answer.
Casebash
+1  A: 

To get the data from the database, you'd use a SimpleCursorAdapter.

I think you can directly bind the SimpleCursorAdapter to a ListView - if not, you can create a custom adapter class that extends SimpleCursorAdapter with a custom ViewBinder that overrides setViewValue.

Look at the Notepad tutorial to see how to use a SimpleCursorAdapter.

JRL
A: 

here is a good sample to bind the SimpleCursorAdapter to a ListView

ambiguiti.es/2009/04/making-custom-rows-in-android-listviews/

Vincent
A: 

File name should match the layout id which in this example is : items_list_item.xml in the layout folder of your application

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >  

<ImageView android:id="@+id/R.id.list_item_image"
  android:layout_width="100dip"
  android:layout_height="wrap_content" />  
</LinearLayout>
David Parry
A: 

Published a sample code here http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview/3068012#3068012, should be helpful.

Fedor
A: 

in first answer where is the "Item[]" defined?