package com.shinado.Schedule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class SetClass extends Activity{
//ListView三基友
private List<Map<String, Object>> list;
private SimpleAdapter adapter ;
private ListView detailView;
private String theclass;//某节课的标识,用于修改数据库,如"1-2"
private String theweek;//星期的标识,用于修改数据库,如"Mon"
private String className = "";//课名
private String classPlace = "";//上课地点
private int weekTime = 0;//上课的周次
private ScheduleDAO sd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setclasslayout);
detailView = (ListView) findViewById(R.id.listview3);
Button doneBt = (Button) findViewById(R.id.donebutton);
Button cancelBt = (Button) findViewById(R.id.cancelbutton);
setTitle("设定课名");
/*
* 获取从SetDay传来的参数,并进行转换
*/
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
//转换课时的值,如0 -> "1-2"
theclass = TipUtil.getTheClass(bundle.getInt("theclass"));
int w = bundle.getInt("theweek");
//转换星期的值,如0 -> "Mon"
theweek = TipUtil.getTheWeek(w);
String information = "";
sd = new ScheduleDAO(this);
/*
* select * from Schedule where Class = theclass
* 返回Class为theclass的每一列
* 当theclass为"1-2"时,就返回每一天的1-2节课的上课信息
*/
Cursor c = sd.getValue("Class", theclass);
if(c.moveToNext())
{
//获取某一天的某节课的上课信息
information = c.getString(w);
}
list = new ArrayList<Map<String, Object>>();
adapter = new SimpleAdapter(this,getData(information),R.layout.setclasslist,
new String[]{"tip","editcontent"},
new int[]{R.id.tip,R.id.editcontent});
detailView.setAdapter(adapter);
MyListener ml = new MyListener();
detailView.setOnItemClickListener(ml);
MyButtonListener bl = new MyButtonListener();
doneBt.setOnClickListener(bl);
cancelBt.setOnClickListener(bl);
}
@Override
public void onDestroy()
{
super.onDestroy();
sd.closeDB();
}
private List<Map<String, Object>> getData(String information) {
SClass sc = SClass.getInformation(information);
className = sc.getTheclass();
classPlace = sc.getTheplace();
weekTime = sc.getWeekTime();
Map<String, Object> map = new HashMap<String, Object>();
map.put("tip", "课名");
map.put("editcontent", " " className);
list.add(map);
map = new HashMap<String, Object>();
map.put("tip", "地点");
map.put("editcontent", " " classPlace);
list.add(map);
map = new HashMap<String, Object>();
map.put("tip", "周次");
map.put("editcontent", " " SClass.getWeek(weekTime));
list.add(map);
return list;
}
class MyListener implements AdapterView.OnItemClickListener
{
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
switch(position)
{
case 0:
showClassNameDialog(position);
break;
case 1:
showClassPlaceDialog(position);
break;
case 2:
showWeekPickerDialog(position);
break;
}
}
}
private void showClassNameDialog(final int position)
{
final EditText editText = new EditText(SetClass.this);
new AlertDialog.Builder(SetClass.this)
.setTitle("请输入课名")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(editText)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
className = " " editText.getText().toString();
updateListView(new String[]{"tip", "editcontent"}, new String[]{"课名", className}, position);
}
})
.setNegativeButton("取消", null)
.show();
}
private void showClassPlaceDialog(final int position)
{
final EditText editText = new EditText(SetClass.this);
new AlertDialog.Builder(SetClass.this)
.setTitle("请输入地点")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(editText)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
classPlace = " " editText.getText().toString();
//更新列表的显示
updateListView(new String[]{"tip", "editcontent"}, new String[]{"地点", classPlace}, position);
}
})
.setNegativeButton("取消", null)
.show();
}
private void showWeekPickerDialog(final int position)
{
new AlertDialog.Builder(SetClass.this)
.setTitle("请选择周次")
.setSingleChoiceItems(new String[] {"单周","双周","全部","选择"}, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = "";
switch(which)
{
case 0:
value = " 单周";
weekTime = 1;
break;
case 1:
value = " 双周";
weekTime = 2;
break;
case 2:
value = " 全部";
weekTime = 0;
break;
case 3:
//选择开始周次和结束周次
showWeekDetailPickerDialog(position);
break;
}
updateListView(new String[]{"tip", "editcontent"}, new String[]{"周次", value}, position);
dialog.dismiss();
}
}
)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();
}
private void showWeekDetailPickerDialog(final int position)
{
final HashSet<Integer> choice = new HashSet<Integer>();
final String[] values = {"1","2","3","4","5","6","7","8","9","10","11","12",
"13","14","15","16","17","18","19","20","21","22","23","24","25"};
new AlertDialog.Builder(SetClass.this)
.setTitle("请选择选择开始周次和结束周次")
.setMultiChoiceItems(values, null, new OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked)
{
choice.add(which 1);
}
else
{
choice.remove(which 1);
}
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(choice.size() == 2)
{
//选中2个
Object object[] = choice.toArray();
//用于显示
String value = Math.min((Integer)object[0],(Integer)object[1]) "-" Math.max((Integer)object[0],(Integer)object[1]);
updateListView(new String[]{"tip", "editcontent"}, new String[]{"周次", value}, position);
//用于存入数据库
weekTime = Math.min((Integer)object[0],(Integer)object[1])*1000 Math.max((Integer)object[0],(Integer)object[1]);
dialog.dismiss();
}
else
{
new AlertDialog.Builder(SetClass.this)
.setTitle("注意")
.setMessage("请选择两个选项")
.setPositiveButton("确定",null)
.show();
}
choice.clear();
}
})
.setNegativeButton("取消", null).show();
}
class MyButtonListener implements Button.OnClickListener
{
public void onClick(View arg0) {
int source = arg0.getId();
switch (source)
{
case R.id.donebutton:
//按下确定键,在数据库中保存信息
String message = SClass.getInformationBack(new SClass(className,classPlace,weekTime));
sd.updateValue(theweek, theclass, message);
//回送信息给SetDay
Intent intent = SetClass.this.getIntent();
intent.putExtra("information", message);
SetClass.this.setResult(1, intent);
intent.setClass(SetClass.this,SetDay.class);
SetClass.this.finish();
break;
case R.id.cancelbutton:
SetClass.this.finish();
break;
}
}
}
private void updateListView(String[] key, String[] value, int position)
{
Map<String, Object> map = new HashMap<String, Object>();
for(int i=0; i<key.length; i )
{
map.put(key[i], value[i]);
}
list.set(position, map);
adapter.notifyDataSetChanged();
}
}
评论