Question

I am using import android.support.v4.app.ListFragment to create a ListFragment. Inside the fragment itself -- not the parent activity -- I want to implement the onItemClick method. Will someone please provide a simple example on how this might work?

public class MyListFragment extends ListFragment {

    Spannable[] stuff = {};//to be filled

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ArrayAdapter<Spannable> adapter = new ArrayAdapter<Spannable>(inflater.getContext(), android.R.layout.simple_list_item_1, stuff);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//this is red underlined by eclipse
    }
}
Was it helpful?

Solution

Have you done some research before asking that? Anyway, it can be done by overriding onListItemClick method, as following:

public class Test extends ListFragment {

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //do the stuff

    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top