Onlistitemclick not working on Listfragment

Asked

Viewed 42 times

0

The Toast of the onListItemClick method never appears, someone knows what I did wrong?
Code:

public class TiposFragment extends ListFragment  {
private ArrayList<Tipo> tipos;
private TipoAdapter tipoAdapter;
private int selecionado;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    try {
        Bundle bundle = getArguments();
        if (bundle != null) {
            tipos = bundle.getParcelableArrayList("tipos");
        }


    } catch (Exception e) {
        Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
    }

    if (tipos != null) {
        try {
            tipoAdapter = new TipoAdapter(getContext(), tipos);
            setListAdapter(tipoAdapter);

            ListView listaTipos = getListView();
            listaTipos.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
           // Toast.makeText(getContext(),String.valueOf(listaTipos.getCount()),Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
        }

    }

}

@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
    super.onListItemClick(lv, v, position, id);
    selecionado = tipoAdapter.getItem(position).tipCod;
    Toast.makeText(getContext(), String.valueOf(selecionado), Toast.LENGTH_SHORT).show();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tipo_trilha, container, false);
    return view;
}

2 answers

0

Solved! just add the line android:focusable="false" in the xml of Fragment

0

Try to do it that way:

public class TiposFragment extends ListFragment  {
    private ArrayList<Tipo> tipos;
    private TipoAdapter tipoAdapter;
    private int selecionado;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        try {
            Bundle bundle = getArguments();
            if (bundle != null) {
                tipos = bundle.getParcelableArrayList("tipos");
            }
        } catch (Exception e) {
            Log.d("TAG", e.toString());
            // desnecessário mostrar o erro no app, use log para ver no console
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tipo_trilha,     container, false);
        // se voce passou o parametro corretamente para o bundle,
        // dificilmente irá precisar de um bloco try ... catch
        // se precisar, é só implementar ai de novo
        tipoAdapter = new TipoAdapter(getContext(), tipos);
        setListAdapter(tipoAdapter);

        ListView listaTipos = getListView();
        listaTipos.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listaTipos.setOnListItemClick(this);
        // muita atenção nessa linha, eu prefiro montar os meus proprios adapters.
        // por isso vc precisa verificar corretamente isso, mas o que deve estar faltando é isso
        // é através desse comando que o listview sabe onde 
        // procurar o listener do click, 
        // sem isso, ele nunca vai ser disparado
        return view;
    }

    @Override
    public void onListItemClick(ListView lv, View v, int position, long id) {
        super.onListItemClick(lv, v, position, id);
        selecionado = tipoAdapter.getItem(position).tipCod;
        Toast.makeText(getContext(), String.valueOf(selecionado),    Toast.LENGTH_SHORT).show();
    }
}

try it this way, if you get in trouble, let me know ...

  • It didn’t work out, but I’ve never seen set the system for onListItemClick

  • a question, Voce even need to instantiate un Listfragment, or it can be any other type?

  • It may be any type I think, but it has to be Fragment

  • then a guy, mounts your Adapter and passes it to a normal listview directly in a Fragment, besides being easier to implement, Oce has more control over the components. I’m running here now, but if you wait I’ll put another answer with an example, it might be?

Browser other questions tagged

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