2015-08-28 80 views
-1

您好我有一個listview及其適配器和對象類,每個listview位置都有一個按鈕,並且在適配器中有一個clicklistener,但是當我點擊我的buttonlistview它返回最後一個項目的位置。從適配器中的onclick監聽器獲取listiview的對象

這裏是我的適配器:

public class comentario_adapter extends BaseAdapter { 
    protected Activity activity; 
    protected ArrayList<comentario_obj> items; 
    private Context mContext; 
    comentario_obj prod; 
    ImageButton correcta; 
    boolean marcada = false; 
    View vi; 
    public comentario_adapter(Activity activity, ArrayList<comentario_obj> items, Context context) { 
     this.activity = activity; 
     this.items = items; 
     this.mContext=context; 
    } 

    @Override 
    public int getCount() { 
     return items.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return items.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     vi=convertView; 

     if(convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      vi = inflater.inflate(R.layout.comentario_adapter, null); 
     } 

     prod = items.get(position); 

     TextView title_coment = (TextView) vi.findViewById(R.id.title_coment); 
     ImageButton voteup= (ImageButton) vi.findViewById(R.id.voteup); 
     ImageButton votedown= (ImageButton) vi.findViewById(R.id.votedown); 
     title_coment.setText(prod.getTitulo()); 

     ImageButton btn=(ImageButton)vi.findViewById(R.id.correcta); 
     btn.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if(mContext instanceof verPregunta_class){ 
        ((verPregunta_class) mContext).MyMethod(); 
         //NEED THE TITLE OF LISTVIEW POSITION CLICKED 
       } 
      } 
     }); 

     return vi; 
    } 
    public interface IMethodCaller{ 
     void MyMethod(); 
    } 
} 
+0

天真的方式:讓'prod'本地最終或使用View.Tag ......也使用谷歌,問baziilion次 – Selvin

+0

http://stackoverflow.com/questions/14782868/android-removing-item-from-listview- is-not-working-properly – Palejandro

+0

是的,但是我該怎麼處理這個標籤? –

回答

0

您可以在OnCreate中創建,例如,爲您的ListView和裏面的方法setOnItemClickListener,要求該項目在您的項目陣列的位置。例如,

listview.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, 
     int pos, long id) { 

     String titulo = array.get(pos).getTitulo(); 
} 

希望它可以幫助

再見 });

+0

謝謝,但我需要適配器類中的標題 –

+0

嗯相同的想法應該適用於適配器。你試過了嗎? – jekeyeke

+0

不,因爲我還沒有列表視圖 –

0

確定我已經找到了解決方案與setTag

public class comentario_adapter extends BaseAdapter { 
    protected Activity activity; 
    protected ArrayList<comentario_obj> items; 
    private Context mContext; 
    comentario_obj prod; 
    ImageButton correcta; 
    boolean marcada = false; 
    View vi; 
    public comentario_adapter(Activity activity, ArrayList<comentario_obj> items, Context context) { 
     this.activity = activity; 
     this.items = items; 
     this.mContext=context; 
    } 

    @Override 
    public int getCount() { 
     return items.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return items.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     vi=convertView; 

     if(convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      vi = inflater.inflate(R.layout.comentario_adapter, null); 
     } 

     prod = items.get(position); 

     TextView title_coment = (TextView) vi.findViewById(R.id.title_coment); 
     ImageButton voteup= (ImageButton) vi.findViewById(R.id.voteup); 
     ImageButton votedown= (ImageButton) vi.findViewById(R.id.votedown); 
     title_coment.setText(prod.getTitulo()); 

     final ImageButton btn=(ImageButton)vi.findViewById(R.id.correcta); 
     **btn.setTag(prod.getId());** 
     btn.setOnClickListener(new Button.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if(mContext instanceof verPregunta_class){ 
        ((verPregunta_class) mContext).MyMethod(); 
         **String title = (String) v.getTag();** 
       } 
      } 
     }); 

     return vi; 
    } 
    public interface IMethodCaller{ 
     void MyMethod(); 
    } 
} 

感謝所有!