通過使用ClickableListAdapter,我們可以爲列表視圖中的每個列表項目提供按鈕操作。此代碼幫助您在列表中顯示按鈕功能。 public class Play { 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);
}
到底什麼問題?無法做到這不是一個真正的問題描述。 And Alert是指AlertDialog? – RoflcoptrException 2010-05-11 12:24:08