DMC19
DMC19

Reputation: 947

No data in activity's layout while using Layout Inflater and view

I'm trying to learn Android doing some easy projects, and now I don't know how to solve this problem:

I have a main activity who has a ListView. In my onClick function, I'm sending to my second activity a parameter called "name". I catch this parameter, and I try to fill my ImageView and TextView.

This is the code of my sencond activity onCreate method:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String padre=getIntent().getStringExtra("name");

    View view = getLayoutInflater().inflate(R.layout.activity_ficha, null);
    TextView textView_ficha = (TextView) view.findViewById(R.id.DescripcionFicha);
    ImageView imageView_ficha = (ImageView)view.findViewById(R.id.FotoFicha);

    if(padre.equals("cibeles")){         
        imageView_ficha.setImageResource(R.drawable.cibeles);
        textView_ficha.setText("Cibeles");
    }
    if(padre.equals("bernabeu")){
        imageView_ficha.setImageResource( R.drawable.bernabeu);
        textView_ficha.setText("Bernabeu");
    }
    setContentView(R.layout.activity_ficha);
}

And my Layout code is:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:baselineAligned="false">

    <ImageView
        android:id="@+id/FotoFicha"
        android:layout_width="374dp"
        android:layout_height="277dp"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/DescripcionFicha"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/FotoFicha"
        android:text="TextView" />
</RelativeLayout>

The question is, why my second activity is empty? I only see an image of ic_launcher and the text "TextView".

Thanks in advance.

Upvotes: 1

Views: 62

Answers (3)

MrCurious
MrCurious

Reputation: 150

try this to get the passed string bundle

String padre = getIntent().getExtras().getString("name");

and let me know

Upvotes: 0

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

use setContentView(view);

instead of setContentView(R.layout.activity_ficha);

because by doing setContentView(R.layout.activity_ficha); you are adding an empty layout with activity but your data is inside children of view reference

View view = getLayoutInflater().inflate(R.layout.activity_ficha, null);
TextView textView_ficha = (TextView) view.findViewById(R.id.DescripcionFicha);
ImageView imageView_ficha = (ImageView)view.findViewById(R.id.FotoFicha);

Upvotes: 2

Salman thaikkat
Salman thaikkat

Reputation: 1

In your layout xml file: Remove this (app:srcCompat="@mipmap/ic_launcher") and (android:text="TextView") from your ImageView and TextView respectively

Upvotes: 0

Related Questions