recover item position in listview

Asked

Viewed 252 times

0

when the person clicks on item A in the fragment categoria I would need to take it to an Activity. And when I click on item B take it to another Activity, so I can make the filters.

Here the class of items:

public class Itens {

    private int imagem;

    public Itens(int imagem) {
        this.imagem = imagem;
    }

    public int getImagem() {
        return imagem;
    }

    public void setImagem(int imagem) {
        this.imagem = imagem;
    }
}

This is the Adapter:

public class CategoriaAdapter extends ArrayAdapter<Itens> {

    private Context context;
    private ArrayList<Itens> lista;

    public CategoriaAdapter(Context context, ArrayList<Itens> lista) {
        super(context,0,lista);
        this.context = context;
        this.lista = lista;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Itens itemPosicao = this.lista.get(position);
        convertView = LayoutInflater.from(this.context).inflate(R.layout.lista_categoria,null);

        ImageView imageView = (ImageView) convertView.findViewById(R.id.image_lista_categoria);
        imageView.setImageResource(itemPosicao.getImagem());

        return convertView;
    }
}

And here the fragment:

public class PorCategoria extends Fragment {

    private ArrayAdapter<Itens> adapter;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_por_categoria, container, false);

        final ArrayList<Itens> lista = new ArrayList<Itens>();

        Itens a = new Itens(R.drawable.categoria1);
        Itens b = new Itens(R.drawable.categoria2);
        Itens c = new Itens(R.drawable.categoria3);
        Itens d = new Itens(R.drawable.categoria4);
        Itens e = new Itens(R.drawable.categoria5);

        lista.add(a);
        lista.add(b);
        lista.add(c);
        lista.add(d);
        lista.add(e);

        adapter = new CategoriaAdapter(getActivity(),lista);
        ListView listaCategoria = (ListView) view.findViewById(R.id.list_categoria);
        listaCategoria.setAdapter(adapter);

        return view;
    }
}

1 answer

1


Use the method setOnItemClickListener(). Behold:

listaCategoria.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) {

            // o int position representa a posição no qual você clicou
            Toast.makeText(getBaseContext(), "Posição: "+position, Toast.LENGTH_LONG).show();

            if(position == 0){
                // aqui você coloca seu intent para qual activity quer ir
            }
        }
});
  • thanks for the reply but I can’t give the getItemAtposition turns red

  • @Felipemoreira corrected for you. I read your question again, and I realized what I wanted. See.

  • thank you very much friend, to succeed in giving continuity to my project

  • @Felipemoreira if the answer helped you, you can validate it to help other people.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.