Send data from a Fragment to an Activity

0

Good Morning Personal I have a fragment containing a listview where it clears the data coming from a webservice. In the onclick method I have the following code where I pass some values to another activity according to the code below.

ltwPacote.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long 
id) {
JsonObject obj = (JsonObject) parent.getItemAtPosition(position);
String codigo = obj.get("i_cdpacote").getAsString();
String descricaopacote = obj.get("c_descricao").getAsString();
String preco = obj.get("n_precopacote").getAsString();
String regra = obj.get("c_regra").getAsString();
Intent intent = new Intent(getActivity(), VisualizaPacote.class);
intent.putExtra("i_cdpacote", codigo);
intent.putExtra("c_descricao", descricaopacote);
intent.putExtra("n_precopacote", preco);
intent.putExtra("c_regra", regra);
startActivity(intent);

In my activity, Visualize Package has the following code:

Intent intent = getIntent();
String descricao = intent.getStringExtra("descricaopacote");
String preco = intent.getStringExtra("preco");
String regra =intent.getStringExtra("regra");
TextView txtdescricaopacote = (TextView) findViewById(R.id.txtPacote);
txtdescricaopacote.setText(descricao);
TextView txtpreco = (TextView) findViewById(R.id.txtPreco);
txtpreco.setText(preco);
TextView txtregra = (TextView) findViewById(R.id.txtRegra);
txtregra.setText(regra);

It turns out that by clicking on the list item to see the details these parameters are not appearing in the Visualization activity. If anyone could guide me, thank you.

    
asked by anonymous 27.04.2017 / 13:58

2 answers

2

Extras are saved in the intent in the key / value format. So in intent.getStringExtra() you have to use the key used in intent.putExtra() .

Change the activity code as follows:

Intent intent = getIntent();
String descricao = intent.getStringExtra("c_descricao");
String preco = intent.getStringExtra("n_precopacote");
String regra =intent.getStringExtra("c_regra");
TextView txtdescricaopacote = (TextView) findViewById(R.id.txtPacote);
txtdescricaopacote.setText(descricao);
TextView txtpreco = (TextView) findViewById(R.id.txtPreco);
txtpreco.setText(preco);
TextView txtregra = (TextView) findViewById(R.id.txtRegra);
txtregra.setText(regra);
    
27.04.2017 / 14:38
0

In theory it is not a good practice to send data from a direct Fragment to another Activity because the Fragment principle is to be reusable. For example, if you wanted to implement a multi-pane layout for your Tablet app, where it would just be an Activity with 2 Fragments, that your Fragment could not be reused because it will open another Activity.

The solution to this is to create an interface in your Fragment to make MainActivity take care of the ListView click and receive the required data from the clicked item to decide what to do when the app is running on mobile (open new Activity passing the data) or tablet (pass the data to the other Fragment in the same Activity).

Regarding your specific problem, it's as they already replied, you're not using the same key in the putString and GetStringExtra methods.

    
28.04.2017 / 05:53