I have two buttons in each item that are set to invisible. I want, that when the user clicks on an item, the buttons only in that item turn to visible.
Im using a custom adapter for my list view...
public class LocationAdapter extends BaseAdapter{
String [] n;
Context context;
String[] a;
int bint = View.INVISIBLE;
private static LayoutInflater inflater=null;
public LocationAdapter(MainActivity mainActivity, String[] names, String[] addresses, int bint) {
// TODO Auto-generated constructor stub
this.bint = bint;
n=names;
context=mainActivity;
a=addresses;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return n.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView name;
TextView address;
Button b1;
Button b2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.rowlayout2, null);
holder.name =(TextView) rowView.findViewById(R.id.EditTextName);
holder.address =(TextView) rowView.findViewById(R.id.EditTextAddress);
holder.b1 = (Button) rowView.findViewById(R.id.Edit);
holder.b2 = (Button) rowView.findViewById(R.id.Delete);
holder.b1.setVisibility(bint);
holder.b2.setVisibility(bint);
holder.name.setText(n[position]);
holder.address.setText(a[position]);
rowView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+n[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
Can anyone please give me ideas on how i need to do this?
you only can set onclicklistener for view objects and its childs....so either you implementate your own API where you have a method that does the same as the standard onclicklistener but you can set it on every object that you want...would be very interesting imho...
or you use views like textviews etc and then set an onclicklistener on them!!!
here is what you can try Add onclicklsitener to buttom instead of entire view. Inside buttons xml to which you want apply click listener add following two lines
now apply listener to button as follows
Inside getview method declare holder final
and you are done
You need to set an
OnItemClickListener
on yourListView
and change the visibility of theButtons
insideonItemClick()
.Also note that your current implementation of
LocationAdapter
does not checkif(convertView == null)
and it does not set theViewHoler
as the tag of the rowView
.