package com.pjbest.custom.todolist;

import java.util.ArrayList;

import com.alibaba.fastjson.JSONObject;
import com.pjbest.custom.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

/**
 * 
 * 简单的列表适配器（列表项视图使用自定义的视图，如记事本样式的视图）. <br>
 * 
 */
public class ToDoListActivity extends Activity {

	private String TAG = "ToDoListActivity";

	private ListView myListview;
	private EditText myEditText;
	/** 自定义的TextView视图、sdk自带的简单列表项 */
	private int[] layouts = new int[] { R.layout.todolist_item, android.R.layout.simple_list_item_1 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_to_do_list);
		myListview = (ListView) findViewById(R.id.myListView);
		myEditText = (EditText) findViewById(R.id.myEditText);

		final ArrayList<String> todoItems = new ArrayList<String>();
		final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, layouts[0], todoItems);
		myListview.setAdapter(aa);

		myEditText.setOnKeyListener(new View.OnKeyListener() {

			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
						todoItems.add(0, myEditText.getText().toString());
						aa.notifyDataSetChanged();
						myEditText.setText("");
						String temp = testFastjson("vip", "pjbest");
						Log.e(TAG, temp);
						return true;
					}
				}
				return false;
			}
		});
		String temp = testFastjson("予", "盾");
		Log.e(TAG, temp);
	}

	private String testFastjson(String key, String value) {
		JSONObject json = new JSONObject();
		json.put(key, value);
		return json.toString();
	}
}
