Android

android


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="160dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓 名:"
        android:textSize="18sp"/>
    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入姓名"
        android:textSize="16sp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="电 话:"
        android:textSize="18sp"/>
    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入手机号:"
        android:textSize="16sp"/>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <Button
        android:id="@+id/btn_add"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="#B9B9FF"
        android:onClick="onClick"
        android:text="添 加"
        android:textSize="18sp"/>
    <Button
        android:id="@+id/btn_query"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:onClick="onClick"
        android:background="#DCB5FF"
        android:text="查 询"
        android:textSize="18sp"/>
    <Button
        android:id="@+id/btn_update"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:background="#E6CAFF"
        android:text="修 改"
        android:onClick="onClick"
        android:textSize="18sp"/>
    <Button
        android:id="@+id/btn_delete"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:onClick="onClick"
        android:background="#ACD6FF"
        android:text="删 除"
        android:textSize="18sp"/>
</LinearLayout>
<TextView
    android:id="@+id/tv_show"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:textSize="20sp"/>

package com.geye.directory;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MyHelper myHelper;
private EditText mEtName;
private EditText mEtPhone;
private TextView mTvShow;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelete;
private int position;
private List

addresses;
private AddressDao addressDao;
private AddressAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
private void init(){
mEtName = (EditText) findViewById(R.id.et_name);
mEtPhone = (EditText) findViewById(R.id.et_phone);
mTvShow = (TextView) findViewById(R.id.tv_show);
mBtnAdd = (Button) findViewById(R.id.btn_add);
mBtnQuery = (Button) findViewById(R.id.btn_query);
mBtnUpdate = (Button) findViewById(R.id.btn_update);
mBtnDelete = (Button) findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelete.setOnClickListener(this);
RecyclerView rvAddress=findViewById(R.id.rv_address);
rvAddress.setLayoutManager(new LinearLayoutManager(this));
rvAddress.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));
adapter=new AddressAdapter(addresses);
rvAddress.setAdapter(adapter);
adapter.setOnItemTouchListener(new AddressAdapter.OnItemClickListener(){

        @Override
        public void onItemClick(View view, int position) {
            MainActivity.this.position=position;
            mEtName.setText(addresses.get(position).getName());
            mEtPhone.setText(addresses.get(position).getPhone());
            adapter.notifyDataSetChanged();
        }
    });
}

@Override
public void onClick(View v){
    String name;
    String phone;
    SQLiteDatabase db;
    ContentValues values;
    switch (v.getId()){
        case R.id.btn_add:
            insert();

            break;
        case R.id.btn_query:
            query();

            break;
        case R.id.btn_update:
            update();

            break;
        case R.id.btn_delete:
            delete();

            break;
    }
}

private void delete() {
    db = myHelper.getWritableDatabase();
    db.delete("information",null,null);
    Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
    mTvShow.setText("");
    db.close();
}

private void update() {
    db = myHelper.getWritableDatabase();
    values = new ContentValues();
    values.put("phone",phone = mEtPhone.getText().toString());
    db.update("information",values,"name=?",new String[]{mEtName.getText().toString()});
    Toast.makeText(this,"信息已修改",Toast.LENGTH_SHORT).show();
    db.close();
}

private void query() {
    db = myHelper.getReadableDatabase();
    Cursor cursor = db.query("information",null,null,null,null,null,null);
    if(cursor.getCount() == 0){
        mTvShow.setText("");
        Toast.makeText(this,"没有数据",Toast.LENGTH_SHORT).show();
    }else{
        cursor.moveToFirst();
        mTvShow.setText("Name:"+cursor.getString(1)+";Tel:"+cursor.getString(2));
    }
    while(cursor.moveToNext()){
        mTvShow.append("\n"+"Name:"+cursor.getString(1)+";Tel:"+cursor.getString(2));
    }
    cursor.close();
    db.close();
}

String name;
String phone;
SQLiteDatabase db;
ContentValues values;
private void insert() {
    name = mEtName.getText().toString();
    phone = mEtPhone.getText().toString();
    db = myHelper.getWritableDatabase();
    values = new ContentValues();
    values.put("name",name);
    values.put("phone",phone);
    db.insert("information",null,values);
    Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
    db.close();
}

class MyHelper extends SQLiteOpenHelper{
    public MyHelper(Context context){
        super(context,"example.db",null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
    }
}
private void delete(){
    new AlertDialog.Builder(this).setTitle("删除").setMessage("确认删除?")
    int pos=MainActivity.this.position;
    if(addressDao.delete(addresses.get(pos).get_id)){
        addresses.remove(pos);

    }


}

}

package com.geye.directory;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Address;

import java.util.ArrayList;

/**
*
*/
public class AddressDao {
private SQLiteDatabase db;
private DBHeiper dbHeiper;
public AddressDao(Context context){
dbHeiper=new DBHelper(context);
}

public boolean insert(Address address){
    int newId=0;
    db=dbHeiper.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put("name",address.getName());
    values.put("phone",address.getPhone());

    long num=db.insert("information",null,values);
    if (num>0){
        //获取新增数据的自增
        Cursor cursor=db.rawQuery("select last_insert_rowid() from information",null);
        if(cursor !=null &&cursor.moveToFirst()){
            newId=cursor.getInt((cursor.getColumnIndex("_id")));
            cursor.close();
        }

        db.close();
        return newId;
    }

    public boolean update(Address address) {
        db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", address.getName());
        values.put("phone", address.getPhone());
        long num = db.update("information", values, "_id=?",
                new String[]{String.valueOf(address.get_id())});
        db.close();
        return num > 0;
        //直接执行SQL语句的写法
        //String sql = "update information set name=?,phone=? where _id=?";
        //db.execsQL(sql,new String[]{address.getName(), address.getPhone()]);
    }


    public boolean delete(int _id){
        db = dbHelper.getWritableDatabase();
        long num = db.delete("information","_id=?",new String[]{String.valueOf(_id)});
        db.close();
        return num >0;
        //直接执行 SQL语句的写法
        //String sql = "delete from information where _id=?" ;
        //db .execsQL(sql, new String[]{String.value0f(_id)});
    }

}


public List<com.zqx.directory.Address>query(){
    List<com.zqx.directory.Address>addresses=new ArrayList<>();
    db=dbHeiper.getReadableDatabase();

}

}

package com.geye.directory;

import java.io.Serializable;

public class Address implements Serializable {
private int _id;
private String name;
private String phone;

public Address() {
}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}


public Address(String name, String phone) {
    this.name = name;
    this.phone = phone;
}

public Address(int _id, String name, String phone) {
    this._id = _id;
    this.name = name;
    this.phone = phone;
}

@Override
public String toString() {
    return "Address{" +
            "_id=" + _id +
            ", name='" + name + '\'' +
            ", phone='" + phone + '\'' +
            '}';
}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值