Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1633 from zcjzzf/master
Browse files Browse the repository at this point in the history
#5 #407 第五次实验
  • Loading branch information
zengsn authored Jan 10, 2021
2 parents b035e1f + 5680820 commit 50d5f03
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 50 deletions.
24 changes: 19 additions & 5 deletions students/net1814080903214/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs">
package="edu.hzuapp.androidlabs.net1814080903141">

<application
android:allowBackup="true"
Expand All @@ -9,13 +9,27 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Net1814080903214Activity"
<activity android:name=".RecordActivity"
android:parentActivityName=".Net1814080903141Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Net1814080903141Activity"></meta-data>
</activity>
<activity android:name=".GameActivity"
android:parentActivityName=".Net1814080903141Activity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Net1814080903141Activity"></meta-data>
</activity>
<activity android:name=".Net1814080903141Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<provider
android:authorities="edu.hzuapps.androidlabs.ContentProviderOfRecords"
android:name=".ContentProviderOfRecords"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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;

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();
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.app.AppCompatActivity;
package edu.hzuapp.androidlabs.net1814080903141;

import android.content.Intent;
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 HistoryScoreActivity extends 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_history_score);

Button returnToGame = (Button)findViewById(R.id.returnToGame);
returnToGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(HistoryScoreActivity.this, GameActivity.class);
startActivity(intent);
}
}
);

Button returnToMain = (Button)findViewById(R.id.returnToMain);
returnToMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(HistoryScoreActivity.this, Net1814080903106Activity.class);
startActivity(intent);
}
}
);

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("");
}

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import androidx.appcompat.app.AppCompatActivity;
package edu.hzuapp.androidlabs.net1814080903141;

import android.content.Intent;
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();

Button toHistoryScore = (Button)findViewById(R.id.toHistoryScore);
toHistoryScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(GameActivity.this, HistoryScoreActivity.class);
startActivity(intent);
}
}
);

ContentValues record = new ContentValues();
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);
}
}

0 comments on commit 50d5f03

Please sign in to comment.