PDA

View Full Version : ListView. Getting itemId when click on imageView



vovs
04-19-2011, 12:10 PM
Hello, guys!

In my ListView each item consists of ImageView and TextView. I want when I click on image - get itemid(and then show image in dialog).

How to catch click on ImageView and get Id of ListItem?
http://i.piccy.info/i5/21/14/1401421/lv_240.jpg



ImageView image;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_friends);

contactsList=(ListView)findViewById(android.R.id.l ist);
contactsList.setOnItemClickListener(clickListener) ;

image = (ImageView)findViewById(R.id.image_a);
image.setOnClickListener(imListener);

}

OnItemClickListener clickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long itemId) {
Toast.makeText(getApplicationContext(), "listItem " + itemId, Toast.LENGTH_SHORT).show();

}
};

OnClickListener imListener = new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Image of listItem ", Toast.LENGTH_SHORT).show();

}
};

vovs
04-20-2011, 01:40 PM
It is easy..

I must write class, that implements OnClickListener in my ListAdapter:


class youaddaper extends BaseAdapter{

public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflate = LayoutInflater.from(context);
View v = inflate.inflate(id, parent, false);

ImageView imageview = (ImageView) v.findViewById(R.id.imageView);
imageview.setOnClickListener(new imageViewClickListener(position));
//you can pass what ever to this class you want,
//i mean, you can use array(postion) as per the logic you need to implement
}
class imageViewClickListener implements OnClickListener {
int position;
public imageViewClickListener( int pos)
{
this.position = pos;
}

public void onClick(View v) {
{// you can write the code what happens for the that click and
// you will get the selected row index in position
}
}