2012-07-08 3 views
1

사용자 지정 목록 항목 (단추 포함)에서 ListView를 사용하고 있습니다. 나는 단추의 OnClickListener를 (어댑터 클래스에 정의 된) 내부 클래스로 설정하는 사용자 지정 어댑터가 있습니다.다른 레이아웃에서보기 액세스

listview가 표시된 클래스의 버튼 (ListAcitivty 클래스의 다른 xml 파일에 정의 됨)에 액세스하려고합니다. 이 클래스 내에 onClickListener 메서드를 설정하고 싶습니다. 이 일을하는 가장 좋은 방법은 무엇입니까?

편집 : 이것은 내 list_item.xml (내 ListView 행에 사용)입니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 


<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="22dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="10dp" 
    android:layout_marginTop="4dp" 
    android:contentDescription="Color" 
    android:src="@drawable/ic_launcher" > 
</ImageView> 

<EditText 
    android:id="@+id/nameEdit" 
    android:layout_width="250dp" 
    android:layout_height="wrap_content" 
    android:textSize="20dp" 
    android:inputType="textPersonName" 
    > 
</EditText> 


<Button 
    android:id="@+id/deleleButton" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@drawable/delete_button" > 

</Button> 

</LinearLayout> 

ListActivity를 확장하는 클래스 내에서 (id : deleteButton) 버튼에 액세스하려면 어떻게합니까? listactivity를 확장하는 클래스는 별도의 레이아웃을 가지고 있습니다 (main.xml을 보자). listactivity를 확장하는 클래스에서 setContentView (main)를 수행하면 findViewById (R.id.deleteButton)는 null을 반환합니다.

편집 2 : 이것은 ListActivity를 확장 한 클래스입니다. setContentView 다음에 findViewById (R.id.deleteButton)를 배치하면 null이 반환됩니다. 이를 달성하기

public class PlayerSelection extends ListActivity { 
    ListView list; 
    ArrayList<PlayerField> textviews = null; 
    PlayerFieldsAdapter adapter = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.player_selection_page); 
     list = getListView(); 

     textviews = new ArrayList<PlayerField>(); 
     for (int i = 0; i< GlobalVariables.getPlayers(); i++) { 
      textviews.add(i, new PlayerField()); 
     } 
     adapter = new PlayerFieldsAdapter(this, R.layout.list_item, textviews); 

    ViewGroup header = (ViewGroup) getLayoutInflater().inflate(R.layout.list_header, null); 
    Button backButton = null; 
    for (int i = 0; i < header.getChildCount(); i++) { 
     View v = header.getChildAt(i); 
     if (v instanceof Button) { 
      backButton = (Button) v; 
      break; 
     } 
    } 
    backButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Intent optionsIntent = new Intent(PlayerSelection.this, OptionsScreen.class); 
       startActivity(optionsIntent); 
      } 
     }); 
    list.addHeaderView(header); 
     list.setAdapter(adapter); 
    } 
+0

일부 코드 게시 문제를 이해하기 힘들 기 때문에 고려해야합니다. – Egor

+0

코드를 추가했습니다. 이제는 더 명확 해지기를 바랍니다. – user1478754

+0

사실, XML이 아닌 Java 코드를 의미했습니다. – Egor

답변

1

쉬운 방법은 CustomAdapter 클래스의 내부 필드를 만드는 것입니다 :

private OnClickListener listener; 

다음이 필드에 대한 setter 메소드를 지정합니다. 다음 단계는 그처럼 Activity 클래스 내에서이 리스너를 지정하는 것입니다 : 내부 그리고

CustomAdapter adapter = (CustomAdapter) getListView().getAdapter(); 
adapter.setListener(<your implementation of the listener>); 

당신의 CustomAdapter의 당신이 당신의 버튼이 리스너를 설정 getView() 방법이 도움이

Button btn = (Button) convertView.findViewById(R.id.btn); 
btn.setOnClickListener(listener); 

희망.

+0

감사합니다. 잘 작동합니다 :) – user1478754

+0

@ user1478754? 천만에요! – Egor

+0

"<청취자 구현>"대신 "작성 대상"@Egor에 문의하십시오. –