gpt4 book ai didi

java - 单击后退按钮时使用 PutExtra 发送数据

转载 作者:行者123 更新时间:2023-12-01 20:15:03 25 4
gpt4 key购买 nike

我遇到以下问题。

  • Activity 1:通过 PutExtra 将用户 ID 发送到哪里。
  • Activity 2:使用 GetExtra 获取数据。
  • 在 Activity 2 中的某个时刻,我发送到 Activity 3,然后使用 PutExtra 再次完成发送。

我想返回 Activity 2,将数据作为 PutExtra 发送。但在 Activity 2 中,您已经有一个 GetExtra 需要来自 Activity 1 的数据,因此它会给出错误。如何将此数据从 Activity 3 发送到 Activity 2,并且不与 Activity 2 冲突,因为我已经期望使用来自 Activity 1 的 GetExtra 获取数据。

注意:发送的数据始终相同。它始终是作为 PutExtra 发送并作为 GetExtra 接收的用户 ID。

编辑:

从 Activity 2 向 Activity 3 发送或给予代码

    public class PerfilEmpTab2 extends Fragment {

private RecyclerView mCardServicoList;

private String mId_Empresa = null;

private DatabaseReference mDatabaseServicos;

private boolean mProcessAddServico = false;


public PerfilEmpTab2() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_perfil_emp_tab2, container, false);

/* Recebe id de outra tela*/
mId_Empresa = getActivity().getIntent().getExtras().getString("id_empresa");

mDatabaseServicos = FirebaseDatabase.getInstance().getReference().child("Produtos_Empresas").child(mId_Empresa);

/*Recuperar REcyclerView*/
mCardServicoList = (RecyclerView) view.findViewById(R.id.cardListaServicos);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);

//mCardCategList.setHasFixedSize(true);
mCardServicoList.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
/*Fim Recycler View*/

loadServicos();


return view;
}

private void loadServicos() {

FirebaseRecyclerAdapter<CardServico_row, CardServicosViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<CardServico_row, CardServicosViewHolder>(

CardServico_row.class,
R.layout.card_servicos_row,
CardServicosViewHolder.class,
mDatabaseServicos

) {
@Override
protected void populateViewHolder(final CardServicosViewHolder viewHolder, final CardServico_row model, int position) {

final String servico_key = getRef(position).getKey();
final String nome_produto = model.getNome_produto();
final String duracao = model.getDuracao();
final String valor = model.getValor();
final String valorOld = model.getValorOld();


viewHolder.setNome_produto(model.getNome_produto());
viewHolder.setDuracao(model.getDuracao());
viewHolder.setValor(model.getValor());
viewHolder.setValorOld(model.getValorOld());


/*Clique na view*/
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intentServicoDetalhes = new Intent(getActivity(), ServicoDetalhes.class);
intentServicoDetalhes.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentServicoDetalhes.putExtra("id_empresa", mId_Empresa);
startActivity(intentServicoDetalhes);

}
});


viewHolder.mAddBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//Toast.makeText(Categorias.this, nome + post_key, Toast.LENGTH_LONG).show();
CharSequence opcoes[] = new CharSequence[] {"Editar Serviço", "Ver Detalhes"};

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//builder.setTitle("Opçoes");
//builder.setCancelable(false);
builder.setItems(opcoes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// the user clicked on colors[which]

switch (which) {
case 0:
Toast.makeText(getActivity().getApplication(), "Dados" + "-" + servico_key + "-" + nome_produto + "-" + duracao + "-" + valor + "-" + valorOld, Toast.LENGTH_LONG).show();
/*Intent criarSubC = new Intent(Categorias.this, CadastroSubCategorias.class);
criarSubC.putExtra("id_categ", post_key);
startActivity(criarSubC);*/
mProcessAddServico = false;
break;
case 1:
Toast.makeText(getActivity().getApplication(), "Dados" + "-" + servico_key + "-" + nome_produto + "-" + duracao + "-" + valor + "-" + valorOld, Toast.LENGTH_LONG).show();
mProcessAddServico = false;
break;
}


}

});
builder.show();

}
});

}
};
mCardServicoList.setAdapter(firebaseRecyclerAdapter);


}




public static class CardServicosViewHolder extends RecyclerView.ViewHolder{

View mView;

ImageButton mAddBtn;

public CardServicosViewHolder (View itemView){
super(itemView);

mView = itemView;

mAddBtn = (ImageButton) mView.findViewById(R.id.addServico_tab2);

}


public void setNome_produto(String nome_produto){

TextView card_nomeProduto = (TextView) mView.findViewById(R.id.tvNomeProduto);
card_nomeProduto.setText(nome_produto);

}

public void setDuracao(String duracao){

TextView card_duracao = (TextView) mView.findViewById(R.id.tvDuracao);
card_duracao.setText(duracao);

}

public void setValor(String valor){

TextView card_valor = (TextView) mView.findViewById(R.id.tvValor);
card_valor.setText(valor);

}

public void setValorOld(final String valorOld){

if ( valorOld != null ){


TextView card_valorOld = (TextView) mView.findViewById(R.id.tvValorOld);
card_valorOld.setText(valorOld);
card_valorOld.setPaintFlags(card_valorOld.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); // Risca o texto
//card_valorOld.setPaintFlags(card_valorOld.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG)); // Remove o Risca o texto

} else {

TextView card_valorOld = (TextView) mView.findViewById(R.id.tvValorOld);
card_valorOld.setText(valorOld);
card_valorOld.setVisibility(View.GONE);

}



}

}
}

在 Activity 3 中我得到:

mId_Empresa = getIntent().getExtras().getString("id_empresa");

最佳答案

当您启动 Activity 3 时,调用 startActivityForResult(intent, code) 而不是 startActivity(intint)。然后在 Activity 3 中覆盖 finish() 并调用 setResult(Activity.RESULT_OK, data),其中 data 是您创建的对象 new Intent() code> 并根据需要调用 putExtra data.putExtra(key, value) 。然后在 Activity 2 中重写 onActivityResult(int requestCode, int resultCode, Intent data) 来处理它。 requestCode 是您启动 Activity 所用的代码。请注意,onActivityResult 发生在 onResume 之前,因此尝试从 onActivityResult 更新 UI 可能无法按预期工作,例如通知适配器。

请参阅此文档以获取更多信息 https://developer.android.com/training/basics/intents/result.html

编辑:添加代码示例

从 Activity 2 开始 Activity 3:

static final int SERVICO_DETALHES_REQUEST = 1;  // The request code

@Override
public void onClick(View view) {
{
Intent intentServicoDetalhes = new Intent(getActivity(), ServicoDetalhes.class);
intentServicoDetalhes.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentServicoDetalhes.putExtra("id_empresa", mId_Empresa);
startActivityForResult(intentServicoDetalhes, SERVICO_DETALHES_REQUEST );
}

设置 Activity 3 的结果:

@Override    
public void finish()
{
Intent data = new Intent();
data.putExtra("id_empresa", "new_id");
setResult(Activity.RESULT_OK, data);
super.finish();
}

处理 Activity 2 的结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == SERVICO_DETALHES_REQUEST && resultCode == RESULT_OK)
String newId = data.getStringExtra("id_empresa");
}

关于java - 单击后退按钮时使用 PutExtra 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46019703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com