package csu.matos.actionBar;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Fragment1 extends Fragment {
	
	OnPictureSelectedListener mListener;
	
	private String items[] = { 
			"Picture-01","Picture-02","Picture-03","Picture-04","Picture-05",
			"Picture-06","Picture-07","Picture-08","Picture-09","Picture-10",
			"Picture-11","Picture-12","Picture-13","Picture-14","Picture-15" };
	
	// ///////////////////////////////////////////////////////////////
	@Override
	public View onCreateView(LayoutInflater inflater, 
			ViewGroup container,
			Bundle savedInstanceState) {	
		
		// this view is dynamically created with code
		ListView listView = new ListView(getActivity());
		
		ArrayAdapter<String> array = new ArrayAdapter<String>(
				getActivity(),
				android.R.layout.simple_list_item_1,
				items);
		
		listView.setAdapter(array);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, 
					View v, int position, long id) {
				
				Toast.makeText(getActivity(), 
						" you picked: " + position, 1).show();
		        // Send the event and clicked item's row ID to the host activity
		        mListener.onPictureSelected(position);
			}
		});
		
		return listView;
	}//onCreateView
	
	// ///////////////////////////////////////////////////////////////
	// Main Activity must implement this interface
    public interface OnPictureSelectedListener {
        public void onPictureSelected(Integer selectedRow);
    }
    // ///////////////////////////////////////////////////////////////
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnPictureSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() 
            		+ " must implement OnArticleSelectedListener");
        }
    }//onAttach
    
}//class
