[英]Using same views multiple times in a layout
I have an Activity
which also has a ListView
in it. 我有一个Activity
,里面还有一个ListView
。 I'm loading some EditText
boxes, buttons, etc to that ListView
in my getView
method. 我正在我的getView
方法中为ListView
加载一些EditText
框,按钮等。
if(position==0)
{
((TextView)convertView.findViewById(R.id.textview_settings)).setText("User Information");
}
else if(position==1 || position==2 || position==5 || position==6 || position==7 || position==10 || position==13)
{
((LinearLayout)convertView.findViewById(R.id.linear_settings)).setVisibility(View.VISIBLE);
((RelativeLayout)convertView.findViewById(R.id.Relative_home)).setVisibility(View.GONE);
if(position==1 || position==5 || position==5)
((EditText)convertView.findViewById(R.id.edittext_settings)).setBackgroundResource(R.drawable.f_t_1_iphone);
else if(position==7 || position==2)
((EditText)convertView.findViewById(R.id.edittext_settings)).setBackgroundResource(R.drawable.f_t_3_iphone);
else if(position==6)
((EditText)convertView.findViewById(R.id.edittext_settings)).setBackgroundResource(R.drawable.f_t_2_repeat_iphone);
else if(position==13 || position==10)
((EditText)convertView.findViewById(R.id.edittext_settings)).setBackgroundResource(R.drawable.f_t_4_iphone);
}
And in multiple instances I'm also adding a Button
to the same List
. 在多个实例中,我也在同一个List
添加一个Button
。 Basically, I'm just loading a particular layout file to a particular row based on the row index of the ListView
. 基本上,我只是根据ListView
的行索引将特定的布局文件加载到特定的行。 The button has the id button_title
- defined in my xml below. 该按钮具有id button_title
- 在我的下面的xml中定义。
convertView = mInflater.inflate(R.layout.next, null); *//inside getView method*
Contents of next.xml: next.xml的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linear">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/frame_settings">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/Relative_home"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|left"
android:layout_margin="5dp"
android:text=""
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans" android:layout_alignParentLeft="true"/>
<ImageButton
android:id="@+id/Button_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="10dp"
android:background="@drawable/edit_button_pressed" android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_settings"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext_settings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textStyle="bold"
android:typeface="sans" android:layout_alignParentLeft="true"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Now, since I have multiple instances of the same button within this activity, I'm not able to assign unique IDs to each of these buttons. 现在,由于此活动中有多个相同按钮的实例,因此我无法为每个按钮分配唯一的ID。 All buttons technically now hold the same ID. 从技术上讲,所有按钮现在都具有相同的ID。
Is there any way in which I can assign different IDs to each of these buttons? 有什么方法可以为每个按钮分配不同的ID? Programmatically? 编程?
You need to use findViewById on the buttons parent layout in order to change the id on the specific button. 您需要在按钮父布局上使用findViewById才能更改特定按钮上的ID。
Egs: EGS:
convertView = mInflater.inflate(R.layout.next, null);
Button buttonIWantToChangeID = convertView.findViewById(R.whatever.Button_title);
buttonIWantToChangeID.setId(1000+listPosition);
Notice findViewById is used on the convertView... 注意findViewById用于convertView ...
Here is how you can change the id of a view 以下是更改视图ID的方法
Button button = context.findViewById(R.whatever.button);
button.setId();// <-- put the new id as the param
NOTE: make sure you don't have any conflicting ids!! 注意:确保没有任何冲突的ID!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.