Using the same views multiple times in a layout

I have an Activity that also has a ListView in it. I load some EditText boxes, buttons, etc. In ListView in my getView method.

 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 also add Button to the same List . Basically, I just load a specific layout file into a specific row based on the index of the ListView row. The button has the identifier button_title - defined in my xml below.

  convertView = mInflater.inflate(R.layout.next, null); *//inside getView method* 

Contents 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 several instances of the same button in this action, I cannot assign unique identifiers to each of these buttons. All buttons technically now contain the same identifier.

Is there a way I can assign different identifiers to each of these buttons? Program?

+4
source share
2 answers

You need to use findViewById on the parent button layout to change the identifier on a specific button.

Egs:

 convertView = mInflater.inflate(R.layout.next, null); Button buttonIWantToChangeID = convertView.findViewById(R.whatever.Button_title); buttonIWantToChangeID.setId(1000+listPosition); 

Note that findViewById is used in convertView ...

+2
source

Here's how you can change the view id

 Button button = context.findViewById(R.whatever.button); button.setId();// <-- put the new id as the param 

NOTE: make sure you do not have conflicting identifiers!

+1
source

Source: https://habr.com/ru/post/1415902/


All Articles