2012-10-23 39 views
1

我有一個listView,其中每個列表項都有一個按鈕。我想設置一個onClickListener兩個列表項和按鈕 - 我很擔心,這是唯一可能做一個或另一個..如何爲列表項目和列表項目中的按鈕設置onClickListener?

這裏是我的適配器代碼:

private class MyListAdapter extends ResourceCursorAdapter { 

    // In your ListActivity class, create a new inner class that extends ResourceCursorAdapter. 
    //This inner class is the custom CursorAdapter we will use to manage how data is bound to a list item: 

    public MyListAdapter(Context context, Cursor cursor) { 
     super(context, R.layout.row_location, cursor); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     TextView text_first_line = (TextView) view.findViewById(R.id.location_row_item_main_text); 
     TextView text_second_line = (TextView) view.findViewById(R.id.location_row_item_secondary_text); 
     ImageView flagIcon = (ImageView) view.findViewById(R.id.flagIcon); 

     String row_text_component = cursor.getString(cursor.getColumnIndex(RMDbAdapter.COMPONENT)); 
     String row_text_position = ", Position " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.POSITION)); 
     if(row_text_position.equals(", Position Not Applicable")){ 
      row_text_position = ""; 
     } 
     String row_text_action = " - " + cursor.getString(cursor.getColumnIndex(RMDbAdapter.ACTION_REQUIRED)); 

     text_first_line.setText(row_text_component + row_text_position + row_text_action); 
     text_second_line.setText("test text, test text"); 

     String risk = cursor.getString(cursor.getColumnIndex(RMDbAdapter.RISK)); 
     if (risk.equals("Red Risk")){ 
      flagIcon.setImageResource(R.drawable.red_flag); 
     } 
     else if (risk.equals("Green Risk")){ 
      flagIcon.setImageResource(R.drawable.green_flag); 
     } 
     else if (risk.equals("No Risk")){ 
      flagIcon.setImageResource(R.drawable.note); 
     } 

    } 
} 

在之前的更簡單的活動中,我爲列表項設置了onListItemClick,但是如何設置按鈕的偵聽器?

非常感謝!

回答

0

在ListView行中可以有多個偵聽器。只需設置一個OnClickListener正常人一樣:

button.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     // Do something 
    } 
}); 

,當你有問題觸摸按鈕這隻會取代的OnListItemClick聽衆。

也請觀看Android的Romain Guy discussion adapters and efficiency

+0

謝謝山姆。會給它一個去,並儘快剔! – Scamparelli

相關問題