Get click id in the listview of the previous fragment

0

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

}

    
asked by anonymous 11.06.2014 / 13:36

1 answer

1

Since you are instantiating Fragment just before adding it to the layout, you can pass the position by Argument , and it is available anywhere in the next Fragment . I have commented on the changes I made to your code.

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

        // Codigo e mais codigo...

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

            // Guardar o id, assim quando o item for clicado é possivel recuperá-lo
            listaEtapa.put("id", buscarEtapas.get(i).getId());

            aList.add(listEtapa);
        }

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

                        Bundle args = new Bundle();

                        HashMap<String, String> item = (HashMap<String, String>) parent.getItemAtPosition(position); // Recupero o item clicado
                        args.set("ID", item.get("id")); // Recupero o id do item clicado e passo como Arguments para o novo Fragment

                        fragment1.setArguments(args);

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

                        Bundle args = new Bundle();

                        HashMap<String, String> item = (HashMap<String, String>) parent.getItemAtPosition(position); // Recupero o item clicado
                        args.set("ID", item.get("id")); // Recupero o id do item clicado e passo como Arguments para o novo Fragment

                        fragment2.setArguments(args);

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

        return(view);
    }
}

In the Fragment that will be added, just recover the arguments and retrieve the id of the item to be detailed.

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

        // Recupero o ID passado como argumento do Fragment anterior
        Integer id = getArguments().getInt("id");

        BD bd = new BD(getActivity());
        Caminhos buscar = bd.buscarCaminho(id);

        // Popular os dados do caminho no layout deste fragment

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

        return(view);
    }
}
    
11.06.2014 / 14:00