简体   繁体   中英

Inflated xml Layout with a TextView has an onClick attribute, but it does not get called.

I have an XML layout file that has a TextView within a CoordinatorLayout.

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".SpecificProgramSelectionActivity">
<TextView
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:id="@+id/saved_program"
    android:text="Empty"
    android:textAlignment="center"
    android:gravity="fill"
    android:textSize="20dp"
    android:background="@drawable/program_selection_border"
    android:textColor="@color/black"
    android:clickable="true"
    android:focusable="true"
    android:onClick="addToSavedPrograms"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"/>

And this code that inflates the layout and adds it into a Linear Layout in the activity's view.

for (PlayerWithObjectives player : players){
        name = player.getName();
        for (String objective : player.getObjectives()){
            objectives.add(objective);
        }
        nameView = inflater.inflate(R.layout.inflatable_player_name_view, null);
        ((TextView)nameView.findViewById(R.id.saved_program)).setText(name);
        ((TextView)nameView.findViewById(R.id.saved_program)).setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        ((TextView)nameView.findViewById(R.id.saved_program)).setTextSize(20);
        linearLayout.addView(nameView);

    }

(This is the activity's layout XML)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SpecificProgramSelectionActivity"
android:orientation="vertical">
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/specific_program_selection_linear_layout">

    </LinearLayout>
</ScrollView>

Everything looks fine in the app when I run it. Every inflated view shows up, the only issue is that the method that I specified in the onClick attribute for the inflated TextView does not get called. Why is that? Here is the method that is supposed to be called

public void addToSavedPrograms(View view){
    String name = (String) (((TextView)view.findViewById(R.id.saved_program)).getText());
    namesToSend.add(name);
    editor.putStringSet("Tracked Players", namesToSend);
    editor.commit();
    Toast.makeText(getApplicationContext(),name + " was saved to preferences.", Toast.LENGTH_SHORT);
}

Why doesn't the method get called? I already saw all the other threads about using setContent() and stuff but that didn't work and it was not explained that great in the answer. Any help would be greatly appreciated.

public class YourActivity extends AppCompatActivity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textview=(TextView)findViewById(R.id.saved_program);
textview.setOnClickListener(this);
}
@Override
public void onClick(View view) {

switch(view.getId()){
    case R.id.saved_program:
       //call your function here
    default:
        break;

}
}

you can do it like this.

Update: First off, thank you to everybody that commented, your help was greatly appreciated. Your no judgment assistance is an awesome and welcome change from a lot of what I have experienced and seen on in this community.

Secondly, I figured out my issue. Or got my code to work at least. Once I got a better understanding of inflating layouts I changed this line of code

nameView = inflater.inflate(R.layout.inflatable_player_name_view, null);

To

nameView = (TextView) inflater.inflate(R.layout.inflatable_add_player_name_view, linearLayout, false);

The difference is that When you inflate, it returns an object that is the same type as the top parent in the specified layout file (Which in this case is "R.layout.inflatable_player_name_view"). So I changed nameView to a TextView object and cast the returned object as a TextView. Then I specified the wanted parent layout (To get the right layoutParams) and put false so that it does not attach it automatically to that parent layout. After that I simply made the alterations to the textView that I wanted, like setting the text values and whatnot, and then manually added it to the parent linearlayout that I wanted. After doing this there was not even a need to set an onClickListener programmatically because the android:onClick method worked just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM