我正在开发用于学习多层次语言的游戏。所以,例如,我有EnglishAlphabet类,我可以打开所有的英文字母(A,B,C,D,E ......)和EnglishAlphabetA,EnglishAlphabetB ......这些级别。现在当有人完成EnglishAlphabetA级别时,我想让这个级别的按钮不可见。
我正在尝试用SQLite完成这项工作:
DatabaseHelper.java
package com.mmilosanovic.interenglish_serbian;
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public static final String KEY_ID = "_id";
public static final String KEY_ALPHABET = "Abeceda";
public static final String KEY_KATEGORIZACIJA = "Kategorizacija";
public static final String KEY_VREDNOSTI = "Vrednosti";
private static final String DATABASE_NAME = "interEnglishSerbian";
private static final String DATABASE_TABLE = "interensrb";
private static final int DATABASE_VERSION = 1;
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ALPHABET
+ " TEXT NOT NULL, " + KEY_KATEGORIZACIJA
+ " TEXT NOT NULL, " + KEY_VREDNOSTI + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public void createEntry(String abeceda) { // for instance, inserting "a" if EnglishAlphabetA level is completed
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ALPHABET, abeceda);
try {
db.insert(DATABASE_TABLE, null, values);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public ArrayList<String> getAllStringValues() { // reading all the values from KEY_ALPHABET column
SQLiteDatabase db = this.getReadableDatabase();
ArrayList<String> alphabet = new ArrayList<String>();
Cursor result = db.query(true, DATABASE_TABLE,
new String[] { KEY_ALPHABET }, null, null, null, null,
null, null);
if (result.moveToFirst()) {
do {
alphabet.add(result.getString(result
.getColumnIndex(KEY_ALPHABET)));
} while (result.moveToNext());
} else {
return null;
}
return alphabet;
}
}
EnglishAlphabet.java (这是在onCreate方法中)
package com.mmilosanovic.interenglish_serbian;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class EnglishAlphabet extends Activity implements OnClickListener {
Button a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w,
x, y, z;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_english_alphabet);
DatabaseHelper db = new DatabaseHelper(EnglishAlphabet.this);
ArrayList<String> array = db.getAllStringValues();
if (array != null) {
String[] arr = array.toArray(new String[array.size()]);
if (arr[0].equalsIgnoreCase("a")) {
a.setVisibility(View.INVISIBLE);
}
}
a = (Button) findViewById(R.id.bA);
b = (Button) findViewById(R.id.bB);
c = (Button) findViewById(R.id.bC);
d = (Button) findViewById(R.id.bD);
e = (Button) findViewById(R.id.bE);
f = (Button) findViewById(R.id.bF);
g = (Button) findViewById(R.id.bG);
h = (Button) findViewById(R.id.bH);
i = (Button) findViewById(R.id.bI);
j = (Button) findViewById(R.id.bJ);
k = (Button) findViewById(R.id.bK);
l = (Button) findViewById(R.id.bL);
m = (Button) findViewById(R.id.bM);
n = (Button) findViewById(R.id.bN);
o = (Button) findViewById(R.id.bO);
p = (Button) findViewById(R.id.bP);
q = (Button) findViewById(R.id.bQ);
r = (Button) findViewById(R.id.bR);
s = (Button) findViewById(R.id.bS);
t = (Button) findViewById(R.id.bT);
u = (Button) findViewById(R.id.bU);
v = (Button) findViewById(R.id.bV);
w = (Button) findViewById(R.id.bW);
x = (Button) findViewById(R.id.bX);
y = (Button) findViewById(R.id.bY);
z = (Button) findViewById(R.id.bZ);
a.setOnClickListener(this);
b.setOnClickListener(this);
c.setOnClickListener(this);
d.setOnClickListener(this);
e.setOnClickListener(this);
f.setOnClickListener(this);
g.setOnClickListener(this);
h.setOnClickListener(this);
i.setOnClickListener(this);
j.setOnClickListener(this);
k.setOnClickListener(this);
l.setOnClickListener(this);
m.setOnClickListener(this);
n.setOnClickListener(this);
o.setOnClickListener(this);
p.setOnClickListener(this);
q.setOnClickListener(this);
r.setOnClickListener(this);
s.setOnClickListener(this);
t.setOnClickListener(this);
u.setOnClickListener(this);
v.setOnClickListener(this);
w.setOnClickListener(this);
x.setOnClickListener(this);
y.setOnClickListener(this);
z.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bA:
Intent ia = new Intent(this, EnglishAlphabetA.class);
startActivity(ia);
break;
case R.id.bB:
Intent ib = new Intent(this, EnglishAlphabetB.class);
startActivity(ib);
break;
case R.id.bC:
break;
case R.id.bD:
break;
case R.id.bE:
break;
case R.id.bF:
break;
case R.id.bG:
break;
case R.id.bH:
break;
case R.id.bI:
break;
case R.id.bJ:
break;
case R.id.bK:
break;
case R.id.bL:
break;
case R.id.bM:
break;
case R.id.bN:
break;
case R.id.bO:
break;
case R.id.bP:
break;
case R.id.bQ:
break;
case R.id.bR:
break;
case R.id.bS:
break;
case R.id.bT:
break;
case R.id.bU:
break;
case R.id.bV:
break;
case R.id.bW:
break;
case R.id.bX:
break;
case R.id.bY:
break;
case R.id.bZ:
break;
}
}
}
在 EnglishAlphabetA.java 类中,我正在插入&#34; a&#34;进入KEY_ALPHABET栏。
package com.mmilosanovic.interenglish_serbian;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.graphics.Typeface;
import android.util.Log;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class EnglishAlphabetA extends Activity implements AnimationListener, OnClickListener {
private TextView option1, option2, option3, option4, option5, option6, choice1, choice2, choice3, choice4, choice5, choice6;
ImageView crveni, zeleni;
public CharSequence dragData;
Animation animZeleni, animCrveni;
FrameLayout frameLayout;
boolean choice1Dropped = false;
boolean choice2Dropped = false;
boolean choice3Dropped = false;
boolean choice4Dropped = false;
boolean choice5Dropped = false;
boolean choice6Dropped = false;
Button nextLevel;
Button menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_english_alphabet_a);
// get both sets of text views
frameLayout = (FrameLayout) findViewById(R.id.FrameLayout1);
// views to drag
option1 = (TextView) findViewById(R.id.option_1);
option2 = (TextView) findViewById(R.id.option_2);
option3 = (TextView) findViewById(R.id.option_3);
option4 = (TextView) findViewById(R.id.option_4);
option5 = (TextView) findViewById(R.id.option_5);
option6 = (TextView) findViewById(R.id.option_6);
menu = (Button) findViewById(R.id.bMenu);
nextLevel = (Button) findViewById(R.id.bNextLevel);
menu.setOnClickListener(this);
nextLevel.setOnClickListener(this);
// views to drop onto
choice1 = (TextView) findViewById(R.id.choice_1);
choice2 = (TextView) findViewById(R.id.choice_2);
choice3 = (TextView) findViewById(R.id.choice_3);
choice4 = (TextView) findViewById(R.id.choice_4);
choice5 = (TextView) findViewById(R.id.choice_5);
choice6 = (TextView) findViewById(R.id.choice_6);
// set touch listeners
option1.setOnTouchListener(new ChoiceTouchListener());
option2.setOnTouchListener(new ChoiceTouchListener());
option3.setOnTouchListener(new ChoiceTouchListener());
option4.setOnTouchListener(new ChoiceTouchListener());
option5.setOnTouchListener(new ChoiceTouchListener());
option6.setOnTouchListener(new ChoiceTouchListener());
// set drag listeners
choice1.setOnDragListener(new ChoiceDragListener());
choice2.setOnDragListener(new ChoiceDragListener());
choice3.setOnDragListener(new ChoiceDragListener());
choice4.setOnDragListener(new ChoiceDragListener());
choice5.setOnDragListener(new ChoiceDragListener());
choice6.setOnDragListener(new ChoiceDragListener());
crveni = (ImageView) findViewById(R.id.ivCrveni);
zeleni = (ImageView) findViewById(R.id.ivZeleni);
animZeleni = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
animCrveni = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out);
animZeleni.setAnimationListener(this);
animCrveni.setAnimationListener(this);
}
/**
* ChoiceTouchListener will handle touch events on draggable views
*
*/
private final class ChoiceTouchListener implements OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/*
* Drag details: we only need default behavior - clip data could
* be set to pass data as part of drag - shadow can be tailored
*/
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
// start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
}
/**
* DragListener will handle dragged views being dropped on the drop area -
* only the drop action will have processing added to it as we are not -
* amending the default behavior for other parts of the drag process
*
*/
private class ChoiceDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
// no action necessary
break;
case DragEvent.ACTION_DRAG_EXITED:
// no action necessary
break;
case DragEvent.ACTION_DROP:
// handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
// view dragged item is being dropped on
TextView dropTarget = (TextView) v;
// view being dragged and dropped
TextView dropped = (TextView) view;
// checking whether first character of dropTarget equals first
// character of dropped
String[] srb = getResources().getStringArray(R.array.ealphabet_a);
String[] eng = getResources().getStringArray(R.array.salphabet_a);
if (dropTarget.getText().toString().equals(srb[0])
&& dropped.getText().toString().equals(eng[2])) {
choice1Dropped = true;
// stop displaying the view where it was before it was
// dragged
zeleni.setVisibility(View.VISIBLE);
zeleni.startAnimation(animZeleni);
view.setVisibility(View.INVISIBLE);
// update the text in the target view to reflect the data
// being dropped
dropTarget.setVisibility(View.GONE);
// make it bold to highlight the fact that an item has been
// dropped
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
// if an item has already been dropped here, there will be a
// tag
Object tag = dropTarget.getTag();
// if there is already an item here, set it back visible in
// its original place
if (tag != null) {
// the tag is the view id already dropped here
int existingID = (Integer) tag;
// set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
// set the tag in the target view being dropped on - to the
// ID of the view being dropped
dropTarget.setTag(dropped.getId());
// remove setOnDragListener by setting OnDragListener to
// null, so that no further drag & dropping on this TextView
// can be done
dropTarget.setOnDragListener(null);
} else if (dropTarget.getText().toString().equals(srb[1])
&& dropped.getText().toString().equals(eng[3])) {
choice2Dropped = true;
zeleni.setVisibility(View.VISIBLE);
zeleni.startAnimation(animZeleni);
view.setVisibility(View.INVISIBLE);
dropTarget.setVisibility(View.GONE);
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
Object tag = dropTarget.getTag();
if (tag != null) {
// the tag is the view id already dropped here
int existingID = (Integer) tag;
// set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
dropTarget.setTag(dropped.getId());
dropTarget.setOnDragListener(null);
} else if (dropTarget.getText().toString().equals(srb[2])
&& dropped.getText().toString().equals(eng[5])) {
choice3Dropped = true;
zeleni.setVisibility(View.VISIBLE);
zeleni.startAnimation(animZeleni);
view.setVisibility(View.INVISIBLE);
dropTarget.setVisibility(View.GONE);
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
Object tag = dropTarget.getTag();
if (tag != null) {
// the tag is the view id already dropped here
int existingID = (Integer) tag;
// set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
dropTarget.setTag(dropped.getId());
dropTarget.setOnDragListener(null);
} else if (dropTarget.getText().toString().equals(srb[3])
&& dropped.getText().toString().equals(eng[4])) {
choice4Dropped = true;
zeleni.setVisibility(View.VISIBLE);
zeleni.startAnimation(animZeleni);
view.setVisibility(View.INVISIBLE);
dropTarget.setVisibility(View.GONE);
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
Object tag = dropTarget.getTag();
if (tag != null) {
// the tag is the view id already dropped here
int existingID = (Integer) tag;
// set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
dropTarget.setTag(dropped.getId());
dropTarget.setOnDragListener(null);
} else if (dropTarget.getText().toString().equals(srb[4])
&& dropped.getText().toString().equals(eng[0])) {
choice5Dropped = true;
zeleni.setVisibility(View.VISIBLE);
zeleni.startAnimation(animZeleni);
view.setVisibility(View.INVISIBLE);
dropTarget.setVisibility(View.GONE);
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
Object tag = dropTarget.getTag();
if (tag != null) {
// the tag is the view id already dropped here
int existingID = (Integer) tag;
// set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
dropTarget.setTag(dropped.getId());
dropTarget.setOnDragListener(null);
} else if (dropTarget.getText().toString().equals(srb[5])
&& dropped.getText().toString().equals(eng[1])) {
choice6Dropped = true;
zeleni.setVisibility(View.VISIBLE);
zeleni.startAnimation(animZeleni);
view.setVisibility(View.INVISIBLE);
dropTarget.setVisibility(View.GONE);
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
Object tag = dropTarget.getTag();
if (tag != null) {
// the tag is the view id already dropped here
int existingID = (Integer) tag;
// set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
dropTarget.setTag(dropped.getId());
dropTarget.setOnDragListener(null);
// ovdeeeeeeeeeeeeee
} else if (!dropTarget.getText().toString().equals(srb[0])
&& !dropped.getText().toString().equals(eng[2])) {
crveni.setVisibility(View.VISIBLE);
crveni.startAnimation(animCrveni);
} else if (!dropTarget.getText().toString().equals(srb[1])
&& !dropped.getText().toString().equals(eng[3])) {
crveni.setVisibility(View.VISIBLE);
crveni.startAnimation(animCrveni);
} else if (!dropTarget.getText().toString().equals(srb[2])
&& !dropped.getText().toString().equals(eng[5])) {
crveni.setVisibility(View.VISIBLE);
crveni.startAnimation(animCrveni);
} else if (!dropTarget.getText().toString().equals(srb[3])
&& !dropped.getText().toString().equals(eng[4])) {
crveni.setVisibility(View.VISIBLE);
crveni.startAnimation(animCrveni);
} else if (!dropTarget.getText().toString().equals(srb[4])
&& !dropped.getText().toString().equals(eng[0])) {
crveni.setVisibility(View.VISIBLE);
crveni.startAnimation(animCrveni);
} else if (!dropTarget.getText().toString().equals(srb[5])
&& !dropped.getText().toString().equals(eng[1])) {
crveni.setVisibility(View.VISIBLE);
crveni.startAnimation(animCrveni);
}
if (choice1Dropped == true && choice2Dropped == true && choice3Dropped == true && choice4Dropped == true && choice5Dropped == true && choice6Dropped == true) {
menu.setVisibility(View.VISIBLE);
nextLevel.setVisibility(View.VISIBLE);
DatabaseHelper entry = new DatabaseHelper(EnglishAlphabetA.this);
entry.createEntry("a");
Log.d("DatabaseHelper", entry+" rows inserted.");
entry.close();
}
break;
case DragEvent.ACTION_DRAG_ENDED:
// no action necessary
break;
default:
break;
}
return true;
}
}
public void reset(View view) {
option1.setVisibility(TextView.VISIBLE);
option2.setVisibility(TextView.VISIBLE);
option3.setVisibility(TextView.VISIBLE);
choice1.setVisibility(TextView.VISIBLE);
choice2.setVisibility(TextView.VISIBLE);
choice3.setVisibility(TextView.VISIBLE);
choice1.setText("Jabuka");
choice2.setText("Narandza");
choice3.setText("Lopta");
choice1.setTag(null);
choice2.setTag(null);
choice3.setTag(null);
choice1.setTypeface(Typeface.DEFAULT);
choice2.setTypeface(Typeface.DEFAULT);
choice3.setTypeface(Typeface.DEFAULT);
choice1.setOnDragListener(new ChoiceDragListener());
choice2.setOnDragListener(new ChoiceDragListener());
choice3.setOnDragListener(new ChoiceDragListener());
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.bMenu:
Intent i = new Intent(this, EnglishAlphabet.class);
startActivity(i);
break;
case R.id.bNextLevel:
break;
}
}
}
我正在使用 Intent 在活动之间移动。 应用程序工作正常,除非它在完成关卡后不会使按钮不可见。
我真的很感激一些帮助。提前谢谢。