forked from hzuapps/android-labs-2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
625 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletions
183
...03141/src/main/java/edu/hzuapp/androidlabs/net1814080903141/ContentProviderOfRecords.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package edu.hzuapp.androidlabs.net1814080903141; | ||
|
||
import android.content.ContentProvider; | ||
import android.content.ContentUris; | ||
import android.content.ContentValues; | ||
import android.content.Context; | ||
import android.content.UriMatcher; | ||
import android.database.Cursor; | ||
import android.database.SQLException; | ||
import android.database.sqlite.SQLiteDatabase; | ||
import android.database.sqlite.SQLiteOpenHelper; | ||
import android.database.sqlite.SQLiteQueryBuilder; | ||
import android.net.Uri; | ||
import android.text.TextUtils; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* 类名最好改为: BookProvider | ||
*/ | ||
public class ContentProviderOfRecords extends ContentProvider { | ||
|
||
static final String PROVIDER_NAME = "edu.hzuapps.androidlabs.ContentProviderOfRecords"; | ||
static final String URL = "content://" + PROVIDER_NAME + "/records"; | ||
static final Uri CONTENT_URI = Uri.parse(URL); | ||
|
||
static final String _ID = "_id"; | ||
static final String SCORE = "score"; | ||
static final String BEST = "best"; | ||
|
||
static final int RECORDS = 1; | ||
static final int RECORD_ID = 2; | ||
|
||
static final UriMatcher uriMatcher; | ||
|
||
static { | ||
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); | ||
uriMatcher.addURI(PROVIDER_NAME, "records", RECORDS); | ||
uriMatcher.addURI(PROVIDER_NAME, "records/#", RECORD_ID); | ||
} | ||
|
||
// 数据库相关操作 | ||
private SQLiteDatabase db; | ||
static final String DATABASE_NAME = "Game"; | ||
static final String TABLE_RECORDS = "records"; | ||
static final int DATABASE_VERSION = 1; | ||
static final String CREATE_DB_TABLE = | ||
" CREATE TABLE " + TABLE_RECORDS + | ||
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + | ||
" score TEXT NOT NULL, " + | ||
" best TEXT NOT NULL);"; | ||
|
||
/** | ||
* 用于创建数据库的帮助类 | ||
*/ | ||
private static class DatabaseHelper extends SQLiteOpenHelper { | ||
DatabaseHelper(Context context) { | ||
super(context, DATABASE_NAME, null, DATABASE_VERSION); | ||
} | ||
|
||
@Override | ||
public void onCreate(SQLiteDatabase db) { | ||
db.execSQL(CREATE_DB_TABLE); | ||
} | ||
|
||
@Override | ||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | ||
db.execSQL("DROP TABLE IF EXISTS " + TABLE_RECORDS); | ||
onCreate(db); | ||
} | ||
} | ||
|
||
private static HashMap<String, String> RECORDS_PROJECTION_MAP; | ||
|
||
public ContentProviderOfRecords() { | ||
} | ||
|
||
@Override | ||
public String getType(Uri uri) {switch (uriMatcher.match(uri)){ | ||
case RECORDS: | ||
return "vnd.android.cursor.dir/vnd.example.students"; | ||
case RECORD_ID: | ||
return "vnd.android.cursor.item/vnd.example.students"; | ||
|
||
default: | ||
throw new IllegalArgumentException("Unsupported URI: " + uri); | ||
} | ||
} | ||
|
||
// 插入|保存一条记录 | ||
@Override | ||
public Uri insert(Uri uri, ContentValues values) { | ||
long rowID = db.insert(TABLE_RECORDS, "", values); | ||
if (rowID > 0) { | ||
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID); | ||
getContext().getContentResolver().notifyChange(_uri, null); | ||
return _uri; | ||
} | ||
throw new SQLException("无法插入数据 " + uri); | ||
} | ||
|
||
// 初始化内容提供器 | ||
@Override | ||
public boolean onCreate() { | ||
Context context = getContext(); | ||
DatabaseHelper dbHelper = new DatabaseHelper(context); | ||
// 创建可写的数据库(如果没有则新建) | ||
db = dbHelper.getWritableDatabase(); | ||
//db = dbHelper.getReadableDatabase() | ||
return (db == null) ? false : true; | ||
} | ||
|
||
// 查询记录 | ||
@Override | ||
public Cursor query(Uri uri, String[] projection, String selection, | ||
String[] selectionArgs, String sortOrder) { | ||
SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); | ||
qb.setTables(TABLE_RECORDS); | ||
|
||
switch (uriMatcher.match(uri)) { | ||
case RECORDS: | ||
qb.setProjectionMap(RECORDS_PROJECTION_MAP); | ||
break; | ||
|
||
case RECORD_ID: | ||
qb.appendWhere(_ID + "=" + uri.getPathSegments().get(1)); | ||
break; | ||
|
||
default: | ||
throw new IllegalArgumentException("Unknown URI " + uri); | ||
} | ||
|
||
if (sortOrder == null || sortOrder == "") { | ||
sortOrder = SCORE; | ||
} | ||
Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder); | ||
c.setNotificationUri(getContext().getContentResolver(), uri); | ||
return c; | ||
} | ||
|
||
@Override | ||
public int update(Uri uri, ContentValues values, String selection, | ||
String[] selectionArgs) { | ||
int count = 0; | ||
switch (uriMatcher.match(uri)) { | ||
case RECORDS: | ||
count = db.update(TABLE_RECORDS, values, selection, selectionArgs); | ||
break; | ||
|
||
case RECORD_ID: | ||
count = db.update(TABLE_RECORDS, values, _ID + " = " + uri.getPathSegments().get(1) + | ||
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); | ||
break; | ||
|
||
default: | ||
throw new IllegalArgumentException("Unknown URI " + uri); | ||
} | ||
getContext().getContentResolver().notifyChange(uri, null); | ||
return count; | ||
} | ||
|
||
@Override | ||
public int delete(Uri uri, String selection, String[] selectionArgs) { | ||
int count = 0; | ||
switch (uriMatcher.match(uri)) { | ||
case RECORDS: | ||
count = db.delete(TABLE_RECORDS, selection, selectionArgs); | ||
break; | ||
|
||
case RECORD_ID: | ||
String id = uri.getPathSegments().get(1); | ||
count = db.delete(TABLE_RECORDS, _ID + " = " + id + | ||
(!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""), selectionArgs); | ||
break; | ||
|
||
default: | ||
throw new IllegalArgumentException("Unknown URI " + uri); | ||
} | ||
|
||
getContext().getContentResolver().notifyChange(uri, null); | ||
return count; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../net1814080903141/src/main/java/edu/hzuapp/androidlabs/net1814080903141/GameActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,64 @@ | ||
package edu.hzuapp.androidlabs.net1814080903141; | ||
|
||
|
||
import android.content.ContentValues; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.Toast; | ||
|
||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
public class GameActivity extends AppCompatActivity { | ||
|
||
private Button btnsave; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_game); | ||
|
||
btnsave=findViewById(R.id.btnsave); | ||
|
||
final GameActivity thisActivity=this; | ||
// 保存记录信息 | ||
btnsave.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
thisActivity.saveRecord(); | ||
} | ||
}); | ||
|
||
} | ||
|
||
private void saveRecord() { | ||
String score = ((EditText) findViewById(R.id.tscore)).getText().toString(); | ||
String best = ((EditText) findViewById(R.id.tbest)).getText().toString(); | ||
|
||
// 插入新记录 | ||
ContentValues record = new ContentValues(); | ||
//book.put("name", name); // Map <- Key:Value | ||
record.put(ContentProviderOfRecords.SCORE,score); | ||
record.put(ContentProviderOfRecords.BEST, best); | ||
|
||
Uri uri = getContentResolver() // 执行插入操作 | ||
.insert(ContentProviderOfRecords.CONTENT_URI, record); | ||
|
||
Toast.makeText(getBaseContext(), // | ||
"保存成功! \n" + uri.toString(), Toast.LENGTH_LONG).show(); | ||
|
||
this.showRecordInfo("", ""); // 清除内容 | ||
} | ||
|
||
private void showRecordInfo(String score, String best) { | ||
((EditText) findViewById(R.id.tscore)).setText(score); | ||
((EditText) findViewById(R.id.tbest)).setText(best); | ||
} | ||
|
||
|
||
} | ||
|
||
|
32 changes: 0 additions & 32 deletions
32
...03141/src/main/java/edu/hzuapp/androidlabs/net1814080903141/Net1814080903141Activity.java
This file was deleted.
Oops, something went wrong.
60 changes: 60 additions & 0 deletions
60
...et1814080903141/src/main/java/edu/hzuapp/androidlabs/net1814080903141/RecordActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,73 @@ | ||
package edu.hzuapp.androidlabs.net1814080903141; | ||
|
||
import android.content.ContentValues; | ||
|
||
|
||
import android.database.Cursor; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
|
||
import androidx.appcompat.app.AppCompatActivity; | ||
|
||
public class RecordActivity extends AppCompatActivity { | ||
|
||
private Button btnclear; | ||
private Button btncheck; | ||
private StringBuilder sb; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_record); | ||
|
||
btncheck = findViewById(R.id.btncheak); | ||
btnclear=findViewById(R.id.btnclear); | ||
|
||
final RecordActivity thisActivity = this; | ||
|
||
|
||
btncheck.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
thisActivity.showRecord(); | ||
} | ||
}); | ||
btnclear.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
thisActivity.clearRecord(); | ||
} | ||
}); | ||
} | ||
private void showRecord() { | ||
sb =new StringBuilder(); | ||
// 插入新记录 | ||
Cursor cursor=getContentResolver().query(ContentProviderOfRecords.CONTENT_URI,null,null,null,null); | ||
|
||
if (cursor.moveToFirst()){ | ||
do { | ||
int gscore=cursor.getInt(cursor.getColumnIndex(ContentProviderOfRecords.SCORE)); | ||
int gbest=cursor.getInt(cursor.getColumnIndex(ContentProviderOfRecords.BEST)); | ||
sb.append("Score:"+gscore+" "+"Best:"+gbest+"\n"); | ||
((TextView) findViewById(R.id.trecord)).setText(sb); | ||
}while ((cursor.moveToNext())); | ||
|
||
} | ||
cursor.close(); | ||
} | ||
private void clearRecord(){ | ||
getContentResolver().delete(ContentProviderOfRecords.CONTENT_URI,null,null); | ||
Toast.makeText(getBaseContext(), // | ||
"删除成功! \n" + sb.toString(), Toast.LENGTH_LONG).show(); | ||
((TextView) findViewById(R.id.trecord)).setText(""); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.