Passing an Editview name to a Textview between Fragments

Asked

Viewed 140 times

0

Guys, I have a project that uses a Drawer navigation, so Mainactivity’s Mplements is already "busy", so many methods of doing this don’t work in my case. I have a mainactivity that controls the fragments I use. In one fragment I have an Edittext that will receive a name typed by the user and another that will receive this name and display it.

Fragment Editarnome

public class EditarNome extends Fragment implements View.OnClickListener{
private EditText editarNome;
private Button enviar;
String oi;
conexao con;

View rootview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.fragment_editar, container, false);
    editarNome = (EditText) rootview.getRootView().findViewById(R.id.editarNome);
    oi = editarNome.getText().toString();

    return rootview;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    con = (conexao) Inicio);
    enviar = (Button) rootview.findViewById(R.id.enviar);
    enviar.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    con.resposta(oi);
}

}

Fragment Inicio (is the Activity I used to manage the Start Fragment)

public class Inicio extends FragmentActivity implements conexao {
protected void onCreate(Bundle savedInstaceState) {
    super.onCreate(savedInstaceState);
    setContentView(R.layout.inicio);
}
@Override

public void resposta(String data) {
    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
    InicioFragment frag = (InicioFragment) manager.findFragmentById(R.id.fragment);
    frag.changeText(data);

}

}

Fragment Homefragment (where I want to display the name)

public class InicioFragment extends Fragment {
TextView text;
View rootview;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.fragment_main, container, false);

    return rootview;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    text = (TextView) rootview.findViewById(R.id.campoData2);
}

public void changeText(String data) {
    text.setText(data);
}

}

Connected interface

connected public interface { public void reply(String data); }

The error that Android says is in Fragment Editarnome, when I call Activity Inicio. I hope you can help me! Vlw

  • Add the log with the error it generated

1 answer

0

I’m going to pose some possible problems.

Fragment Editarnome

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    con = (conexao) Inicio);//um parenteses aqui abandonado, e tambem não entendi o que vc quis fazer nessa linha, vc nao instanciou a classe
    enviar = (Button) rootview.findViewById(R.id.enviar);
    enviar.setOnClickListener(this);
}

Fragment Inicio

public void resposta(String data) {
    android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
    InicioFragment frag = (InicioFragment) manager.findFragmentById(R.id.fragment);
    frag.setData(data);  
}

Fragment Iniciofragment

String data;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    text = (TextView) rootview.findViewById(R.id.campoData2);
    text.setText(data);
}
public void setData(String data){
   this.data=data;
}
  • On this line con = (connected) Start, I want the connection to "connect" to Activity Start. For example, if it was with Mainactivity I would put a getActivity() and that’s it, but the problem is that I want to connect with another Activity and I don’t know how.

  • Enter the full mainactivity code

Browser other questions tagged

You are not signed in. Login or sign up in order to post.