问题
I have a custom ListView
which contains a Button
. The function of this button is Delete button. Whenever user click to this button, the current Row will be deleted.
- How can I do it?
- How can I set
onClickListener
for this button? - How can I catch the row Id which this button is on?
Thanks in advance.
回答1:
In your Custom Adapater class's getView()
buttonDelete.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Use position parameter of your getView() in this method it will current position of Clicked row button
// code for current Row deleted...
}
});
回答2:
First, that will be very easy for you to handle the click on this item.
You just have to add a listener with: myButton.setOnClickListener(mBuyButtonClickListener)
. This action is usually done on the getView(...)
implementation of your ListView
.
Your listener can know what is the item position of the button by using myListView.getPositionForView(myButton)
.
Look at this sample of the listener :
private OnClickListener mBuyButtonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
final int position = getListView().getPositionForView(v);
if (position != ListView.INVALID_POSITION) {
//DO THE STUFF YOU WANT TO DO WITH THE position
}
}
};
When this is done, you should have problems to handle the click on the item itself and some other touch event propagation.
So, you should read this article that fully describes the use of multiple touchable areas inside a ListView
. It will be very helpful.
回答3:
Use this inside the getView method of your adapter class.
holder.button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
}
});
回答4:
I looked everywhere and non of the answers helped me until one did.
What i did was create a HolderView and wrapped up all my textviews and other widgets in the getView mathod of the adpater:
put this class in:
static class ViewHolder
{
TextView text1;
TextView text2;
TextView text3;
}
and i also decalared the same ones normally as they are seperate now:
TextView text1;
TextView text2;
TextView text3;
then declare all the views and widgets after inflating the layout in getView:
holder = new ViewHolder();
holder.text1 = (TextView) vi.findViewById(R.id.textView2single);
holder.text2 = (TextView) vi.findViewById(R.id.textView3single);
holder.text3 = (TextView) vi.findViewById(R.id.textView4single);
inside the onClick method, i used:
View parentView = (View) v.getParent();
and then declared all my widgets again using this parentview:
text1 = (TextView) parentView.findViewById(R.id.textView2single);
text2 = (TextView) parentView.findViewById(R.id.textView3single);
text3 = (TextView) parentView.findViewById(R.id.textView4single);
In doing this im forcing my onClick to get the parent position of the list view item, therefore all widgets inside that parent can be called easily, then you can do what you like with it. This is the only way i have found that works.
回答5:
Create a custom list view adapter, extend BaseAdapter and implement OnClickListener
public class ListAdapter extends BaseAdapter implements OnClickListener {}
override the getView method
@Override
public View getView(int
position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.b1= (Button) row.findViewById(R.id.b1);
b1.setOnClickListener(this);
else {
holder = (ViewHolder) row.getTag();
}
}
now override the onclick method
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.b1:
//button b1 click event
break;
}
}
public class ViewHolder {
Button b1;
}
回答6:
Create a new View type variable and assign it with currenView(Check if iit is null).Define variables(ImageButton taken here) and set viewId in the below mentioned way.
public View getView(int position, View convertView, ViewGroup parent) {
View itemview=convertView;
if (itemview==null)
{
itemview=getLayoutInflater().inflate(R.layout.group_list,parent,false);
}
final group_contacts currentcontact=g_contact.get(position);
ImageButton delete=(ImageButton) itemview.findViewById(R.id.imgbtn_delete);
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Add your code here to run on tapping button
}
});
return itemview;
}
回答7:
To use onClickListener()
method on a Button
of a ListView
, you Must Apply onClickListener()
on the Button in Adapter
's getView
Methode.
But the problem is, you will not be able to get the Value from the position
parameter of getView
.
To get the position
of the Button you have Touched, you must Call getPositionForView()
method on your ListView
.
Lets discus with an Example For Better Understanding,
Consider you have a
ListView
namedExampleListview
A
Button
on it namedExampleButton
and u want to useonClickListener()
on it.Examplebutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final int position= ExampleListview..getPositionForView(v); /*Where 'v' is actually the 'View' object Which you are clicking.*/ /*you can now right your code her & determine the your desired button by using the position parameter */ }
来源:https://stackoverflow.com/questions/11591501/how-to-use-onclicklistener-method-of-button-on-a-listview