我的ListView中有Button和TextView,我会在按钮上添加监听器,但我不能这样做。
其实我在我的适配器中:
[代码]
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
}
});
[/代码]
OnClick有效但是,我想在我的活动上显示警报,我不能这样做:/我不知道如何谎言我的活动和我的适配器
答案 0 :(得分:0)
通过使用ClickableListAdapter,我们可以为每个列表项的列表视图中的按钮提供操作。此代码可帮助您获取列表中的按钮功能。 公共课 { int id; 字符串名称; }
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);
}
答案 1 :(得分:-1)
事实上,当用户点击按钮时,我会在第一个列表上方显示一个列表。所以我想起AlertsDialog来做到这一点。如果用户点击整行,我会显示另一个活动。
但是就像适配器上的OnClick一样,我无法检索活动列表中所选项目的位置。
实际上点击整行是有效的,但不是点击行上的按钮。