Mainactivity.java
package com.example.direction;
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.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;
@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);
mBtnDelete.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name;
String phone;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()) {
case R.id.btn_add:
name = mEtName.getText().toString();
phone = mEtPhone.getText().toString();
db = myHelper.getReadableDatabase();
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();
break;
case R.id.btn_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();
break;
case R.id.btn_update:
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("phone", phone = mEtPhone.getText().toString());
db.update("information", values, "name=?", new String[]{mEtName.getText().toString()});
db.close();
break;
case R.id.btn_delete:
db = myHelper.getWritableDatabase();
db.delete("information", null, null);
Toast.makeText(this, "資訊已經洗掉", Toast.LENGTH_SHORT).show();
mTvShow.setText("");
db.close();
break;
}
}
}
uj5u.com熱心網友回復:
看log日志啊,日志能告訴你為啥退出了uj5u.com熱心網友回復:
錯誤 日志 ,沒有的話,幫不了你的轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/43973.html
標籤:Android
上一篇:微信小程式開發的好處有
