package in.co.sdslabs.cognizance;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.database.SQLException;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Favourites extends ListFragment {

	private String EVENTNAME = "eventname";
	private String EVENTONELINER = "eventoneliner";
	private String EVENTIMAGE = "eventimage";
	ArrayList<String> eventname;
	ArrayList<String> eventoneliner;
	FragmentManager fragmentManager;

	public Favourites() {
	}

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

		List<HashMap<String, String>> eventList = new ArrayList<HashMap<String, String>>();
		DatabaseHelper myDbHelper = new DatabaseHelper(getActivity()
				.getBaseContext());
		try {
			myDbHelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}

		try {
			myDbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
		
		View v = inflater.inflate(R.layout.eventcategoryfragment_layout,
				container, false);
		
		eventname = myDbHelper.getFavouritesName();
		int x ,y;
		for (int i = 0; i < eventname.size(); i++) {
			HashMap<String, String> hm = new HashMap<String, String>();
			Log.v("dfsd", eventname.get(i));
			hm.put("eventname", eventname.get(i));
			hm.put("eventoneliner", myDbHelper.getEventOneLiner(eventname.get(i)));
			x = myDbHelper.getImageX(eventname.get(i));
			y = myDbHelper.getImageY(eventname.get(i));
			hm.put(EVENTIMAGE,
			Integer.toString(Drawables.eventsImages[x][y]));
			eventList.add(hm);
		}
		String[] from = { EVENTNAME, EVENTONELINER, EVENTIMAGE };

		int[] to = { R.id.tv_eName, R.id.tv_eDescr, R.id.eventImage };

		// Instantiating an adapter to store each items
		// R.layout.drawer_layout defines the layout of each item

		SimpleAdapter mAdapter = new SimpleAdapter(getActivity()
				.getBaseContext(), eventList, R.layout.eventcategory_list_item,
				from, to);

		// Setting the adapter to the listView
		setListAdapter(mAdapter);
		Log.v("Day", "1");
		return v;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		setListAdapter(null);
	}

	@Override
	public void onListItemClick(ListView l, View v, int pos, long id) {
		super.onListItemClick(l, v, 1, id);
		
		Bundle data = new Bundle();
		data.putString("event", eventname.get(pos));
		Intent i = new Intent(getActivity().getBaseContext() , EventActivity.class);
		i.putExtras(data);
		startActivity(i);
	}

}
@@ -506,16 +506,45 @@
 Adapter;
+%0Aimport android.widget.Toast;
 %0A%0Apublic
@@ -958,16 +958,216 @@
 State) %7B
+%0A%09%09View v = inflater.inflate(R.layout.eventcategoryfragment_layout,%0A%09%09%09%09container, false);%0A%09%09return v;%0A%09%7D%0A%0A%09@Override%0A%09public void onResume() %7B%0A%09%09// TODO Auto-generated method stub%0A%09%09super.onResume();
 %0A%0A%09%09List
@@ -1550,157 +1550,279 @@
 %09%09%7D%0A
-%09%09
 %0A%09%09
-View v = inflater.inflate(R.layout.eventcategoryfragment_layout,%0A%09%09%09%09container, false);%0A%09%09%0A%09%09eventname = myDbHelper.getFavouritesName();
+eventname = myDbHelper.getFavouritesName();%0A%0A%09%09if (eventname.size() == 0) %7B%0A%09%09%09Toast.makeText(getActivity().getBaseContext(), %0A%09%09%09%09%09%22There are no current Favourites%22, Toast.LENGTH_SHORT).show();%0A%09%09%09getActivity().getSupportFragmentManager().popBackStack();%0A%0A%09%09%7D
 %0A%09%09int x
  ,y;
@@ -1817,18 +1817,18 @@
 %0A%09%09int x
- 
 ,
+ 
 y;%0A%09%09for
@@ -2036,17 +2036,22 @@
 eliner%22,
- 
+%0A%09%09%09%09%09
 myDbHelp
@@ -2205,20 +2205,17 @@
 NTIMAGE,
-%0A%09%09%09
+ 
 Integer.
@@ -2752,27 +2752,16 @@
 , %221%22);%0A
-%09%09return v;
 %0A%09%7D%0A%0A%09@O
@@ -2970,18 +2970,16 @@
 1, id);%0A
-%09%09
 %0A%09%09Bundl
@@ -3106,11 +3106,14 @@
 xt()
- , 
+,%0A%09%09%09%09
 Even
@@ -3171,13 +3171,14 @@
 ity(i);%0A
+%0A
 %09%7D%0A%0A%7D

Update Favourites in OnResume() in place of OnCreateView()