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();
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---