2010-05-11 58 views
1

我在我的ListView中有Button和TextView,並且我會在按鈕上添加偵聽器,但我無法做到這一點。listView中的按鈕

其實我有我的適配器:

[代碼]

 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

到底什麼問題?無法做到這不是一個真正的問題描述。 And Alert是指AlertDialog? – RoflcoptrException 2010-05-11 12:24:08

回答

-1

事實上,當用戶點擊按鈕,我會顯示一個列表上方的第一個。所以我想到了AlertsDialog來做到這一點。如果用戶點擊整行,我會顯示另一個活動。

但是就像適配器上的OnClick一樣,我無法檢索活動列表中所選項目的位置。

實際上點擊整行的工作,但不是點擊行上的按鈕。

+0

請不要將更多的細節問題作爲答案發布。只需編輯您的問題 – 2010-05-11 13:12:30

+0

對不起,我沒有注意到我們可以添加評論。 – Nanis 2010-05-11 13:15:30

0

通過使用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); 
}