我是新的Android應用程序開發(大約一個月前開始),所以我希望你會原諒我,如果這個問題很容易,或者我編碼錯了。在gridview中永久更改按鈕backgroundi android
我想建立一個謎語的應用程序,其中應用程序問一個問題,代碼檢查,如果答案是正確的(對於第一個應用果然如此簡單嗎?) 這個謎語的應用程序有多個謎語,你需要解鎖(如果你完成了謎語1,你只能玩謎語2)。 我在Gridview中顯示不同的謎語,我設法得到點擊功能和一切工作。此外,使用SQlitedatabase我管理該應用程序只允許您在Riddle 1完成時單擊Riddle 2(並在關閉應用程序時保存進度)。
好,所以這個問題是這樣的
現在的問題是,我希望用戶很容易地看到他/她迄今已解鎖該水平。 要做到這一點,我只想顯示帶有圖像的gridview(實際上帶有背景圖像的按鈕),並在上一個謎語完成時,圖像應該永久變爲解鎖圖像。 現在我花了好幾天的時間來嘗試這個,看着很多看似相似的線程,但我無法把它關閉。所以我希望你們能幫忙!
代碼: 包含在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,
};
}
所以basicly我想要的代碼做的是改變資源的mThumbIds到R.drawable.unlockedwood(就像默認的第一個謎語),當前面的謎語是compl時eted。如前所述,我使用一個非常基本的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;
}
每當一個難題完成。添加一個新字符串:「puzzel#completed」
我希望有人能夠幫助! 由於事先 Chiel
你可以檢查有關[自定義ViewTypes](http://stackoverflow.com/questions/4777272/android-listview -with-different-layout-for-each-row)在適配器中。 – Fllo