SQLite数据库实例四

前端之家收集整理的这篇文章主要介绍了SQLite数据库实例四前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

老师用户功能实现:更改密码,更改个人信息,还有事给自己班级对于课程名称的学生录入成绩

之前一篇分享过学生的更改密码和个人信息,老师的操作与它相似,这里不重复。

对于录入成绩。需要显示的有那个学生,他的学号是什么,还有他的成绩,因为这些都是不定的,而且是从数据库中读取的,所以需要使用到一个ListVIew来显示这些数据,ListView的显示需要使用到相关条目和适配器,我的是自定义的适配器,当然大家可以使用SimpleAdapter,这样更简单,不过我只是想试一下自己能否自定义适配器使用而已。

TeacherMainActivity的代码

public class TeacherMainFrame extends Activity
{
	private Intent intent;
	private ListView teachet_listview;
	private DataBaSEOpenHelper helper;
	private Button sure_score;
	private LinearLayout showStudentPartMessage;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.teacher_main_frame);
		teachet_listview = (ListView) findViewById(R.id.teachet_listview);
		helper = new DataBaSEOpenHelper(this);
		sure_score = (Button)findViewById(R.id.sure_score);
		showStudentPartMessage = (LinearLayout)findViewById(R.id.showStudentPartMessage);
	}

	/**
	 * 统一使用这个点击事件名称管理在老师主界面的各个点击事件按,学生和管理员中也是类似
	 * @param v
	 */
	public void check_message_teacher(View v)
	{
		switch (v.getId())
		{
		case R.id.change_password_teacher://切换至更改老师密码界面
			Intent intentFromTeacher = getIntent();
			String teacherName = intentFromTeacher
					.getStringExtra("teacherName");//教师号是外码,传递之后用于查询密码表中对于教师号的密码
			intent = new Intent(TeacherMainFrame.this,TeacherChangePassword.class);
			intent.putExtra("teacherName",teacherName);
			startActivityForResult(intent,1);
			break;

		case R.id.chekSelfMessage_teacher://切换至更改老师个人信息界面
			Intent intentFromTeacher2 = getIntent();
			String teacherName2 = intentFromTeacher2
					.getStringExtra("teacherName");//教师号是外码,不能够改变,所以需要在在个人信息中使用一个TextView来显示
			intent = new Intent(TeacherMainFrame.this,TeacherMessageActivity.class);
			intent.putExtra("name",teacherName2);
			startActivity(intent);
			break;
			
		case R.id.change_green_teacher://给自己班级所教科目的学生入炉成绩
			teachet_listview.setVisibility(View.VISIBLE);
			sure_score.setVisibility(View.VISIBLE);
			showStudentPartMessage.setVisibility(View.VISIBLE);
			change_score_teacher();
			break;
		}
	}

	/**
	 * 录入成绩
	 */
	private void change_score_teacher()
	{
		Intent intentFromTeacher = getIntent();
		String teacherName = intentFromTeacher.getStringExtra("teacherName");
		sqliteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery(
				"select T_Class_name from allteachermessage where Tno=?",new String[]
				{ teacherName });//查询教师的个人信息

		if (cursor.moveToFirst())
		{
			String T_Class_name = cursor.getString(cursor
					.getColumnIndex("T_Class_name"));
			Cursor cursorscore = db
					.rawQuery(
							"select allstudentmessage.name,allstudentmessage.Sno "
									+ "from allstudentmessage,allteachermessage,dbS_C "
									+ "where allstudentmessage.[classIn]=allteachermessage.T_Class "
									+ "AND dbS_C.s_name=allstudentmessage.Sno "
									+ "AND dbS_C.c_name=allteachermessage.T_Class_name"
									+ " AND allteachermessage.[T_Class_name]=?",new String[]
							{ T_Class_name });//查询老师所教班级对于自己所教科目的学生的学号,姓名两类信息
			cursor.close();
			List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();//存放自己所教学生的信息的集合
			while (cursorscore.moveToNext())
			{
				String S_no = cursorscore.getString(cursorscore
						.getColumnIndex("allstudentmessage.Sno"));
				String name = cursorscore.getString(cursorscore
						.getColumnIndex("allstudentmessage.name"));
				Map<String,Object> item = new HashMap<String,Object>();
				item.put("name",name);//学生的姓名
				item.put("S_no",S_no);//学生的学号
				item.put("score",0 + "");//学生默认成绩0
				items.add(item);//添加到集合汇总
			}<pre name="code" class="java">
//SimpleAdapter adapter = new SimpleAdapter(this,items,R.layout.itm_teacher_for_score,new String[]{"name","S_no","score"},new int[]{R.id.teacher_Sname,R.id.teacher_Sno,R.id.teacher_score});<span style="font-family: Arial,Helvetica,sans-serif;">//使用系统的适配器</span>
TeacherBaseAdapter adapter = new TeacherBaseAdapter(this,R.layout.itm_teacher_for_score);teachet_listview.setAdapter(adapter);//绑定适配器}}/** * 点击确认按钮的时候发生的事件处理 * @param v */@SuppressLint("ShowToast")public void sure_score_teacher(View v){Intent intentFromTeacher = getIntent();String teacherName = intentFromTeacher.getStringExtra("teacherName");String class_name = null;sqliteDatabase db = helper.getReadableDatabase();Cursor cursor = db.rawQuery("select T_Class_name from allteachermessage where Tno=?",new String[]{ teacherName });if (cursor.moveToFirst()){class_name = cursor.getString(cursor.getColumnIndex("T_Class_name"));}cursor.close();
<span style="white-space:pre">		</span>//遍历listview,把其中排在item第一二三个位置的textview,edittext的值获得,从而插入数据库
for (int i = 0; i < teachet_listview.getChildCount(); i++){LinearLayout layout = (LinearLayout) teachet_listview.getChildAt(i);// 获得子item的layout// EditText et = (EditText) layout.findViewById(R.id.et);//// 从layout中获得控件,根据其id// TextView textView01 = (TextView) layout.getChildAt(0);TextView textView02 = (TextView) layout.getChildAt(1);EditText et = (EditText) layout.getChildAt(2);// String name = textView01.getText().toString();String S_no = textView02.getText().toString();String score = et.getText().toString();Log.i("TeacherMainFrame",S_no + score);db.execsql("UPDATE dbS_C SET score=? WHERE s_name=? AND c_name=?",new Object[]{ score,S_no,class_name });//更新数据库,因为学生之前选课的时候是插入数据库,这里把对应的学生的数据插入到学生--课程表中Toast.makeText(getApplicationContext(),"录入成功",0).show();sure_score.setVisibility(View.GONE);teachet_listview.setVisibility(View.GONE);showStudentPartMessage.setVisibility(View.GONE);}}protected void onActivityResult(int requestCode,int resultCode,Intent data){if (1 == requestCode)if (2 == resultCode){String teacherName = data.getStringExtra("teacherName");intent.putExtra("teacherName",teacherName);}}private AlertDialog builder = null;public boolean onKeyUp(int keyCode,KeyEvent event){if (keyCode == KeyEvent.KEYCODE_BACK){builder = new AlertDialog.Builder(TeacherMainFrame.this).setTitle("温馨提示:").setMessage("退出本程序?").setPositiveButton("确定",new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int whichButton){TeacherMainFrame.this.finish();}}).setNegativeButton("取消",int whichButton){builder.dismiss();}}).show();}return true;}}
 

教师的布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
<span style="white-space:pre">	</span><!--更改密码 -->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="@string/change_password"
            android:textColor="#000000"
            android:textSize="18sp" />

        <Button
            android:id="@+id/change_password_teacher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="check_message_teacher"
            android:text="@string/check_self_message" />
    </LinearLayout><pre name="code" class="java"><span style="white-space:pre">	</span><!--更改个人信息 -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="200dp" android:layout_height="wrap_content" android:text="@string/check_self_message_01" android:textColor="#000000" android:textSize="18sp" /> <Button android:id="@+id/chekSelfMessage_teacher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="check_message_teacher" android:text="@string/check_self_message" /> </LinearLayout>
<span style="white-space:pre">	</span><!--录入成绩 -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<span style="white-space:pre">	</span><!--显示所有的学生部分信息 -->
<TextView android:layout_width="200dp" android:layout_height="wrap_content" android:text="@string/change_green_teacher_01" android:textColor="#000000" android:textSize="18sp" />
<span style="white-space:pre">	</span><!--去而录入 -->
<Button android:id="@+id/change_green_teacher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="check_message_teacher" android:text="@string/check_self_message" /> </LinearLayout> <LinearLayout android:id="@+id/showStudentPartMessage" android:layout_gravity="left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:visibility="gone" > <TextView android:text="名字" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_marginLeft="30dp"/> <TextView android:text="学号" android:layout_width="80dp" android:layout_height="wrap_content" android:layout_marginLeft="30dp"/> <TextView android:text="成绩" android:layout_width="80dp" android:layout_height="wrap_content"/></LinearLayout> <ListView android:id="@+id/teachet_listview" android:focusable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> <Button android:id="@+id/sure_score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sure_score_teacher" android:text="确定" android:visibility="gone" /></LinearLayout>
 自定义的适配器 
 
 
public class TeacherBaseAdapter extends BaseAdapter
{
	List<Map<String,Object>> items;//绑定放入数据
	private int resouce;//绑定的数据
	private LayoutInflater inflater;//布局填充器

	public TeacherBaseAdapter (Context context,List<Map<String,Object>> items,int resouce)
	{
		super();
		this.items = items;
		this.resouce = resouce;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	/**
	 * 得到数据的总数
	 */
	public int getCount()
	{
		
		return items.size();
	}

	/**
	 * 
	 */
	public Object getItem(int position)
	{
		
		return items.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	@SuppressLint("InflateParams")
	public View getView(int position,View convertView,ViewGroup parent)
	{
		if(convertView == null)
		{
			convertView = inflater.inflate(resouce,null);
		}
		TextView name = (TextView) convertView.findViewById(R.id.teacher_Sname);
		TextView S_no = (TextView) convertView.findViewById(R.id.teacher_Sno);
		final EditText score = (EditText) convertView.findViewById(R.id.teacher_score);
		Map<String,Object> item =  items.get(position);
		name.setText((CharSequence) item.get("name"));
		S_no.setText((CharSequence) item.get("S_no"));
		score.setText((CharSequence) item.get("score"));
		return convertView;
	}
}
关于老师更改个人信息,需要我们注意的是老师身为一个用户他需要有哪一些信息,我这里定义的有教师号,名字,年龄,性别(使用RadioGroup),所在学院,所教科目,所教班级,然后所教科目使用一个单选的对话框实现,从数据库查询有哪些课程。其他的雨学生用户的操作类似。还有最后一次是关于管理员用户的。下一篇再与大家分享

猜你在找的Sqlite相关文章