Send data from a Fragment to an Activity

Asked

Viewed 809 times

0

Good Morning Everyone I have a Fragment containing a listview where I list 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 Visualizapackage I have 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 when clicking on the item in the list to view the details these parameters are not appearing in the Preview Toolpack. If anyone could guide me, I’d appreciate it.

  • Try it like this: String Description = Intent.getStringExtra("c_description") .....

  • Thanks for the help, it worked.

2 answers

2

Extras are stored in the Inside in key/value format.
Thus, in the intent.getStringExtra() must use the key used in the intent.putExtra().

Change the Activity code like this:

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);

0

In theory it is not a good practice to send data from one Fragment directly to another Activity, because the principle of Fragment is to be reusable. For example, if you wanted to implement a multipainel layout of your app for tablets, where it would be just an Activity with 2 Fragments, this 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 necessary data from the clicked item to decide what to do when the app is running on mobile (open new Activity passing data) or tablet (pass data to other Fragment in the same Activity).

Regarding your specific problem, it is as already answered, you are not using the same key in the putString and Getstringextra methods.

Browser other questions tagged

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