0

So I have a fragment that uses a cardAdapter. When I click on a card, it shows the details of a object called Carona(ride), which is an activity. 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.

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.

Nathan Barreto
  • 365
  • 2
  • 21

1 Answers1

0

Did you override onActivityResult in your mainActivity? If yes, make sure to call super.onActivityResult in that method to get the call in the fragment also.

Change context.startActivityForResult() in the fragment with just startActivityForResult().

masp
  • 515
  • 1
  • 5
  • 15
  • The problem is that I call the activity from the adapter (viewHolder) and I cannot use just startActivityForResult() – Nathan Barreto Sep 21 '16 at 12:53
  • That is another question. Check the answers to this http://stackoverflow.com/questions/15444375/how-to-create-interface-between-fragment-and-adapter . There many ways to deal with this. – masp Sep 21 '16 at 13:11