2012-04-24 28 views
0

Iダイアログにリストを表示するフレームセグメントがあります。ダイアログでは、1つのアクティビティを開くclickListenerがあります。このアクティビティはFragmentActivityではありません。フラグメントから別のアクティビティに値を渡す

私はこの値を取得しようとしているとき、彼はnullになります。 ...

マイ断片クラス

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( "タラ")を取得しようとすると 彼のヌル来

なぜ???

答えて

0

通常、バンドルはさまざまなアクティビティ間でデータを渡すために使用します。どのタイプの値を渡すかによって異なりますが、バンドルはすべてのタイプの値を保持して新しいアクティビティに渡すことができます。あなたもandroid-using-bundle-for-sharing-variables上でより多くの情報を見つけることができますし、Passing-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"); 
関連する問題