首页 > 其他分享 >3.12

3.12

时间:2024-03-12 22:44:24浏览次数:13  
标签:name show 3.12 db cursor import setText

废话不多说 上代码

DBhelper 类

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context){super(context,"Test.db",null,1);}
    //第一个参数是上下文,第二个参数是数据库名称,
    //第三个参数是CursorFactory对象,一般设置为null,第四个参数是数据库的版本
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),age INTEGER)");
    }
    //创建表 表名information 表结构 自增id,字符串姓名,int年龄

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("myDeBug","数据库版本已更新");
    }
    //数据库版本发生变化时调用
}

activity_xml

<?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:orientation="vertical"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓名"
            android:textSize="30sp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:textSize="20sp"
            android:id="@+id/name"
            />
    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄"
            android:textSize="30sp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入年龄"
            android:textSize="20sp"
            android:id="@+id/age"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"
        android:text="插入"
        android:id="@+id/btn_insert"
        android:textAllCaps="false"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="20sp"
        android:text="更新"
        android:id="@+id/btn_update"
        android:textAllCaps="false"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    android:textSize="20sp"
    android:text="查询"
    android:id="@+id/btn_search"
        android:textAllCaps="false"
    />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
    android:textSize="20sp"
    android:text="删除"
    android:id="@+id/btn_delete"
        android:textAllCaps="false"
    />


</LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tv_show"
            android:textSize="20sp"
            android:gravity="center_horizontal"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tv_showAge"
            android:textSize="20sp"
            android:gravity="center_horizontal"
            />
</LinearLayout>


</LinearLayout>

mainactivity

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class DictActivity extends AppCompatActivity {
    private Button insertButton,updateButton,searchButton,deleteButton;
    private EditText name,age;
    private TextView show,showAge;
    final DatabaseHelper dbHelper=new DatabaseHelper(DictActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dict);

        insertButton=findViewById(R.id.btn_insert);
        updateButton=findViewById(R.id.btn_update);
        searchButton=findViewById(R.id.btn_search);
        deleteButton=findViewById(R.id.btn_delete);
        name=findViewById(R.id.name);
        age=findViewById(R.id.age);
        show=findViewById(R.id.tv_show);
        showAge=findViewById(R.id.tv_showAge);



        SQLiteDatabase db=dbHelper.getReadableDatabase();

        myShow();
        

        insertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("name",name.getText().toString());
                values.put("age",age.getText().toString());
                long id =db.insert("information",null,values);
                Log.d("myDeBug","insert");

                myShow();
                

                db.close();
                name.setText(null);
                age.setText(null);
            }
        });
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("age",age.getText().toString());
                db.update("information",values,"name=?",new String[]{name.getText().toString()});
                
                    myShow();


                db.close();
                Log.d("myDebug","update");
                name.setText(null);
                age.setText(null);
            }
        });
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                String name1=name.getText().toString();
                show.setText(null);
                if(name1.equals("")){
//                    show.setText("姓名");
//                    showAge.setText("年龄");
//                    Cursor cursor = db.rawQuery("select * from information",null);
//
//                    while (cursor.moveToNext()) {
//                        String newName = cursor.getString(cursor.getColumnIndex("name"));
//                        int newAge = cursor.getInt(cursor.getColumnIndex("age"));
//                        show.setText(show.getText() + "\n" + newName);
//                        showAge.setText(showAge.getText()+"\n" + newAge);
//                    }

                    myShow();
                    
                    db.close();
                }else {
                    show.setText("姓名");
                    showAge.setText("年龄");
                    Cursor cursor = db.rawQuery("select * from information where name = ? ", new String[]{name1});

                    while (cursor.moveToNext()) {
                        String newName = cursor.getString(cursor.getColumnIndex("name"));
                        int newAge = cursor.getInt(cursor.getColumnIndex("age"));
//                        show.setText(show.getText() + "\n" + newName + "\t" + newAge);
                        show.setText(show.getText() + "\n" + newName);
                        showAge.setText(showAge.getText()+"\n" + newAge);
                    }

                    cursor.close();
                    db.close();
                    name.setText(null);
                    age.setText(null);
                }
            }
        });
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                db.delete("information","name=?",new String[]{name.getText().toString()});

                
                myShow();
             
                
                db.close();
                Log.d("myDeBug","DeleteSuccess");
                name.setText(null);
                age.setText(null);
            }
        });

    }

    public void myShow(){
        SQLiteDatabase db=dbHelper.getReadableDatabase();


        show.setText("姓名");
        showAge.setText("年龄");
        Cursor cursor = db.rawQuery("select * from information",null);

        while (cursor.moveToNext()) {
            String newName = cursor.getString(cursor.getColumnIndex("name"));
            int newAge = cursor.getInt(cursor.getColumnIndex("age"));
            show.setText(show.getText() + "\n" + newName);
            showAge.setText(showAge.getText()+"\n" + newAge);
        }
        cursor.close();
    }

 

这段代码是一个简单的Android应用程序,用于创建一个简单的字典应用。以下是对代码的简明清晰的解释:

1. 导入必要的Android库和类。
2. 创建了一个名为`DictActivity`的类,继承自`AppCompatActivity`。
3. 在`onCreate`方法中,设置了布局和初始化各种UI元素。
4. 创建了一个`DatabaseHelper`对象用于数据库操作。
5. 实现了四个按钮的点击事件,分别用于插入、更新、搜索和删除数据库中的数据。
6. 每个按钮点击事件中都会获取数据库实例,执行相应的操作,并最终调用`myShow`方法刷新显示数据。
7. `myShow`方法用于展示数据库中的所有数据。
8. 数据库操作包括插入数据、更新数据、根据姓名搜索数据和删除数据。
9. 在搜索数据时,根据输入的姓名进行搜索,显示对应的姓名和年龄。
10. 每次操作完成后都会关闭数据库连接,并清空输入框中的内容。

整体来说,这段代码实现了一个简单的字典应用,可以通过界面进行数据的插入、更新、搜索和删除操作,并实时展示数据库中的数据。

标签:name,show,3.12,db,cursor,import,setText
From: https://www.cnblogs.com/wcy1111/p/18069545

相关文章

  • 2023.03.12
     第六天 所花时间(包括上课) 3h 代码量(行) 100行 博客量(篇) 1篇 所学习到的内容 android的页面制作(下拉框,如何输入文字等)       packagecom.example.myapplication1;importandroidx.appcompat.app.AppCompatActivity;importand......
  • 计算机与人工智能学院 天梯赛选拔 2024.3.12
    L1L1-1直接输出可以,C++可能比较麻烦一点,Python和Java都有块形字符串,语法"""我是字符串!""",再不济直接PHP复制粘贴也行!由于代码过长,这里不再展示原版,不过你可以玩玩别的hhpackageGPLT_test;/***@Title:L1*@Author李浩天*@Date2024/3/128:21......
  • 24.3.12
    所花时间:上一天课,代码量:400行博客量:6了解的知识点:Androidstudio、IDEA和MySQL联合开发,安卓使用okhttp进行http请求,使用json的格式进行数据传输到IDEA的web服务器,由web服务器解析进行数据库新增操作:实现代码:安卓端写在前面:安卓开发因为模拟器和电脑并不属于同一台机器,所以......
  • 2024.3.12 zdy
    退出之前会留恋。调整一下图寻单元考一下就差不多了。考不好的话就不好说了。教材帮。加深印象。文科稍微放放。理科多练练。每天坚持背五六个单词。不如我的100/day的bcz复习做简单的题,恢复心态。不要有太大压力。刚回来差一点没关系。累了多休息。不要强迫。总有一......
  • 2024.03.12
         第5天所花时间(包括上课)2h代码量(行)140行博客量(篇)1篇学习到的知识点页面的跳转,和注册页面,登录页面的构建       packagecom.example.myapplication1;importandroidx.appcompat.app.AppCompatActivity;importandroid.an......
  • faster-fifo:C++实现的python多进程通信队列 —— 强化学习ppo算法库sample-factory的C
    项目地址:https://github.com/alex-petrenko/faster-fifo需要注意,该项目给出了两种安装方法,一种是pip从pypi官网安装,一种是从GitHub上的源码安装;经过测试发现这个项目维护程度较差,因此pypi官网上的项目比较落后,因此不建议使用pypi上的安装,而是进行源码编译安装。给出源码编......
  • Burp Suite Professional 2023.12.1.5 (macOS, Linux, Windows) - Web 应用安全、测试
    BurpSuiteProfessional2023.12.1.5(macOS,Linux,Windows)-Web应用安全、测试和扫描BurpSuiteProfessional,Test,find,andexploitvulnerabilities.请访问原文链接:BurpSuiteProfessional2024.1.1(macOS,Linux,Windows)-Web应用安全、测试和扫描,查看最新......
  • CSharp: QuestPDF 2023.12.4 in doenet 8.0
     /*ide:vs202217.5.net8.0QuestPDF23.12.4from:https://github.com/QuestPDF/QuestPDF/discussions/560*/namespaceConsoleAppFontPdfDemo{usingQuestPDF;usingQuestPDF.Fluent;usingQuestPDF.Infrastructure;usingQuest......
  • 龙蜥8.6 源码安装python3.12
    ​ 闲来无事用虚拟机安装了一下龙蜥系统。[root@localhosthome]#cat/etc/*release*AnolisOSrelease8.6NAME="AnolisOS"VERSION="8.6"ID="anolis"ID_LIKE="rhelfedoracentos"VERSION_ID="8.6"PLATFORM_ID="platform:an......
  • 2023.12.9 总结
    T1题意:一枚棋子每一步只能走到与它原位置不同行与不同列的位置,现在将其放在一个\(R\)行\(C\)列的棋盘中,此棋子走\(N\)步,经过的点构成一个排列,问有多少种不同排列?\((R,C,N\le200)\)初步思路此题是\(DP\)。设\(f_{i,j,u}\)为走了\(i\)步,在\(j,u\)位置的走法,每一......