第四周星期三每日总结
今日学习了Android Studio的数据库SQLite的查询操作,用以完善每日打卡app的查看打卡记录功能。查询功能的实质是利用
Cursor cursor = db.rawQuery("select * from wm where biao = ? ", new String[]{n_search});语句寻找表中与之匹配的记录。
cursor.moveToNext()语句实现光标的下移操作。
cursor.getString(i)语句得到表中第i-1列的数据
最后将得到的数据显示在Textview中。
查询部分代码:
activity_revise.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".revise"> 8 9 <LinearLayout 10 android:id="@+id/linearLayout3" 11 android:layout_width="0dp" 12 android:layout_height="0dp" 13 android:orientation="vertical" 14 app:layout_constraintBottom_toBottomOf="parent" 15 app:layout_constraintEnd_toEndOf="parent" 16 app:layout_constraintStart_toStartOf="parent" 17 app:layout_constraintTop_toTopOf="parent"> 18 <!-- 用户名部分 --> 19 <TextView 20 android:layout_width="fill_parent" 21 android:layout_height="35dp" 22 android:text="请输入打卡关键字进行查询:" /> 23 24 <EditText 25 android:id="@+id/search" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:text="" /> 29 <LinearLayout 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:orientation="horizontal"> 33 <Button 34 android:id="@+id/revis" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_weight="1" 38 android:layout_margin="5dp" 39 android:text="查询" 40 /> 41 <Button 42 android:id="@+id/index" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:layout_weight="1" 46 android:layout_margin="5dp" 47 android:text="首页" 48 /> 49 </LinearLayout> 50 <LinearLayout 51 android:layout_width="match_parent" 52 android:layout_height="wrap_content" 53 android:orientation="horizontal" 54 > 55 <TextView 56 android:layout_width="wrap_content" 57 android:layout_height="wrap_content" 58 android:layout_weight="1" 59 android:id="@+id/tv_show" 60 android:textSize="20sp" 61 android:gravity="center_horizontal"/> 62 <TextView 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" 65 android:layout_weight="1" 66 android:id="@+id/tv_showAge" 67 android:textSize="20sp" 68 android:gravity="center_horizontal" 69 /> 70 </LinearLayout> 71 </LinearLayout> 72 73 </androidx.constraintlayout.widget.ConstraintLayout>
revise.java
1 package com.example.null001; 2 import android.content.Intent; 3 import android.database.Cursor; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.EditText; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 12 import androidx.appcompat.app.AppCompatActivity; 13 14 public class revise extends AppCompatActivity { 15 final Mysql dbHelper = new Mysql(this,"wm",null,1); 16 private Button searchButton, indexButton; 17 private EditText search; 18 private TextView tv_show, tv_showAge; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_revise); 24 searchButton = findViewById(R.id.revis); 25 indexButton = findViewById(R.id.index); 26 search = findViewById(R.id.search); 27 tv_show = findViewById(R.id.tv_show); 28 tv_showAge = findViewById(R.id.tv_showAge); 29 searchButton.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View v) { 32 SQLiteDatabase db = dbHelper.getWritableDatabase(); 33 String n_search = search.getText().toString(); 34 tv_show.setText(null); 35 if (n_search.equals("")) { 36 myShow(); 37 38 db.close(); 39 } else { 40 tv_show.setText("关键字"); 41 tv_showAge.setText("内容"); 42 Cursor cursor = db.rawQuery("select * from wm where biao = ? ", new String[]{n_search}); 43 44 while (cursor.moveToNext()) { 45 String newbiao = cursor.getString(3); 46 String newnei = cursor.getString(4); 47 tv_show.setText(tv_show.getText() + "\n" + newbiao); 48 tv_showAge.setText(tv_showAge.getText() + "\n" + newnei); 49 } 50 51 cursor.close(); 52 db.close(); 53 } 54 } 55 }); 56 indexButton.setOnClickListener(new View.OnClickListener() { 57 @Override 58 public void onClick(View v) { 59 60 Intent intent = new Intent(); 61 intent.setClass(revise.this,Welcome.class); //跳转到注册页面 62 startActivity(intent); 63 Toast.makeText(revise.this,"返回首页!",Toast.LENGTH_SHORT).show(); 64 } 65 }); 66 } 67 public void myShow(){ 68 SQLiteDatabase db = dbHelper.getReadableDatabase(); 69 tv_show.setText("关键字"); 70 tv_showAge.setText("内容"); 71 Cursor cursor = db.rawQuery("select * from wm", null); 72 73 while (cursor.moveToNext()) { 74 String newName = cursor.getString(3); 75 String newAge = cursor.getString(4); 76 tv_show.setText(tv_show.getText() + "\n" + newName); 77 tv_showAge.setText(tv_showAge.getText() + "\n" + newAge); 78 } 79 cursor.close(); 80 } 81 }
浙公网安备 33010602011771号