I have a FragmentA that loads a listview. This listview is populated with a hashmap getting the values from bd. When I click on item 1,2,3 ... from the list, it opens a FragmentB (details). How do I get the item id clicked on fragmentA with data populated in fragmentB? I want when you click on Step 1, open in the FragmentB the details of the _id 1 item of the bank.
Follow the codes of the two fragments below:
public class FragmentEtapasCaminhoFr extends Fragment {
private ListView lv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_etapascaminho1, container, false);
BD bd = new BD(getActivity());
List<Etapas> buscarEtapas = bd.buscarListaEtapas();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < buscarEtapas.size(); i++) {
HashMap<String, String> listEtapa = new HashMap<String, String>();
listEtapa.put("nomeEtapas", String.valueOf(buscarEtapas.get(i).getId())+" - "+ buscarEtapas.get(i).getNomeEtapas());
listEtapa.put("km", String.valueOf( buscarEtapas.get(i).getDistancia())+" Km");
listEtapa.put("albergues", String.valueOf(buscarEtapas.get(i).getNumeroAlbergues())+" Albergues");
aList.add(listEtapa);
}
String[] fromFr = new String []{"nomeEtapas", "km", "albergues"};
int layoutNativo = R.layout.fragment_etapascaminho;
int[] toFr = new int[]{R.id.txtNomeEtapa, R.id.txtEtapaDistancia, R.id.txtEtapaAlbergues};
lv = (ListView) view.findViewById(R.id.listViewEtapas); //os componentes que estão dentro do """layoutNativo"""
lv.setAdapter(new SimpleAdapter(getActivity(), aList, layoutNativo, fromFr, toFr)); //será inflado, q nesse caso é o "fragment_caminhos"
lv.setOnItemClickListener(new ListView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Fragment fragment1 = new FragmentCaminhosDetalhes();
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.content_frame, fragment1,"content_frame");
ft.addToBackStack("stak");
ft.commit();
break;
case 1:
Fragment fragment2 = new FragmentCaminhosDetalhes();
FragmentTransaction ft2 = getActivity().getSupportFragmentManager().beginTransaction();
ft2.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft2.replace(R.id.content_frame, fragment2,"content_frame");
ft2.addToBackStack("stak");
ft2.commit();
break;
}
//Toast.makeText(getActivity(), "O item "+(position + 1)+" \""+aList.get(position)+"\" foi clicado", Toast.LENGTH_SHORT).show();
}
});
return(view);
}
}
Below the FragmentB Code (details)
public class FragmentCaminhosDetalhes extends Fragment {
private TextView txt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detalhescaminho, container, false);
BD bd = new BD(getActivity());
Caminhos buscar = bd.buscarCaminho();
txt = (TextView) view.findViewById(R.id.textView3);
TextView txt1 = (TextView) view.findViewById(R.id.textView2);
txt.setText("Conteúdo do menu "+buscar.getId());
txt1.setText("Nome: "+buscar.getNomeCaminhos()); // PRECISO OBTER NOS TXT OS DADOS DOS
// ITENS 1,2,3 do FRAGMENT ANTERIOR
return(view);
}
}