0

i need you help to know how can i catch the view that was clicked and do some action, i have some text view i want to call one method when they are clicked. my code, i tried to use the switch and case, but didn't work, if i use the name of de view work, but in this case i would have to create one onclick to every view and this is not that i want:

public class PerfilFragment extends Fragment {

//Firebabse
private DatabaseReference firebase;
private FirebaseAuth usuario_autenticado;
private ValueEventListener valueEventListenerContatos;

//Usuario
private Preferencias preferencias;
private String identificador_usuario_logado;
private Usuario usuario_recuperado;

//Recuperando os componentes
private TextView nome;
private TextView username;
private TextView email;
private TextView senha;
private TextView assinatura;
private String tipo_assinatura;

public PerfilFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    //Setando o ID dos campos
    View view = inflater.inflate(R.layout.fragment_perfil, container, false);
    nome = view.findViewById(R.id.perfil_nome);
    username = view.findViewById(R.id.perfil_username);
    email = view.findViewById(R.id.perfil_email);
    senha = view.findViewById(R.id.perfil_senha);
    assinatura = view.findViewById(R.id.perfil_assinatura);

    //Buscando os dados no Firebase
    usuario_autenticado = ConexaoFirebase.getFirebaseAutenticacao();
    preferencias = new Preferencias(getActivity());
    identificador_usuario_logado = preferencias.getIdentificador();
    firebase = ConexaoFirebase.getFirebase().child("usuarios").child(identificador_usuario_logado);

    valueEventListenerContatos = new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot != null) {
                usuario_recuperado = dataSnapshot.getValue(Usuario.class);
                nome.setText( usuario_recuperado.getNome());
                username.setText("Usuário: " + usuario_recuperado.getUsername());
                email.setText("E-Mail: " + usuario_recuperado.getEmail());
                senha.setText("Senha: ******");

                if(usuario_recuperado.getAssinatura() == 1){
                    tipo_assinatura = "Administrador";
                }else if(usuario_recuperado.getAssinatura() == 2) {
                    tipo_assinatura = "Assinante";
                }else if(usuario_recuperado.getAssinatura() == 3) {
                    tipo_assinatura = "Grátis";
                }

                assinatura.setText("Tipo de Usuário: " + tipo_assinatura);


            }else {
                Toast.makeText(getContext(), "Não foi Possível Obter os Dados do Perfil!", Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.perfil_nome:
                    Toast.makeText(getContext(), "Nome Clicado", Toast.LENGTH_SHORT).show();
                    atualizarNome();
                    break;
            }
        }
    });

    return view;
}

//Metodos que atualizam os dados do usuario
private void atualizarNome(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
    alertDialog.setTitle("Atualizar Nome de Usuário");
    alertDialog.setMessage("Digite o seu Nome: ");
    alertDialog.setCancelable(false);

}

@Override
public void onStart() {
    super.onStart();
    firebase.addValueEventListener(valueEventListenerContatos);
}

@Override
public void onStop() {
    super.onStop();
    firebase.removeEventListener(valueEventListenerContatos);
}
}

My XML:

<FrameLayout 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="br.com.cifrasemusica.cifrasmusica_teoriamusical.fragment.PerfilFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp"
    android:divider="?android:attr/dividerHorizontal"
    android:showDividers="middle">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

    <ImageView
        android:id="@+id/perfil_foto"
        android:layout_width="@dimen/foto_perfil_largura"
        android:layout_height="@dimen/foto_perfil_altura"
        app:srcCompat="@drawable/user" />

    <TextView
        android:id="@+id/perfil_nome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:clickable="true"/>

</LinearLayout>

    <TextView
        android:id="@+id/perfil_username"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:padding="5dp" />

    <TextView
        android:id="@+id/perfil_email"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/perfil_senha"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:padding="5dp"/>

    <TextView
        android:id="@+id/perfil_assinatura"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:padding="5dp"/>

    <ListView
        android:id="@+id/lv_perfil"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#CCCCCC"
        android:dividerHeight="1dp"
        android:layout_below="@+id/perfil_foto" />

DouglasNickson
  • 135
  • 1
  • 3
  • 15
  • `i tried to use the switch and case, but didn't work,` You didn't use it properly. Currently, you set the click listener to the Fragment: `fragment_perfil`, instead of calling it from the layout (where each View, i.e.: `perfil_nome` must be linked to the same click handler). – Phantômaxx Oct 15 '17 at 18:08
  • And how is it correctly? – DouglasNickson Oct 15 '17 at 18:10
  • but in my case, all the views was at the layout fragment_perfil.xml. – DouglasNickson Oct 15 '17 at 18:16
  • See the duplicate. No matter if your views are in a Fragment or in an Activity. XML onClick works the same way in both cases. – Phantômaxx Oct 15 '17 at 18:17

0 Answers0