我是新的Android应用程序开发(大约一个月前开始),所以我希望你能原谅我,如果这个问题很容易,或者编码错误。
我正在尝试构建一个谜语应用程序,其中应用程序提出问题并且代码检查答案是否正确(对于第一个应用程序来说简单到了吗?) 这个谜语应用程序有多个谜语,你需要按顺序解锁它们(如果你完成了谜语1,你只能玩谜语2)。 我在Gridview中显示不同的谜语,我设法获得了点击功能,一切正常。此外,使用我管理的SQlitedatabase,如果Riddle 1已完成,应用程序仅允许您单击Riddle 2(并在关闭应用程序时保存进度)。
好的,问题是这个
现在的问题是我希望用户能够轻松查看到目前为止他/她已解锁的级别。 要做到这一点,我只想显示带有图像(实际上带有背景图像的按钮)的网格视图,这些图像显示锁定,并且当前一个谜语完成时,图像应该永久地更改为未锁定的图像。 现在我花了好几天尝试这个并看了许多看似相似的线程,但我无法将其拉下来。所以我希望你们能帮忙!
代码: 包含gridview的活动
package com.michiel.puzzelapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
public class Main2Activity extends AppCompatActivity {
Intent i = getIntent();
MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
dbHandler = new MyDBHandler(this, null, null, 1);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
//System.out.println("a" + position);
// I expect that some people will consider this awfull coding.. However for now it works so I'll work on this later:)
if (position == 0) {
Intent iinent = new Intent(Main2Activity.this, Main22Activity.class);
startActivity(iinent);
}
if (position == 1) {
boolean result = dbHandler.hasObject("puzzel 1 completed");
String str = String.valueOf(result);
if (str.equals("true")){
Intent i3 = new Intent(Main2Activity.this, Raadsel2.class);
startActivity(i3);
}
}
// ******* this continues for the other riddles (until position == 50) but i cut this out because the rest is similar and not that relevant..
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
图片适配器代码
package com.michiel.puzzelapp;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
Button button;
public int getCount() {
return filesnames.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
if (convertView == null) {
button = new Button(mContext);
button.setLayoutParams(new GridView.LayoutParams(285, 200));
button.setPadding(8, 8, 8, 8);
} else {
button = (Button) convertView;
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((GridView) parent).performItemClick(v, position, 0);
}
});
button.setText(filesnames[position]);
button.setTextColor(Color.WHITE);
button.setBackgroundResource(mThumbIds[position]);
button.setId(position);
return button;
}
public String[] filesnames = {
"Raadsel 1", "Raadsel 2", "Raadsel 3", "Raadsel 4", "Raadsel 5", "Raadsel 6", "Raadsel 7", "Raadsel 8", "Raadsel 9", "Raadsel 10",
"Raadsel 11", "Raadsel 12", "Raadsel 13", "Raadsel 14", "Raadsel 15", "Raadsel 16", "Raadsel 17", "Raadsel 18", "Raadsel 19", "Raadsel 20",
"Raadsel 21", "Raadsel 22", "Raadsel 23", "Raadsel 24", "Raadsel 25", "Raadsel 26", "Raadsel 27", "Raadsel 28", "Raadsel 29", "Raadsel 30"
};
public Integer[] mThumbIds = {
R.drawable.unlockedwood,R.drawable.wood, R.drawable.wood,R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood,
R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood,R.drawable.wood, R.drawable.wood,R.drawable.wood,
R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood, R.drawable.wood,
R.drawable.wood,R.drawable.wood, R.drawable.wood,R.drawable.wood, R.drawable.wood, R.drawable.wood,
};
}
基本上我想要的代码是在前一个谜语完成时将mThumbIds中的资源更改为R.drawable.unlockedwood(就像默认情况下的第一个谜语一样)。 如前所述,我使用非常基本的SQlitedatabase来跟踪玩家状态。它只包含一个包含两列的表,请参阅下面的代码部分。所以数据库和Imageadapter应该以某种方式进行通信..
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
//Oncreate, onUpgrade, addProduct and delete product code is here in between.
//**********************
// And this is the hasObject i use to check the database:
public boolean hasObject(String id) {
SQLiteDatabase db = getWritableDatabase();
String selectString = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " =?";
Cursor cursor = db.rawQuery(selectString, new String[] {id});
boolean hasObject = false;
if(cursor.moveToFirst()){
hasObject = true;
int count = 0;
while(cursor.moveToNext()){
count++;
}
}
cursor.close();
db.close();
return hasObject;
}
每当拼图完成时。添加了一个新字符串:&#34; puzzel#completed&#34;
我希望有人能够提供帮助! 提前致谢, Chiel
答案 0 :(得分:0)
我认为你应该保持&#34; isLock(真/假)&#34;每个项目的标志,如果标志为true则检入适配器,然后设置锁定背景,否则解锁。
首先默认设置解锁或锁定
答案 1 :(得分:0)
您可以创建一个包含等级信息的新类,例如:
public class LevelInfo{
public boolean locked = true;
public String title;
public int imageID;
public LevelInfo(boolean locked, String title, int imageID){
this.locked = locked;
this.title = title;
this.imageID = imageID;
}
}
稍微更改适配器,以便可以将此类对象的列表传递给它。
public class ImageAdapter extends BaseAdapter {
private Context mContext;
List<LevelInfo> levelsInfo;
public ImageAdapter(Context c, List<LevelInfo> levelsInfo) {
mContext = c;
this.levelsInfo = levelsInfo;
}
public int getCount() {
return levelsInfo.size();
}
public View getView(final int position, View convertView, final ViewGroup parent) {
....
LevelInfo currentLevel = levelsInfo.get(position);
button.setText(currentLevel.title);
button.setTextColor(Color.WHITE);
if(currentLevel.locked){
button.setBackgroundResource(R.drawable.wood);
}else{
button.setBackgroundResource(currentLevel.image);
}
button.setId(position);
return button;
}
}
以下是如何为此addapter创建列表:
public class Main2Activity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
List<LevelInfo> list = new ArrayList<LevelInfo>();
LevelInfo level1 = new LevelInfo(dbHandler.hasObject("puzzel 1 completed"), "Level 1", R.drawable.level1);
list.add(level1);
....
gridview.setAdapter(new ImageAdapter(this, list));
....
}
...
}