Pass a value to a Textview that is in another Layout that is not loaded in the setContentView ()?

1

I have a problem sending text to a TextView, the problem is that the TextView to which I want to send the text is in a Layout that is not loaded in the setContentView ().

This is the nav_header_main from where I try to take the TextView:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    app:srcCompat="@drawable/ic_cuentalog"
    />

**<TextView
    android:id="@+id/ususesion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Usuario" />**
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[email protected]"
    />

And this is the view of the application, in the TextView that says user I want to show the User Name:

And this is the class from where I want to send that data, the code that:

package com.example.enriq.persistencia_en_android_enrique_espinosa;

    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;

    public class pincipal extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
        View view;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_pincipal);

            // se tiene que importar la clase v7 para
            // que no muestre error al instancear la clase Toolbar
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            //si la siguiente linea de codigo marca error es por que no ha implementado el metodo
            navigationView.setNavigationItemSelectedListener(this);

            **LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.nav_header_main, null);
            String nombreArchivo = getIntent().getExtras().getString(MainActivity.FILE_KEY);
            TextView txtusario = (TextView)view.findViewById(R.id.ususesion);
            Toast.makeText(this,"el archivo se llama: "+nombreArchivo, Toast.LENGTH_SHORT).show();

            try {
                File archivo = new File(getExternalFilesDir(null), nombreArchivo);
                ObjectInputStream input = new ObjectInputStream(new FileInputStream(archivo));
                Usuario usuario = (Usuario) input.readObject();
                input.close();
                txtusario.setText(usuario.toString());
                Toast.makeText(this,"el usario que debe aparecer es: "+usuario.toString(), Toast.LENGTH_LONG).show();

            }catch (IOException e){
                Toast.makeText(this, "Error al abrir el archivo", Toast.LENGTH_SHORT).show();
            } catch (ClassNotFoundException e) {
                Toast.makeText(this, "Error al cargar la clase Usuario", Toast.LENGTH_SHORT).show();
            }
        }**

        @Override
        public void onBackPressed(){
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if(drawer.isDrawerOpen(GravityCompat.START)){
                drawer.closeDrawer(GravityCompat.START);
            }else{
                super.onBackPressed();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu){
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        public boolean onOptionsItemSelected(MenuItem item){
            //este metodo es para obtener el id del menu de los
            //tres puntos
            int id = item.getItemId();

            if(id == R.id.exportar){
               onClickRadio(view);
            }else if(id == R.id.sesion){
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            //con este codigo recuperamos el id del item seleccionado en el
            //navigationdrawer para poder seleccionarlo o cambiar de fragmento
            int id = item.getItemId();

            if(id == R.id.iniciotap){
                //Traemos al fragmento de inicio
                /*fragment_home  home = new fragment_home();
                android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
                //vamos a sustitur el contenedor del activity main por un nuevo fragment
                manager.beginTransaction().replace(R.id.exp, home).commit();
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setSubtitle("Inicio");*/
            }else if(id == R.id.noticiastap){
                //Traemos al fragmento de noticias
               /*noticias_generales noticiasg = new noticias_generales();
                android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
                //vamos a sustitur el contenedor del activity main por un nuevo fragment
                manager.beginTransaction().replace(R.id.exp, noticiasg).commit();
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                toolbar.setSubtitle("Noticias Generales");*/
            }
            //Este codigo nos permite mostrar que menu esta seleccionado
            //para poder identificar que menu esta en uso
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }

        public void onClickRadio(View view){
            Dialogo.listaRadio(this, view).show();
        }

    }
    
asked by Kike hatake 15.01.2018 в 20:00
source

1 answer

2

Look up the reference of TextView in this way:

navigationView.getHeaderView(0).findViewById(R.id.nombrePerfil);    

complete example:

//Obtiene referencia de TextView usado como header.
TextView profileName = navigationView.getHeaderView(0).findViewById(R.id.nombrePerfil);    
//Asigna texto a TextView.
profileName.setText("Texto de prueba");
    
answered by 15.01.2018 / 21:10
source