简体   繁体   English

活动结束后通知/更新片段-Android

[英]Notify/Update Fragment after Activity finishes - Android

So I have a fragment that uses a cardAdapter. 所以我有一个使用cardAdapter的片段。 When I click on a card, it shows the details of a object called Carona(ride), which is an activity. 当我单击卡片时,它显示了名为Carona(ride)的对象的详细信息,这是一项活动。 I have the option to CANCEL my proposal in this details activity, and if I do that, I go back to the previous fragment but the cards (my recycler view) is not updated. 我可以选择取消此详细活动中的提案,如果这样做,我可以返回上一个片段,但是卡(我的回收者视图)没有更新。

So this is what I tried so far. 所以这是我到目前为止所尝试的。

My "first" fragment, with all the cards. 我的“第一个”片段,包括所有卡片。 I think the method onActivityResult is never called because I tried putting a Toast there and it never appeared. 我认为永远不会调用onActivityResult方法,因为我尝试将Toast放在那儿并且从未出现过。

public class ProposalsFragment extends Fragment {


private CaroneirosController cc;
private RecyclerView myRecView;
private CardProposalAdapter adapter;
private int MYACTIVITY_REQUEST_CODE = 101;


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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View myView;
    myView = inflater.inflate(R.layout.proposals_layout, container, false);


    cc = CaroneirosController.getInstance();

    myRecView = (RecyclerView) myView.findViewById(R.id.propostasRecycler);
    myRecView.setHasFixedSize(true);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this.getContext());
    myRecView.setLayoutManager(layoutManager);
    myRecView.setItemAnimator(new DefaultItemAnimator());
    showData();

    return myView;
}

private void showData() {
    List<Carona> c = cc.caronasOfferedBy(cc.getSessionUser());

    if(c.size() == 0){
        Toast.makeText(getContext(), "Você não tem caronas ofertadas.", Toast.LENGTH_LONG);
    } else {
        adapter = new CardProposalAdapter(c);
        myRecView.setAdapter(adapter);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if ((requestCode == 10001) && (resultCode == Activity.RESULT_OK)){
        Toast.makeText(getContext(), "Voltou", Toast.LENGTH_SHORT);
        adapter.notifyDataSetChanged();
        getFragmentManager().beginTransaction().detach(this).attach(new ProposalsFragment()).commit();
    }
}

} }

The adapter 适配器

public class CardProposalAdapter extends RecyclerView.Adapter<CardProposalAdapter.ViewHolder> {

List<Carona> caronas;
Context mContext;

public CardProposalAdapter(List<Carona> caronas){
    this.caronas = caronas;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.cards_proposal, parent, false);
    mContext = parent.getContext();
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Carona c =  caronas.get(position);
    holder.tempCarona = c;
    holder.localSaida.setText(c.getLocalSaida());
    holder.localDestino.setText(c.getDestino());
    holder.dataSaida.setText(c.getDataSaida().toString());
    holder.horaSaida.setText(c.getHoraSaida().toString());
    holder.vagas.setText(Integer.toString(c.getVagas()));
    holder.ajudaDeCusto.setText(Double.toString(c.getAjudaDeCusto()));
}

@Override
public int getItemCount() {
    return caronas.size();
}

class ViewHolder extends RecyclerView.ViewHolder{

    public View view;
    private CaroneirosController cc;

    public Carona tempCarona;
    public TextView localSaida;
    public TextView localDestino;
    public TextView dataSaida;
    public TextView horaSaida;
    public TextView vagas;
    public TextView ajudaDeCusto;

    public ViewHolder(final View myView) {
        super(myView);
        cc = CaroneirosController.getInstance();

        localSaida = (TextView) myView.findViewById(R.id.local_saida_card);
        localDestino = (TextView) myView.findViewById(R.id.local_destino_card);
        dataSaida = (TextView) myView.findViewById(R.id.data_saida_card);
        horaSaida = (TextView) myView.findViewById(R.id.horario_card);
        vagas = (TextView) myView.findViewById(R.id.vagas_card);
        ajudaDeCusto = (TextView) myView.findViewById(R.id.custo_card);

        Button cancelarPropBtn = (Button) myView.findViewById(R.id.cancelarPropostaBtn);

        cancelarPropBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cc.cancelProposal(tempCarona);
                caronas.remove(tempCarona);
                notifyDataSetChanged();
            }
        });

        myView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ProposalDetailsActivity.class);
                intent.putExtra("propostaID", tempCarona.getId());
                ((Activity) mContext).startActivityForResult(intent, 10001);
            }
        });

    }

}

} }

And the activity that changes the dataset. 以及更改数据集的活动。

public class ProposalDetailsActivity extends AppCompatActivity {
CaroneirosController cc;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    cc = CaroneirosController.getInstance();

    setContentView(R.layout.activity_proposal_details);

    Intent intent = getIntent();
    final Carona propostaCarona = cc.getCaronaByID(intent.getLongExtra("propostaID", 000));

    TextView saida = (TextView) findViewById(R.id.propostaSaida);
    TextView destino = (TextView) findViewById(R.id.propostaDestino);
    TextView data = (TextView) findViewById(R.id.propostaData);
    TextView hora = (TextView) findViewById(R.id.propostaHora);
    TextView vaga = (TextView) findViewById(R.id.propostaVaga);
    TextView custo = (TextView) findViewById(R.id.propostaCusto);

    saida.setText(propostaCarona.getLocalSaida());
    destino.setText(propostaCarona.getDestino());
    data.setText(propostaCarona.getDataSaida().toString());
    hora.setText(propostaCarona.getHoraSaida());
    vaga.setText(Integer.toString(propostaCarona.getVagas()));
    custo.setText(Double.toString(propostaCarona.getAjudaDeCusto()));

    final CardProposalAdapter ad = new CardProposalAdapter(cc.caronasOfferedBy(cc.getSessionUser()));

    Button cancelarPropBtn = (Button) findViewById(R.id.cancelarPropostaBtn);

    cancelarPropBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cc.cancelProposal(propostaCarona);
            setResult(RESULT_OK);
            setResult(Activity.RESULT_OK);
            finish();
        }
    });

}

} }

I've searched on many places here on stack, but nothing worked for me. 我在堆栈上的许多地方进行了搜索,但没有任何结果适合我。

Did you override onActivityResult in your mainActivity? 您是否在mainActivity中覆盖了onActivityResult If yes, make sure to call super.onActivityResult in that method to get the call in the fragment also. 如果是,请确保在该方法中调用super.onActivityResult ,以也在片段中获取该调用。

Change context.startActivityForResult() in the fragment with just startActivityForResult() . 仅使用startActivityForResult()更改片段中的context.startActivityForResult() startActivityForResult()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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