首页 > 技术文章 > Android-8

yunlan 2020-11-01 00:54 原文

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/ab"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#F85CDFDB"
        android:gravity="center"
        android:text="学生注册页"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名:"
                android:textSize="20sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/e_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="年龄:"
                android:textSize="20sp"
                android:textStyle="bold" />

            <EditText
                android:id="@+id/e_age"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName" />
        </TableRow>

    </TableLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="增加"
            app:backgroundTint="#EB5CDFDB" />

        <Button
            android:id="@+id/btn_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="删除"
            app:backgroundTint="#EB5CDFDB" />

        <Button
            android:id="@+id/btn_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="修改"
            app:backgroundTint="#EB5CDFDB" />

        <Button
            android:id="@+id/btn_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="查找"
            app:backgroundTint="#EB5CDFDB" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>
<?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">

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>
 1 package com.example.myapplication;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteOpenHelper;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.BaseAdapter;
 9 import android.widget.TextView;
10 
11 import androidx.annotation.Nullable;
12 
13 public class Student extends SQLiteOpenHelper {
14     private int Id;
15     private String Name,Age;
16 
17 
18     public Student(@Nullable Context context) {
19         super(context, "student.db", null, 1);
20     }
21 
22 
23     @Override
24     public void onCreate(SQLiteDatabase db) {
25         db.execSQL("CREATE TABLE stu(_id INTEGER PRIMARY KEY AUTOINCREMENT,s_name VARCHAR(20),s_age INTEGER)");
26 
27     }
28 
29     @Override
30     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
31 
32     }
33 
34 
35     public String getName() {
36         return Name;
37     }
38 
39     public void setName(String name) {
40         Name = name;
41     }
42 
43     public String getAge() {
44         return Age;
45     }
46 
47     public void setAge(String age) {
48         Age = age;
49     }
50 
51     public int getId() {
52         return Id;
53     }
54 
55     public void setId(int id) {
56         Id = id;
57     }
58 }
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

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

public class MainActivity extends AppCompatActivity {
    Student helper = new Student(this);
    private EditText e_name,e_age;
    private Button btn_1,btn_2,btn_3,btn_4;
    List<Student> show = new ArrayList<Student>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e_name = (EditText)findViewById(R.id.e_name);
        e_age = (EditText)findViewById(R.id.e_age);
        btn_1 = (Button)findViewById(R.id.btn_1);
        btn_2 = (Button)findViewById(R.id.btn_2);
        btn_3 = (Button)findViewById(R.id.btn_3);
        btn_4 = (Button)findViewById(R.id.btn_4);
        MyBaseAdapter myBaseAdapter = new MyBaseAdapter(this,R.layout.list_item,show);
        ListView lv = (ListView)findViewById(R.id.list);

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert(e_name.getText().toString(),e_age.getText().toString());
            }
        });
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete();
            }
        });
        btn_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update(e_name.getText().toString(),e_age.getText().toString());
            }
        });
        btn_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find(lv);
                lv.setAdapter(myBaseAdapter);
            }
        });
    }
    //增加
    public void insert(String name,String age){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("s_name",name);
        values.put("s_age",age);
        long id = db.insert("stu",null,values);
        db.close();
    }
    //删除
    public void delete(){
        SQLiteDatabase db = helper.getWritableDatabase();
        int number = db.delete("stu",null,null);
        db.close();
    }
    //修改
    public int update(String name,String age){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("s_age",age);
        int number = db.update("stu",values,"s_name=?",new String[]{name});
        db.close();
        return number;
    }
    //查找
    public void find(View view){
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from stu",null);
        if(cursor.getCount() != 0){
            while(cursor.moveToNext()){
               Student s = new Student(this);
               s.setId(cursor.getInt(0));
               s.setName(cursor.getString(1));
               s.setAge(cursor.getString(2));
               show.add(s);
            }
        }
        cursor.close();
        db.close();
    }
    class MyBaseAdapter extends BaseAdapter{

        public MyBaseAdapter(MainActivity mainActivity, int list_item, List<Student> show) {
        }

        @Override
        public int getCount() {
            return show.size();
        }

        @Override
        public Object getItem(int position) {
            return show.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(MainActivity.this,R.layout.list_item,null);
            TextView tvid = (TextView)findViewById(R.id.textView5);
            TextView tvname = (TextView)findViewById(R.id.textView6);
            TextView tvage = (TextView)findViewById(R.id.textView7);

            tvid.setText(show.get(position)+"_id");
            tvname.setText(show.get(position)+"e_name");
            tvage.setText(show.get(position)+"e_age");
            return view;
        }
    }
}

 

推荐阅读