package com.haya.tabulae;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class ListActivity extends Activity {
	
	ListView list;
	Spinner marketSelector;
	TextView price;
	
	// Mock
	ArrayList<String> items = new ArrayList<String>();
	ArrayAdapter<String> adapterList;
	
	ArrayList<String> markets = new ArrayList<String>();
	ArrayAdapter<String> adapterSpinner;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		
		init();
		mock();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_add) {
			addItemDialog();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	
	private void addItemDialog() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		AlertDialog dialog;
		builder.setTitle(getResources().getText(R.string.newItem));

		final EditText input = new EditText(this);
		input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
		input.requestFocus();
		builder.setView(input);

		// OK
		builder.setPositiveButton(getResources().getText(R.string.dialog_ok), new DialogInterface.OnClickListener() { 
		    @Override
		    public void onClick(DialogInterface dialog, int which) {
		    	String itemName = input.getText().toString().trim();
		    	if ( !itemName.isEmpty() ) {		    		
		    		addItem(itemName);
		    	}
		    }
		});
		
		// CANCEL
		builder.setNegativeButton(getResources().getText(R.string.dialog_cancel), new DialogInterface.OnClickListener() {
		    @Override
		    public void onClick(DialogInterface dialog, int which) {
		        dialog.cancel();
		    }
		});

		dialog = builder.create();
		dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		dialog.show();
	}
	
	private void addItem(String item) {
		items.add(item);
		adapterList.notifyDataSetChanged();
	}
	
	private void init() {
		
		list = (ListView) findViewById(android.R.id.list);
		marketSelector = (Spinner) findViewById(R.id.marketSelector);
		price = (TextView) findViewById(R.id.price);
	}
	
	private void mock() {
		
		// LISTVIEW
		items.add("Tomate");
		items.add("Pan");
		items.add("Detergente");
		
		adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
		list.setAdapter(adapterList);
		
		// SPINNER
		markets.add("Mercadona");
		markets.add("Aldi");
		markets.add("Super Sol");

		adapterSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, markets);				
		marketSelector.setAdapter(adapterSpinner);
		
		// TEXTVIEW
		price.setText("23.42");
		
	}
}

@@ -212,24 +212,50 @@
 InputType;%0D%0A
+import android.util.Log;%0D%0A
 import andro
@@ -269,16 +269,16 @@
 .Menu;%0D%0A
-
 import a
@@ -300,16 +300,43 @@
 uItem;%0D%0A
+import android.view.View;%0D%0A
 import a
@@ -376,16 +376,108 @@
 arams;%0D%0A
+import android.widget.AdapterView;%0D%0Aimport android.widget.AdapterView.OnItemClickListener;%0D%0A
 import a
@@ -635,16 +635,46 @@
 xtView;%0D
+%0Aimport android.widget.Toast;%0D
 %0A%0D%0Apubli
@@ -1594,16 +1594,624 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09private void init() %7B%0D%0A%09%09%0D%0A%09%09list = (ListView) findViewById(android.R.id.list);%0D%0A%09%09marketSelector = (Spinner) findViewById(R.id.marketSelector);%0D%0A%09%09price = (TextView) findViewById(R.id.price);%0D%0A%09%09%0D%0A%09%09list.setOnItemClickListener(new OnItemClickListener() %7B%0D%0A%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onItemClick(AdapterView%3C?%3E arg0, View arg1, int position, long id) %7B%0D%0A%09%09%09%09Log.d(%22Tabulae%22, %22Deleting element %22 + position);%0D%0A%09%09%09%09Toast.makeText(getApplicationContext(), items.get(position) + %22 picked!%22, Toast.LENGTH_SHORT).show();%0D%0A%09%09%09%09items.remove(position);%0D%0A%09%09%09%09adapterList.notifyDataSetChanged();%0D%0A%09%09%09%7D%0D%0A%09%09%7D);%0D%0A%09%7D%0D%0A
 %09%0D%0A%09priv
@@ -3453,204 +3453,10 @@
 %09%0D%0A%09
-private void init() %7B%0D%0A%09%09%0D%0A%09%09list = (ListView) findViewById(android.R.id.list);%0D%0A%09%09marketSelector = (Spinner) findViewById(R.id.marketSelector);%0D%0A%09%09price = (TextView) findViewById(R.id.price);%0D%0A%09%7D
+%0D%0A
 %0D%0A%09%0D

Delete elements from list