实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
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();
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论