How to send data from an Activity to a Fragment

0

I'm trying to send my data from Activity CorridaEsteiraActivity to Fragment FragmentCorrida and save them to listview , but I did not succeed in my attempts, below follow the two classes:

public class CorridaEsteiraActivity extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_corrida_esteira);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Corrida na Esteira");

    TextView dataAtual = (TextView) findViewById(R.id.txtDataatual);
    long date = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String dateString = sdf.format(date);
    dataAtual.setText(dateString);

    final EditText edtTempo = (EditText)findViewById(R.id.edtTempo);
    final EditText edtDistancia = (EditText)findViewById(R.id.edtDistancia);
    final TextView txtMedia = (TextView)findViewById(R.id.txtMedia);
    final TextView txtCalorias = (TextView)findViewById(R.id.txtCalorias);
    final TextView txtCalcular = (TextView)findViewById(R.id.txtCalcular);

    txtCalcular.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(CorridaEsteiraActivity.this, "Clicou ", Toast.LENGTH_LONG).show();

            Double tempo = Double.valueOf(edtTempo.getText().toString());
            Double distancia = Double.valueOf(edtDistancia.getText().toString());

            Double media = distancia/(tempo/60);

            String mediat = String.valueOf(media);
            txtMedia.setText(mediat);
        }
    });

    Button btnSalvar = (Button)findViewById(R.id.btnSalvar);
    btnSalvar.setOnClickListener(new View.OnClickListener() {

        String[] dadosEsteira;
        @Override
        public void onClick(View view) {
            dadosEsteira[0] = String.valueOf((EditText)findViewById(R.id.edtTempo));
            dadosEsteira[1] = String.valueOf((EditText)findViewById(R.id.edtDistancia));
            dadosEsteira[2] = String.valueOf((TextView)findViewById(R.id.txtMedia));
            dadosEsteira[3] = String.valueOf((TextView)findViewById(R.id.txtCalorias));
            dadosEsteira[4] = String.valueOf((TextView)findViewById(R.id.txtDataatual));

            Toast.makeText(CorridaEsteiraActivity.this, dadosEsteira[0], Toast.LENGTH_LONG).show();
        }
    });
}
}

public class FragmentCorrida extends Fragment {

public static FragmentCorrida newInstance() {
    FragmentCorrida fragment = new FragmentCorrida();

    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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

    String name=this.getArguments().getString("NAME_KEY").toString();

    ImageButton btnCorridaGps = (ImageButton)view.findViewById(R.id.btnCorridaGps);
    btnCorridaGps.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent it_corridagps = new Intent(getContext(), CorridaGPSActivity.class);
            startActivity(it_corridagps);
        }
    });

    ImageButton btnCorridaEsteira = (ImageButton)view.findViewById(R.id.btnCorridaEsteira);
    btnCorridaEsteira.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent it_corridaesteira = new Intent(getContext(), CorridaEsteiraActivity.class);
            startActivity(it_corridaesteira);
        }
    });

    String[] corridas = {name};

    ListView listaTreinos = (ListView) view.findViewById(listaCorridas);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, corridas);
    listaTreinos.setAdapter(adapter);

    return view;
}
}

I'm trying to use the code below, but returns that the variables are null:

From activity:

Bundle bundleObject = new Bundle();
bundleObject.putString("data", " Send From Activity");
/*set Fragmentclass Arguments*/
Fragment fragmentobject = new Fragment();
fragmentobject .setArguments(bundleObject);
From Fragment You get this way:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String stringText = getArguments().getString("data");    
return inflater.inflate(R.layout.fragment, container, false);
    
asked by anonymous 01.11.2017 / 12:24

0 answers