Reputation: 117
I have created custom list view, in a list there are 2 images(left,right) and a text in the middle when user click on image1, activity1 will be called and user clicks image2, activity2 will be called and when user clicks on text activity3 will be called corresponding to that position. Can any one please tell me how i can do this?
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
ImageView img = (ImageView)convertView.findViewById(R.id.contact_photo);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneNumber = objBean.getPhoneNo();
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(phoneNumber));
con.startActivity(intent);
}
});
holder.tvname = (TextView) view.findViewById(R.id.tvname);
holder.tvPhoneNo = (TextView) view.findViewById(R.id.tvphone);
if (holder.tvname != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tvname.setText(Html.fromHtml(objBean.getName()));
}
if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
&& objBean.getPhoneNo().trim().length() > 0) {
holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
}
return view;
}
public class ViewHolder {
public TextView tvname, tvPhoneNo;
}
Upvotes: 1
Views: 1655
Reputation: 49
I also have the same problem and i am trying to get a work around it. Personally i think androids API of the list view is terrible and should be improved upon so that it is easy to use and implement. I think its a bad idea to call your listeners in the override get view
method cause you can't always trust the android system to return to you the exact view in which you are requesting due to performance reasons. I am quoting for the developers of the list view. using the View Holder static class only helps to hold the views and access them faster but it doesn't assist in handling event listeners for specific items view within the list view item and honestly i haven't seen any reasonable solution on the internet or Google developer site.
Okay after two nights of racking my brains and endless testing i finally have a solution.
Using the setTag
and the ViewHolder
implementation but only differently, turn your ViewHolder static implementation into a View what i mean is this
static class ViewHolder extends View implements OnClickListener {
public ViewHolder(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
ImageView bomb;
public void assignList(){
if(bomb != null)
bomb.setOnClickListener(this);
}
public int position = -1;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("HOMEADAPTER", "OUR OWN STUFF");
}
}
Once you have done that in your activity where you implement your OnItemClickListener
all you need to do is get the tag you set like so
ViewHolder holder = (ViewHolder) view.getTag();
and thats all baby! you are 90% done the final 10% thing you need to do is what ever you wish to do with your gotten view.
Upvotes: 0
Reputation: 2670
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
MyHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_image, parent, false);
holder = new MyViewHolder();
convertView.setTag(holder);
}else {
holder = (MyViewHolder)convertView.getTag();
}
holder. b1 = (Button)convertView.findViewByID(R.id.b1);
holder. b2 = (Button)convertView.findViewByID(R.id.b2);
holder. tv = (TextView)convertView.findViewByID(R.id.tv);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//write intent to go to activity 1
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//write intent to go to activity 2
}
});
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//write intent to go to activity 3
}
});
return convertView;
}
class MyViewHolder{
public TextView tv;
public Button b1, b2;
}
hope this helps you....
Upvotes: 0
Reputation: 2735
First of all set your both image view's and text views clickable property to true in your XML.
android:clickable="true"
Now there are many ways to launch your respective activities.
1) you can right three different methods in your activity and call them on on-click of resp. view in XML itself. But while doing this you must write those methods in your activity , if you are using fragment write it in your main activity.
android:onClick="your method name"
2) In your getView method write on click Listeners for all three views and perform resp. actions in it.
3) Implement OnClickListener and depending on id's you can manage your click listener.
Upvotes: 0
Reputation: 109
You must implement OnClickListener for textView and imageViews in getView method of your adapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// CLICK LISTENERS
}
Upvotes: 1
Reputation: 10876
you can initialization custom view component in getView method and there you can define setOnClickLisner.
Upvotes: 1