mardi 12 mai 2015

Intent is not working in fragment

I have the below code :

public class HomeFragment extends Fragment {
    private List<RowItem> rowItems;

    private static Integer[] images = { R.drawable.red, R.drawable.spidy,
            R.drawable.prisoners, R.drawable.red, R.drawable.spidy };

    public HomeFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);
        ListView lv = (ListView) rootView.findViewById(R.id.myList);
        rowItems = new ArrayList<RowItem>();

        String[] titles = { "Movie1", "Movie2", "Movie3", "Movie4", "Movie5" };
        String[] descriptions = { "First Movie", "Second movie", "Third Movie",
                "Fourth Movie", "Fifth Movie" };
        // Populate the List
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
            rowItems.add(item);
        }

        // Set the adapter on the ListView
        LazyAdapter adapter = new LazyAdapter(getActivity()
                .getApplicationContext(), R.layout.list_row, rowItems);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
                Intent myIntent = new Intent(getActivity(), News.class);
                getActivity().startActivity(myIntent);

                // startActivity(in);
            }
        });
        lv.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        return rootView;
    }    
}

I have written Toast ans startIntent on item click, but it is not working. Neither toast is display nor new activity is starting. I have also tried onItemSelection method but it is not working. How can i used this in this Fragment class?

Aucun commentaire:

Enregistrer un commentaire