今天将课表 的后台数据库 完善 增删改查 功能实现
package com.example.classpai; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.Adapter; import android.widget.Button; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MyClassActivity extends AppCompatActivity implements View.OnTouchListener, GestureDetector.OnGestureListener{ private SwipeRefreshLayout swipeRefresh; private RelativeLayout mLinearLayout; private ListView mListView; private GestureDetector mGestureDetector; private MyDatabaseHelper dbHelper; private ClassClassAdapter adapter; private List<ClassClass> classList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_class); //滑动换活动 findView(); //下拉刷新 swipeRefresh = findViewById(R.id.refresh); swipeRefresh.setColorSchemeResources(R.color.refresh1,R.color.refresh3,R.color.refresh5,R.color.refresh7); swipeRefresh.setDistanceToTriggerSync(200); swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { refreshClass(); } }); //编辑课表按钮 Button add_class = findViewById(R.id.add_class); add_class.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MyClassActivity.this, AddClass_Activity.class); startActivity(intent); } }); //成绩查询按钮 Button search = findViewById(R.id.search); search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://tiedao.vatuu.com/service/login.html")); startActivity(intent); } }); //数据库读取 //数据库添加内容 dbHelper = new MyDatabaseHelper(this,"ClassDatabase.db",null,2); SQLiteDatabase db = dbHelper.getWritableDatabase(); Cursor cursor = db.query("Class",null,null,null,null,null,null); if (!cursor.moveToFirst()){ dataBace_add("1-2"); dataBace_add("3-4"); dataBace_add("5-6"); dataBace_add("7-8"); dataBace_add("9-10"); } //数据库读取 dataBace_endlook(); adapter = new ClassClassAdapter(MyClassActivity.this,R.layout.class_item,classList); ListView listView = findViewById(R.id.class_list_item); listView.setAdapter(adapter); } //左右滑动函数 //滑动n换活动**************************** //给控件注册OnTouch事件,并且允许响应长点击事件 private void findView() { //监听 mLinearLayout = findViewById(R.id.Layout_my_class); mLinearLayout.setOnTouchListener(this); mLinearLayout.setLongClickable(true); mListView = findViewById(R.id.class_list_item); mListView.setOnTouchListener(this); mListView.setLongClickable(true); mGestureDetector = new GestureDetector(this, this); } //实现手势识别接口中的方法 //用户按下屏幕就会触发: @Override public boolean onDown(MotionEvent e) { return false; } //短按触摸屏 @Override public void onShowPress(MotionEvent e) { } //点击屏幕后抬起时触发该事件 @Override public boolean onSingleTapUp(MotionEvent e) { return false; } //在屏幕上拖动控件 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } //长按触摸屏 @Override public void onLongPress(MotionEvent e) { } //滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发; //参数分别表示:按下事件、抬起事件、x方向移动速度、y方向移动速度。 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { final int FLING_MIN_DISTANCE = 100; final int FLING_MIN_VELOCITY = 200; if (-(e1.getX() - e2.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {//左滑 Intent intent = new Intent(this, MainActivity.class); startActivity(intent); //切换活动时动画 overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right); } if ((e1.getX() - e2.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY){//右滑 Intent intent = new Intent(this, AddClass_Activity.class); startActivity(intent); //切换活动时动画 overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left); } return false; } //实现OnTouchListener接口中的方法 @Override public boolean onTouch(View v, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } //下拉刷新函数 private void refreshClass(){ new Thread(new Runnable() { @Override public void run() { try{ Thread.sleep(800); }catch (InterruptedException e){ e.printStackTrace(); } runOnUiThread(new Runnable() { @Override public void run() { classList.clear(); dataBace_endlook(); adapter.notifyDataSetChanged(); swipeRefresh.setRefreshing(false); Toast.makeText(MyClassActivity.this,"刷新成功",Toast.LENGTH_SHORT).show(); } }); } }).start(); } //课表显示函数 private void dataBace_look(String time){ SQLiteDatabase db = dbHelper.getWritableDatabase(); String strings[] = new String[14]; int n = 0; Cursor cursor = db.query("Class",new String []{"name","room"},"time = ?",new String []{time},null,null,null); if (cursor.moveToFirst()){ do { strings[n] = cursor.getString(cursor.getColumnIndex("name")); n++; strings[n] = cursor.getString(cursor.getColumnIndex("room")); n++; }while (cursor.moveToNext()); cursor.close(); initRedString(time,strings); } } private void dataBace_endlook(){ dataBace_look("1-2"); dataBace_look("3-4"); dataBace_look("5-6"); dataBace_look("7-8"); dataBace_look("9-10"); } //数组添加课表信息函数 private void initRedString(String time,String[] strings){ ClassClass message = new ClassClass(time,strings[0]+ " " + strings[1],strings[2]+ " " + strings[3],strings[4]+ " " + strings[5],strings[6]+ " " + strings[7],strings[8]+ " " + strings[9],strings[10]+ " " + strings[11],strings[12]+ " " + strings[13]); classList.add(message); } //数据库添加函数 private void dataBace_add(String time){ SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("day","星期一"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); values.put("day","星期二"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); values.put("day","星期三"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); values.put("day","星期四"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); values.put("day","星期五"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); values.put("day","星期六"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); values.put("day","星期日"); values.put("time",time); values.put("name","课程名称"); values.put("room","教室"); db.insert("Class",null,values); values.clear(); } }
Addclass_Activity.java
package com.example.classpai; import android.content.ContentValues; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class AddClass_Activity extends AppCompatActivity implements View.OnTouchListener, GestureDetector.OnGestureListener{ private MyDatabaseHelper dbHelper; private RelativeLayout mLinearLayout; private GestureDetector mGestureDetector; //滑动n换活动**************************** //给控件注册OnTouch事件,并且允许响应长点击事件 private void findView() { mLinearLayout = findViewById(R.id.Layout_add_class); mLinearLayout.setOnTouchListener(this); mLinearLayout.setLongClickable(true); mGestureDetector = new GestureDetector(this, this); } //实现手势识别接口中的方法 //用户按下屏幕就会触发: @Override public boolean onDown(MotionEvent e) { return false; } //短按触摸屏 @Override public void onShowPress(MotionEvent e) { } //点击屏幕后抬起时触发该事件 @Override public boolean onSingleTapUp(MotionEvent e) { return false; } //在屏幕上拖动控件 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } //长按触摸屏 @Override public void onLongPress(MotionEvent e) { } //滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个 // ACTION_UP触发;参数分别表示:按下事件、抬起事件、x方向移动速度、y方向移动速度。 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { final int FLING_MIN_DISTANCE = 100; final int FLING_MIN_VELOCITY = 200; if (-(e1.getX() - e2.getX()) > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {//左滑右滑皆可 Intent intent = new Intent(this, MyClassActivity.class); startActivity(intent); //切换活动时动画 overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right); } return false; } //实现OnTouchListener接口中的方法 @Override public boolean onTouch(View v, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } //主程序*********************************** @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_class_); findView(); dbHelper = new MyDatabaseHelper(this,"ClassDatabase.db",null,2); Button button_add = findViewById(R.id.add); final Spinner spinner_day = findViewById(R.id.add_day); final Spinner spinner_time = findViewById(R.id.add_time); final EditText editText_name = findViewById(R.id.add_name); final EditText editText_room = findViewById(R.id.add_room); final TextView textView_spinner1 = findViewById(R.id.text_spinner1); final TextView textView_spinner2 = findViewById(R.id.text_spinner2); //获取spinner中的内容(先转到TextView中) spinner_day.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //拿到被选择项的值 String string_day = (String) spinner_day.getSelectedItem(); textView_spinner1.setText(""); textView_spinner1.setText(string_day); } public void onNothingSelected(AdapterView<?> parent) { } }); spinner_time.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { //拿到被选择项的值 String string_time = (String) spinner_time.getSelectedItem(); textView_spinner2.setText(""); textView_spinner2.setText(string_time); } public void onNothingSelected(AdapterView<?> parent) { } }); //按钮保存事件 button_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String string_day = textView_spinner1.getText().toString(); String string_time = textView_spinner2.getText().toString(); String string_name = editText_name.getText().toString(); String string_room = editText_room.getText().toString(); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name",string_name); values.put("room",string_room); //修改 db.update("Class",values,"day = ? and time = ?",new String []{string_day,string_time}); values.clear(); Toast.makeText(AddClass_Activity.this,"编辑成功",Toast.LENGTH_SHORT).show(); } }); } }