package com.activity.chaoran;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.chaoran.component.MyAdapter;
import com.chaoran.entiry.DataGrid;
import com.activity.chaoran.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ParamActivity extends Activity {
	private ListView listView;
	private List listdata;
	private TextView tv;
	private MyAdapter mxAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_param);
		listView = (ListView) findViewById(R.id.list);
		tv = (TextView) findViewById(R.id.tvtitle);
		final Intent intent = getIntent();
		DataGrid dg = (DataGrid) intent.getSerializableExtra("dg");
		listdata = dg.getTableData();
		// SimpleAdapter mxAdapter = new SimpleAdapter(this,listdata,
		// R.layout.activity_menu,
		// new String[] {dg.getTableHead().get(0).get("columnName").toString()},
		// new int[] { R.id.title });

		mxAdapter = new MyAdapter(this, (ArrayList) listdata, dg.getTableHead()
				.get(0).get("columnName").toString());
		tv.setText(dg.getTableHead().get(0).get("columnChineseName").toString());
		listView.setAdapter(mxAdapter);
		// listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//
		// 一定要设置这个属性，否则ListView不会刷新
		OnItemClickListener lis1 = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// mxAdapter.cur_pos=arg2;
				arg1.setBackgroundColor(Color.GREEN);
				Serializable map = (Serializable) listdata.get(arg2);
				// Intent int =getIntent();
				// intent.putExtra("param",map);
				intent.putExtra("param", map);
				intent.putExtra("audioFld",
						intent.getSerializableExtra("audioFld"));
				setResult(1, intent);
				clear();
			}
		};
		listView.setOnItemClickListener(lis1);
	}

	public void onback(View v) {
		clear();
	}

	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.isTracking()
				&& !event.isCanceled()) {
			clear();
			return true;
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
			if (mxAdapter.cur_pos < listdata.size()) {
				mxAdapter.cur_pos++;
				listView.setSelection(mxAdapter.cur_pos);
				mxAdapter.notifyDataSetChanged();
			}
		}
		if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
			if (mxAdapter.cur_pos >0) {
				mxAdapter.cur_pos--;
				listView.setSelection(mxAdapter.cur_pos);
				mxAdapter.notifyDataSetChanged();
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	private void clear() {
		listView = null;
		tv = null;
		if (listdata != null) {
			listdata.clear();
			listdata = null;
			mxAdapter.clear();
			mxAdapter = null;
		}
		finish();
	}

}
