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

时间:2016-09-21 00:58:24

标签: android android-fragments

所以我有一个使用cardAdapter的片段。当我点击卡片时,它会显示一个名为Carona(ride)的对象的详细信息,这是一项活动。我可以选择在此详细活动中取消我的提议,如果我这样做,我会回到上一个片段,但卡片(我的回收者视图)没有更新。

所以这就是我到目前为止所做的。

我的第一个"片段,所有卡片。我认为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();
    }
}

}

适配器

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

    }

}

}

更改数据集的活动。

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

}

}

我已经在堆栈上的很多地方进行了搜索,但对我来说没有任何作用。

1 个答案:

答案 0 :(得分:0)

您是否覆盖了mainActivity中的onActivityResult?如果是,请确保在该方法中调用super.onActivityResult以获取片段中的调用。

仅使用context.startActivityForResult()更改片段中的startActivityForResult()