2012-07-08 97 views
1

我对自定义列表项目(包括按钮)使用ListView。我有一个自定义的适配器,其中我将按钮的OnClickListener设置为内部类(在适配器类中定义)。从另一个布局访问视图

我想访问在其中显示列表视图的类中的按钮(定义在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。如果我将findViewById(R.id.deleteButton)放在setContentView之后,它将返回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 2012-07-08 08:58:09

+0

我添加了代码,希望现在更清楚。 – user1478754 2012-07-08 09:19:34

+0

我的意思是Java代码,而不是XML – Egor 2012-07-08 09:21:02

回答

1

简单的方法来实现这一目标是创建您CustomAdapter类中的字段:

private OnClickListener listener; 

然后指定用于该领域的setter方法。下一步将是指定Activity类中这个监听器这样的:

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

内。然后你CustomAdaptergetView()方法,你这个监听器设置为您的按钮:

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

希望这有助于。

+0

谢谢!工作正常:) – user1478754 2012-07-08 10:16:52

+0

@ user1478754?别客气! – Egor 2012-07-08 10:17:30

+0

我想问@Egor,写什么来代替“<您的监听器的实现>” – 2014-02-22 06:42:49

相关问题