Access View from Another Layout

2019-07-25 07:47发布

I'm using a ListView with custom list items (including a button). I have a custom adapter, in which I set the button's OnClickListener to an inner class (defined in the adapter class).

I want to access the button in the class (defined in a different xml file from the ListAcitivty class's) in which the listview is displayed. I want to set it's onClickListener method inside this class. What's the best way to go about doing this?

Edit: This is my list_item.xml (I use for my ListView rows).

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

How do I access the button (id:deleteButton) inside my class that extends ListActivity? The class that extends listactivity has a separate layout (main.xml let's say). If I do setContentView(main) inside the class that extends listactivity, findViewById(R.id.deleteButton) would return null.

Edit 2: This is my class that extends ListActivity. If I place findViewById(R.id.deleteButton) after the setContentView it returns 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);
    }

1条回答
劳资没心,怎么记你
2楼-- · 2019-07-25 08:30

Easy way to achieve this is to create a field inside your CustomAdapter class:

private OnClickListener listener;

Then specify a setter method for this field. Next step will be to specify this listener inside your Activity class like that:

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

Then inside your CustomAdapter's getView() method you set this listener to your button:

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

Hope this helps.

查看更多
登录 后发表回答