【Java转Android】29

20 阅读1分钟

}

package nopi.aystudio.mthread;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.ContentValues;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.graphics.BitmapFactory;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.text.TextUtils;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import com.google.gson.Gson;

import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import org.xml.sax.XMLReader;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserException;

import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.StringReader;

import java.util.List;

import java.util.SortedMap;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParserFactory;

import okhttp3.OkHttpClient;

import okhttp3.Request;

import okhttp3.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

Button button;

Button btn_add;

Button btn_read;

Button btn_del;

Button btn_update;

private DatabaseHelper databaseHelper;

private static final String TAG = "MainActivity";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = findViewById(R.id.btn_get);

btn_add = findViewById(R.id.btn_add);

btn_read = findViewById(R.id.btn_read);

btn_del = findViewById(R.id.btn_del);

btn_update = findViewById(R.id.btn_update);

button.setOnClickListener(this);

btn_del.setOnClickListener(this);

btn_update.setOnClickListener(this);

btn_read.setOnClickListener(this);

btn_add.setOnClickListener(this);

databaseHelper = new DatabaseHelper(this,"BookStore.db",null,2);

}

@Override

public void onClick(View v) {

SQLiteDatabase db = databaseHelper.getWritableDatabase();

ContentValues values = new ContentValues();

switch (v.getId()) {

case R.id.btn_get:

break;

case R.id.btn_add:

values.put("name","Tom Menu");

values.put("author","Tom");

values.put("pages","454");

values.put("price",16.96);

db.insert("Book",null,values);

values.clear();

values.put("name","Alice Menu");

values.put("author","Alice");

values.put("pages","510");

values.put("price",19.95);

db.insert("Book",null,values);

break;

case R.id.btn_read:

Cursor cursor = db.query("Book",null,null,null,null,null,null);

if (cursor.moveToFirst()){

do {

String name = cursor.getString(cursor.getColumnIndex("name"));

String author = cursor.getString(cursor.getColumnIndex("author"));

int pages = cursor.getInt(cursor.getColumnIndex("pages"));

double price = cursor.getDouble(cursor.getColumnIndex("price"));

Log.d(TAG, "onClick: "+name+"=========="+author+"======="+pages+"======="+price);

}while (cursor.moveToNext());

}

cursor.close();

break;

case R.id.btn_del:

db.delete("Book","pages > ?",new String[]{"500"});

break;

case R.id.btn_update:

values.put("price",10.99);

db.update("Book",values,"author = ?",new String[]{"Tom"});

break;

default:

break;

}

}

}

<LinearLayout xmlns:android="schemas.android.com/apk/res/and…"

xmlns:app="schemas.android.com/apk/res-aut…"

xmlns:tools="schemas.android.com/tools"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<Button

android:id="@+id/btn_get"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="创建数据库"/>

<Button

android:id="@+id/btn_add"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="添加数据"/>

<Button

android:id="@+id/btn_read"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="读取数据"/>

<Button

android:id="@+id/btn_del"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="删除数据"/>

<Button