访问查看从另一个布局 [英] Access View from Another Layout
问题描述
我使用自定义列表项(包括按钮)一个ListView。我有一个自定义适配器,我在其中设置按钮的OnClickListener到一个内部类(在适配器类中定义)。
我要访问该类中显示列表视图(从ListAcitivty类的在不同的XML文件中定义)按钮。我想设置它的这个类中的方法onClickListener。什么是去这样做的最佳方式?
编辑:
这是我的list_item.xml(我用我的ListView行)。
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT>
< ImageView的
机器人:ID =@ + ID / ImageView的
机器人:layout_width =22dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =4DP
机器人:layout_marginRight =10dp
机器人:layout_marginTop =4DP
机器人:contentDescription =颜色
机器人:SRC =@绘制/ ic_launcher>
< / ImageView的><的EditText
机器人:ID =@ + ID / nameEdit
机器人:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:TEXTSIZE =20dp
安卓的inputType =textPersonName
>
< /&的EditText GT;
<按钮
机器人:ID =@ + ID / deleleButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =match_parent
机器人:背景=@绘制/ delete_button>< /按钮>< / LinearLayout中>
我如何访问按钮:我的类扩展ListActivity内(ID deleteButton)?扩展listactivity类有一个单独的布局(main.xml中假设)。如果我做的setContentView(主)扩展listactivity类里面,findViewById(R.id.deleteButton)将返回null。
编辑2:
这是我的类扩展ListActivity。如果我把findViewById(R.id.deleteButton)中的setContentView后返回null。
公共类PlayerSelection扩展ListActivity {
ListView控件列表;
ArrayList的< PlayerField> textviews = NULL;
PlayerFieldsAdapter适配器= NULL; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.player_selection_page);
清单= getListView(); textviews =新的ArrayList< PlayerField>();
的for(int i = 0; I< GlobalVariables.getPlayers();我++){
textviews.add(ⅰ,新PlayerField());
}
适配器=新PlayerFieldsAdapter(这一点,R.layout.list_item,textviews); ViewGroup中头=(ViewGroup中)getLayoutInflater()膨胀(R.layout.list_header,NULL);
键返回按钮= NULL;
的for(int i = 0; I< header.getChildCount();我++){
视图V = header.getChildAt(I)
如果(V的instanceof按钮){
后退按钮=(按钮)V;
打破;
}
}
backButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
意图optionsIntent =新意图(PlayerSelection.this,OptionsScreen.class);
startActivity(optionsIntent);
}
});
list.addHeaderView(头);
list.setAdapter(适配器);
}
简单的方法来实现这一目标是创建 CustomAdapter
类中的字段:
私人OnClickListener监听器;
然后指定此字段的setter方法。下一步将是指定活动的内部监听器
类这样的:
CustomAdapter适配器=(CustomAdapter)getListView()getAdapter()。
adapter.setListener(小于你的听者GT的实施;);
那么你的 CustomAdapter
的 getView()
方法内你这个监听器设置为您的按钮:
按钮BTN =(按钮)convertView.findViewById(R.id.btn);
btn.setOnClickListener(监听);
希望这有助于。
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);
}
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.
这篇关于访问查看从另一个布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!