button in listView
I have But开发者_如何学Goton and TextView in my ListView , and I would add listener on button but I can't do it.
Actually I have in my adapter :
[code]
imgClassement.setImageResource(drawable);
imgClassement.setTag(mail);
imgClassement.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
//I would like display an alerts in my activity
}
});
[/code]
The OnClick works but, I would like to display alerts on my activity and I can't do it :/ I don't know how to lie my activity and my adapter
By using the ClickableListAdapter we can give action to button in listview for each list item.This code while help you for button functionality in list. public class Play { int id; String name; }
ArrayList<Play> playlistname=new ArrayList<Play>();
Listview playlist;
MyClickableListAdapter list= new MyClickableListAdapter(this,R.layout.editlist, playlistname);
playlist.setAdapter(list);
//xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="45dip"
android:orientation="horizontal"
>
<Button android:paddingLeft="5dip" android:id="@+id/playlisticon"
android:layout_gravity="center_vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="5sp" />
<TextView
android:layout_width="fill_parent" android:id="@+id/playlistname"
android:layout_height="wrap_content" android:textColor="#000000"
android:padding="10dp" android:textStyle="bold"
android:textSize="12px" android:layout_weight="1">
</TextView>
</LinearLayout>
static class MyViewHolder extends "packagename of your app".ClickableListAdapter.ViewHolder
{
TextView text;
ImageView icon;
public MyViewHolder(TextView id,ImageView i)
{
text = id;
icon = i;
}
}
//MyClickableListAdapter to perform on click functionality on ListView
public class MyClickableListAdapter extends ClickableListAdapter
{
public MyClickableListAdapter(Context context, int viewid,List<Play> objects)
{
super(context, viewid, objects);
}
protected void bindHolder(ViewHolder holder)
{
MyViewHolder mvh = (MyViewHolder) holder;
Play item = (Play)mvh.data;
mvh.icon.setVisibility(View.VISIBLE); //this is button
mvh.icon.setImageResource(R.drawable.cancel_button);
mvh.text.setText(item.name);//this textview
}
@Override
protected ViewHolder createHolder(View v)
{
TextView text = (TextView) v.findViewById(R.id.playlistname);
ImageView icon = (ImageView) v.findViewById(R.id.playlisticon);
ViewHolder mvh = new MyViewHolder(text,icon);
icon.setOnClickListener(new ClickableListAdapter.OnClickListener(mvh)
{
public void onClick(View v, ViewHolder viewHolder)
{
// we toggle the enabled state and also switch the icon
MyViewHolder mvh = (MyViewHolder) viewHolder;
final Play item = (Play) mvh.data;
//check the id to delete the PlayList from second List
//here u can write the function that has to work while we
click on each button in list
}
});
return mvh; // finally, we return our new holder
}
//this class for ClickableListAdapter used for clicking the any item in the list.....
public abstract class ClickableListAdapter extends BaseAdapter
{
String type;
private LayoutInflater mInflater;
private List<?> mDataObjects;
private int mViewId;
public static class ViewHolder
{
public Object data;
}
public static abstract class OnClickListener implements View.OnClickListener
{
private ViewHolder mViewHolder;
public OnClickListener(ViewHolder holder)
{
mViewHolder=holder;
}
public void onClick(View v)
{
onClick(v,mViewHolder);
}
public abstract void onClick(View v, ViewHolder viewHolder);
// TODO Auto-generated method stub
};
public ClickableListAdapter(Context context, int viewid,
List< "applicationpackagename".Play> objects)
{
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
mDataObjects = objects;
mViewId = viewid;
type="Rowcontain";
if (objects == null)
{
mDataObjects = new ArrayList<Object>();
}
}
public ClickableListAdapter(Context context, int viewid,
List< "applicationpackagename".Play> objects, int x)
{
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
mDataObjects = objects;
mViewId = viewid;
type="Rowdata";
if (objects == null)
{
mDataObjects = new ArrayList<Object>();
}
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return mDataObjects.size();
}
@Override
public Object getItem(int position)
{
// TODO Auto-generated method stub
return mDataObjects.get(position);
}
@Override
public long getItemId(int position)
{
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
if(view==null)
{
view = mInflater.inflate(mViewId, null);
// call the user's implementation
holder = createHolder(view);
// we set the holder as tag
view.setTag(holder);
}
else
{
// get holder back...much faster than inflate
holder = (ViewHolder) view.getTag();
}
// we must update the object's reference
holder.data = getItem(position);
// call the user's implementation
bindHolder(holder);
return view;
}
protected abstract ViewHolder createHolder(View v);
protected abstract void bindHolder(ViewHolder h);
}
In fact , when user click on button, I would display a list above the first one. So I think about AlertsDialog to do that. And if user click on the entire row, I display another activity.
But like the OnClick is on the Adapter, I couldn't retrieve position of the selected item of the list on the activity.
Actually the click on the entire row works, but not the click on the button which is on the row.
精彩评论