0

我有一个在对话框中显示列表的框架。在对话框中,我有一个 clickListener 打开一个 Activity。这个Activity不是FragmentActivity。

当我尝试获取此值时,他为空。帮助...

我的片段课

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

}

我的活动课

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

当我尝试获取 savedInstanceState.getInt("Cod") 他来 null

为什么???

4

1 回答 1

0

Bundle 通常用于在各种活动之间传递数据。这取决于您要传递什么类型的值,但 bundle 可以保存所有类型的值并传递给新活动。您还可以找到有关android-using-bundle-for-sharing-variablesPassing-Bundles-Around-Activities 的更多信息

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

用这一行替换上面的行

 Bundle extras = intent.getExtras();
 String texto  = extras.getInt("Cod")+"-"+extras.getString("Desc");
于 2012-04-24T03:02:32.593 回答