0
I’m trying to send my data from Activity CorridaEsteiraActivity
to the Fragment FragmentCorrida
and save them in a listview
, however I did not succeed in my attempts, below follows 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 receive this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String stringText = getArguments().getString("data");
return inflater.inflate(R.layout.fragment, container, false);
How/where Fragment is started?
– Leonardo Lima
Pardon ignorance, I did not understand what you asked
– Bruno Mendonça
When is this Fragment displayed in your application? You use a
ViewPager
at some point? Add Fragment manually to another Activity? Or add it in XML? This information is needed to help you.– Leonardo Lima
I will describe how the system works, the user logs in and accesses his profile screen, when sliding to the side we have several Fragments, in this case the Fragmentcorrida, his function would be to store the user’s racing history and give option to 2 modules, race by stereo , and by gps, in case these two screens are activitys, the minah difficulty is to take the data of these activitys and display in the fragmentCorrida in listview.
– Bruno Mendonça