简体   繁体   中英

Passing values from fragment to another activity

I Have a framgment that show a list in a Dialog. In the Dialog I have a clickListener that open one Activity. This Activity not is the FragmentActivity.

When I'm try get this values, hes come null. Help...

My Fragment class

package br.com.test.fragments;

import java.util.List;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import br.com.test.controllers.CorredoresController;
import br.com.test.R;
import br.com.test.SPAndroidConstantes;
import br.com.test.activity.ListaCorredoresActivity;
import br.com.test.adapter.ListaCorredoresAdapter;
import br.com.test.util.Utilities;
import br.com.spparser.beans.corredores.NomeCorredor;
import br.com.spparser.exception.SPException;

public class PesquisaFragment extends Fragment implements SPAndroidConstantes,     OnItemClickListener {

private ProgressDialog dialog;
private Handler handler = new Handler();
List<NomeCorredor> p;
private Exception ex;
private ListaCorredoresAdapter lcAdapter;


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //fillAdMod();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
    LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.pesquisa, container, false);
    EditText edit = (EditText) linearLayout.findViewById(R.id.editPesquisa);
    Button btnLinhas = (Button) linearLayout.findViewById(R.id.btnLinhas);
    Button btnPontos = (Button) linearLayout.findViewById(R.id.btnPontos);

    Button btnCorredor = (Button)linearLayout.findViewById(R.id.btnCorredores);
    btnCorredor.setOnClickListener(buttonCorredorListener);

    return linearLayout;
}

private void downloadInfo() {
    new Thread() {
        @Override
        public void run() {
            try {
                p = CorredoresController.getInstance().getCorredores();
                atualizaTela();
            } catch (Exception e) {
                ex = e;
                atualizaTelaExcecao();
            }
        }
    }.start();
}

/**
 * Thread para fechar dialog e apresentar informações
 */
private void atualizaTela() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            dialog.dismiss();
            preencheTela();
        }
    });
}

/**
 * Thread para fechar dialog e apresentar exceção
 */
private void atualizaTelaExcecao() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            dialog.dismiss();
            preencheTelaExcecao();
        }
    });
}

/**
 * Preenche informações da tela
 */
private void preencheTela() {
    // Criando e setando o adaptador personalizado para Lista 1
    lcAdapter = new ListaCorredoresAdapter(this.getActivity(),
            p);

    if (p.size() == 0) {
        Toast.makeText(PesquisaFragment.this.getActivity(), MSG_SEM_CORREDOR,
                MSG_TIME_MILIS).show();
        //this.getActivity().finish();
    }

    Dialog dialogCorredor = new Dialog(PesquisaFragment.this.getActivity());
    dialogCorredor.setTitle("Selecione o corredor");
    LayoutInflater li = (LayoutInflater) PesquisaFragment.this.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li.inflate(R.layout.teste_list, null, false);
    ListView lista = (ListView) v.findViewById(R.id.listTeste);
    lista.setAdapter(lcAdapter);
    dialogCorredor.setContentView(v);
    dialogCorredor.setCancelable(true);
    dialogCorredor.show();

    lista.setOnItemClickListener(this);
}

/**
 * Apresenta mensagem em caso de exceção
 */
private void preencheTelaExcecao() {
    if (ex instanceof SPException) {
        Toast.makeText(PesquisaFragment.this.getActivity(),
                ((SPException) ex).getMessage(), MSG_TIME_MILIS).show();
        //PesquisaFragment.this.getActivity().finish();
    } else {
        Toast.makeText(PesquisaFragment.this.getActivity(),
                getString(R.string.msg_ErroSistema), MSG_TIME_MILIS).show();
        //PesquisaFragment.this.getActivity().finish();
    }
}

private OnClickListener buttonCorredorListener = new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        dialog = ProgressDialog.show(PesquisaFragment.this.getActivity(), "",
                "Aguarde...", true);
        if (!Utilities.chkStatus(PesquisaFragment.this.getActivity()))
            return;

        downloadInfo();
    }
};

@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
    if (!Utilities.chkStatus(PesquisaFragment.this.getActivity()))
        return;
    NomeCorredor corredor = (NomeCorredor) parent.getAdapter().getItem(position);


    Bundle bundle = new Bundle();
    bundle.putInt("Cod", corredor.getCodigoCorredor());
    bundle.putString("Desc", corredor.getNomeCorredor());

    Intent listaCorreIntent = new Intent(PesquisaFragment.this.getActivity(), ListaCorredoresActivity.class);
    listaCorreIntent.putExtras(bundle);
    startActivity(listaCorreIntent);
}

}

My activity class

package br.com.test.activity;

import br.com.test.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class ListaCorredoresActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_corredor);
    String texto = String.valueOf(savedInstanceState.getInt("Cod")) + " - " + savedInstanceState.getString("Desc");
    Toast.makeText(this, texto, Toast.LENGTH_LONG);
}

When I try get the savedInstanceState.getInt("Cod") His come null

Why???

Bundle generally use for passing data between various Activities. It depends on you what type of values you want to pass but bundle can hold all types of values and pass to the new activity. you can also find more info on android-using-bundle-for-sharing-variables and Passing-Bundles-Around-Activities

String texto = String.valueOf(savedInstanceState.getInt("Cod")) + " - " + savedInstanceState.getString("Desc");

replace the above line with this line

 Bundle extras = intent.getExtras();
 String texto  = extras.getInt("Cod")+"-"+extras.getString("Desc");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM