3 messages in com.googlegroups.android-beginners[android-beginners] How to set LIsten...
FromSent OnAttachments
Yatish23 May 2008 22:41 
Mark Murphy24 May 2008 04:13 
Ahmed Abdel samea24 May 2008 04:39 
Subject:[android-beginners] How to set LIstener on ListView
From:Yatish (Y.Y.@gmail.com)
Date:05/23/2008 10:41:08 PM
List:com.googlegroups.android-beginners

Hi all, I want to set Listener on on the listView but the listview is set by BaseAdapter class . and i want set listener on each listview . please help me . Here is my code And i want to set Listener on ListView Which is set by adapter.

public class List8 extends ListActivity {

PhotoAdapter mAdapter;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

// Use a custom layout file setContentView(R.layout.list_8);

// Tell the list view which view to display when the list is empty getListView().setEmptyView(findViewById(R.id.empty));

// Set up our adapter mAdapter = new PhotoAdapter(this); setListAdapter(mAdapter);

// Wire up the clear button to remove all photos Button clear = (Button) findViewById(R.id.clear); clear.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) { mAdapter.clearPhotos(); } });

// Wire up the add button to add a new photo Button add = (Button) findViewById(R.id.add); add.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) { mAdapter.addPhotos(); } }); }

/** * A simple adapter which maintains an ArrayList of photo resource Ids. * Each photo is displayed as an image. This adapter supports clearing the * list of photos and adding a new photo. * */ public class PhotoAdapter extends BaseAdapter {

private Integer[] mPhotoPool = { R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2, R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5, R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};

private ArrayList<Integer> mPhotos = new ArrayList<Integer>();

public PhotoAdapter(Context c) { mContext = c; }

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

public Object getItem(int position) { return position; }

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

public View getView(int position, View convertView, ViewGroup parent) { // Make an ImageView to show a photo ImageView i = new ImageView(mContext);

i.setImageResource(mPhotos.get(position)); i.setAdjustViewBounds(true); i.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); // Give it a nice background i.setBackground(android.R.drawable.picture_frame); return i; }

private Context mContext;

public void clearPhotos() { mPhotos.clear(); notifyDataSetChanged(); }

public void addPhotos() { int whichPhoto = (int)Math.round(Math.random() * (mPhotoPool.length - 1)); int newPhoto = mPhotoPool[whichPhoto]; mPhotos.add(newPhoto); notifyDataSetChanged(); }

} }