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);
    }
4

1 回答 1

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);

希望这可以帮助。

于 2012-07-08T09:36:38.720 回答