package com.example.mobapp_i_gui6;

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

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;

// http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

public class MainActivity extends ActionBarActivity {

	ExpandableListAdapter elAdapter;
    ExpandableListView elView;
    List<String> dataHeader;
    HashMap<String, List<String>> dataContent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

        fillData();
        elAdapter = new ExpandableListAdapter(this, dataHeader, dataContent);
		elView = (ExpandableListView) findViewById(R.id.lvExp);
        elView.setAdapter(elAdapter); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void addSomething(View v)
	{
		// simple example
		int pos = dataHeader.size();
        dataHeader.add("Gruppe new");
        
        List<String> G = new ArrayList<String>();
        G.add("Punkt neu");
        dataContent.put(dataHeader.get(pos), G);
		
        elAdapter.notifyDataSetChanged();
	}
	
	private void fillData() {
        dataHeader = new ArrayList<String>();
        dataContent = new HashMap<String, List<String>>();
 
        dataHeader.add("Gruppe 1");
        dataHeader.add("Gruppe 2");
        dataHeader.add("Gruppe 3");
 
        List<String> G1 = new ArrayList<String>();
        G1.add("Punkt 1.1");

        List<String> G2 = new ArrayList<String>();
        G2.add("Punkt 2.1");
        G2.add("Punkt 2.2");

        List<String> G3 = new ArrayList<String>();
        G3.add("Punkt 3.1");
        G3.add("Punkt 3.2");
        G3.add("Punkt 3.3");
  
        dataContent.put(dataHeader.get(0), G1);
        dataContent.put(dataHeader.get(1), G2);
        dataContent.put(dataHeader.get(2), G3);
    }

}
