Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List Fragment on item click listener

I am trying to do a list on an Android´s ListFragment with an OnItemClickListener but I can´t, I click n list item and nothing happens. I am novice with fragments. Here is my code. Thank you!

public class MyFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v= inflater.inflate(R.layout.hello, container, false);



     ListView lv =(ListView) v.findViewById(android.R.id.list);


        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                // getting values from selected ListItem
                String titulo = ((TextView) v.findViewById(R.id.title))
                        .getText().toString();
                String contenido = ((TextView) v.findViewById(R.id.content))
                .getText().toString();
                String fecha = ((TextView) v.findViewById(R.id.date))
                        .getText().toString();

                // Starting new detail
                Intent in = new Intent(getActivity().getApplicationContext(),
                        NoticiasFragment.class);
                in.putExtra("title", titulo);
                in.putExtra("content", contenido);
                in.putExtra("date", fecha);
                startActivity(in);


            }


    return v;
}
like image 402
Igor Avatar asked May 29 '14 09:05

Igor


1 Answers

ListFragment has a built-in onListItemClick function to override.

You should do it like this:

public class MyFragment extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v= inflater.inflate(R.layout.hello, container, false);
        return v;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //Do your stuff..
    }

}
like image 138
tasomaniac Avatar answered Oct 02 '22 02:10

tasomaniac