This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1633 from zcjzzf/master
- Loading branch information
Showing
4 changed files
with
247 additions
and
50 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
146 changes: 146 additions & 0 deletions
146
students/net1814080903214/app/src/main/java/edu/hzuapps/androidlabs/Records.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,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; | ||
} | ||
} |
82 changes: 51 additions & 31 deletions
82
students/net1814080903214/app/src/main/java/edu/hzuapps/androidlabs/ResetActivity.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,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(""); | ||
} | ||
|
||
} |
45 changes: 31 additions & 14 deletions
45
students/net1814080903214/app/src/main/java/edu/hzuapps/androidlabs/StartActivity.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,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); | ||
} | ||
} |