我已经搜索了所有内容,并尝试按照示例进行操作,但我无法实现SharedPreferences。
我正在创造一种非常低技术的PunchCard。 NFCTag有一个文本字段,上面写着“咖啡”,我还有另一个说“三明治”,然后我有两个同名的字符串。点击咖啡标签,它会更改咖啡图像,点击三明治标签,它会更改三明治图像。
我现在的问题是......我能够保存偏好,但它没有更新图像,任何建议都会受到赞赏..
https://github.com/soboapps/PunchCard - 这可能会让你知道我想要做什么。
这是我的代码(我知道这是一团糟)
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public boolean cflag = false;
public boolean sflag = false;
private static final String NAV_ITEM_ID = "navItemId" ;
private static final long DRAWER_CLOSE_DELAY_MS = 300 ;
private DrawerLayout mDrawerLayout;
private int mNavItemId;
private ActionBarDrawerToggle mDrawerToggle;
private final CoffeeFragment mCoffeeFragment= new CoffeeFragment();
private final SandwichFragment mSandwichFragment = new SandwichFragment();
private final AboutFragment mAboutFragment = new AboutFragment();
private final Handler mDrawerActionHandler = new Handler();
private static final DateFormat TIME_FORMAT = SimpleDateFormat.getDateTimeInstance();
private LinearLayout mTagContent;
private LinearLayout mCoffee1_3Content;
private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private NdefMessage mNdefPushMessage;
private AlertDialog mDialog;
public static String myTagId;
public static String myCTagUrl;
public static String mySTagId;
public static String mySTagUrl;
public static String sPayload;
private ImageView mCoffee1ImageView;
private ImageView mCoffee2ImageView;
private ImageView mCoffee3ImageView;
private ImageView mCoffee4ImageView;
private ImageView mCoffee5ImageView;
private ImageView mCoffee6ImageView;
private ImageView mCoffee7ImageView;
private ImageView mCoffee8ImageView;
private ImageView mCoffee9ImageView;
private ImageView mSandwich1ImageView;
private ImageView mSandwich2ImageView;
private ImageView mSandwich3ImageView;
private ImageView mSandwich4ImageView;
private ImageView mSandwich5ImageView;
private ImageView mSandwich6ImageView;
private ImageView mSandwich7ImageView;
private ImageView mSandwich8ImageView;
private ImageView mSandwich9ImageView;
public SharedPreferences prefs;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = PreferenceManager.getDefaultSharedPreferences(this);
cflag = prefs.getBoolean("selected", false);
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout content = mCoffee1_3Content;
View view = inflater.inflate(R.layout.coffee_fragment, content, false);
mCoffee1ImageView = (ImageView) view.findViewById(R.id.img_cof_1);
checkFlag();
// Initialize Drawer layout -> activity_main
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//Initialize Toolbar android.support.v7.widget.Toolbar
Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//Load saved navigation state if present
if (null==savedInstanceState){
mNavItemId = R.id.drawer_item_1;
}else{
mNavItemId = savedInstanceState.getInt(NAV_ITEM_ID);
}
// Listen to navigation events
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
navigationView.setNavigationItemSelectedListener(this);
//Select the Correct nav menu item
navigationView.getMenu().findItem(mNavItemId).setChecked(true);
// Setup the humberger icon to open and close drawer
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
// navigate function to swich between items id
// parameter item id
navigate(mNavItemId);
mTagContent = (LinearLayout) findViewById(R.id.list);
mCoffee1_3Content = (LinearLayout) findViewById(R.id.cof_1_3);
resolveIntent(getIntent());
mDialog = new AlertDialog.Builder(this).setNeutralButton("Ok", null).create();
mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
showMessage(R.string.error, R.string.no_nfc);
finish();
return;
}
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mNdefPushMessage = new NdefMessage(new NdefRecord[] { newTextRecord(
"Message from NFC Reader :-)", Locale.ENGLISH, true) });
}
public void checkFlag(){
if(cflag) {
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee_free);
} else {
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee);
}
}
private void navigate(final int itemId) {
switch (itemId){
case R.id.drawer_item_1:
//start about fragment
getFragmentManager().beginTransaction().replace(R.id.content,mCoffeeFragment).commit();
break;
case R.id.drawer_item_about:
//start project fragment
getFragmentManager().beginTransaction().replace(R.id.content, mAboutFragment).commit();
break;
}
}
private void showMessage(int title, int message) {
mDialog.setTitle(title);
mDialog.setMessage(getText(message));
mDialog.show();
}
private NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) {
byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
byte[] textBytes = text.getBytes(utfEncoding);
int utfBit = encodeInUtf8 ? 0 : (1 << 7);
char status = (char) (utfBit + langBytes.length);
byte[] data = new byte[1 + langBytes.length + textBytes.length];
data[0] = (byte) status;
System.arraycopy(langBytes, 0, data, 1, langBytes.length);
System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT, new byte[0], data);
}
@Override
protected void onResume() {
super.onResume();
checkFlag();
if (mAdapter != null) {
if (!mAdapter.isEnabled()) {
showWirelessSettingsDialog();
}
mAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
mAdapter.enableForegroundNdefPush(this, mNdefPushMessage);
}
}
@Override
protected void onPause() {
super.onPause();
if (mAdapter != null) {
mAdapter.disableForegroundDispatch(this);
mAdapter.disableForegroundNdefPush(this);
}
}
private void showWirelessSettingsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.nfc_disabled);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.create().show();
return;
}
private void resolveIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
// Unknown tag type
byte[] empty = new byte[0];
byte[] id = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
Parcelable tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] payload = dumpTagData(tag).getBytes();
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, id, payload);
NdefMessage msg = new NdefMessage(new NdefRecord[] { record });
msgs = new NdefMessage[] { msg } ;
}
// Setup the views
buildTagViews(msgs);
}
}
private String dumpTagData(Parcelable p) {
StringBuilder sb = new StringBuilder();
Tag tag = (Tag) p;
byte[] id = tag.getId();
sb.append("Tag ID (hex): ").append(getHex(id)).append("\n");
sb.append("Tag ID (dec): ").append(getDec(id)).append("\n");
sb.append("ID (reversed): ").append(getReversed(id)).append("\n");
String prefix = "android.nfc.tech.";
sb.append("Technologies: ");
for (String tech : tag.getTechList()) {
sb.append(tech.substring(prefix.length()));
sb.append(", ");
}
sb.delete(sb.length() - 2, sb.length());
for (String tech : tag.getTechList()) {
if (tech.equals(MifareClassic.class.getName())) {
sb.append('\n');
MifareClassic mifareTag = MifareClassic.get(tag);
String type = "Unknown";
switch (mifareTag.getType()) {
case MifareClassic.TYPE_CLASSIC:
type = "Classic";
break;
case MifareClassic.TYPE_PLUS:
type = "Plus";
break;
case MifareClassic.TYPE_PRO:
type = "Pro";
break;
}
sb.append("Mifare Classic type: ");
sb.append(type);
sb.append('\n');
sb.append("Mifare size: ");
sb.append(mifareTag.getSize() + " bytes");
sb.append('\n');
sb.append("Mifare sectors: ");
sb.append(mifareTag.getSectorCount());
sb.append('\n');
sb.append("Mifare blocks: ");
sb.append(mifareTag.getBlockCount());
}
if (tech.equals(MifareUltralight.class.getName())) {
sb.append('\n');
MifareUltralight mifareUlTag = MifareUltralight.get(tag);
String type = "Unknown";
switch (mifareUlTag.getType()) {
case MifareUltralight.TYPE_ULTRALIGHT:
type = "Ultralight";
break;
case MifareUltralight.TYPE_ULTRALIGHT_C:
type = "Ultralight C";
break;
}
sb.append("Mifare Ultralight type: ");
sb.append(type);
}
}
return sb.toString();
}
private String getHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = bytes.length - 1; i >= 0; --i) {
int b = bytes[i] & 0xff;
if (b < 0x10)
sb.append('0');
sb.append(Integer.toHexString(b));
if (i > 0) {
sb.append(" ");
}
}
return sb.toString();
}
private long getDec(byte[] bytes) {
long result = 0;
long factor = 1;
for (int i = 0; i < bytes.length; ++i) {
long value = bytes[i] & 0xffl;
result += value * factor;
factor *= 256l;
}
return result;
}
private long getReversed(byte[] bytes) {
long result = 0;
long factor = 1;
for (int i = bytes.length - 1; i >= 0; --i) {
long value = bytes[i] & 0xffl;
result += value * factor;
factor *= 256l;
}
return result;
}
void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) {
return;
}
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout content = mTagContent;
// Parse the first message in the list
// Build views for all of the sub records
Date now = new Date();
List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
final int size = records.size();
for (int i = 0; i < size; i++) {
TextView timeView = new TextView(this);
timeView.setText(TIME_FORMAT.format(now));
content.addView(timeView, 0);
ParsedNdefRecord record = records.get(i);
content.addView(record.getView(this, inflater, content, i), 1 + i);
content.addView(inflater.inflate(R.layout.tag_divider, content, false), 2 + i);
// Getting the text from the TextView from tag_text.xml and presenting it as Toast
TextView myTagView = (TextView)findViewById(R.id.text);
myTagId = myTagView.getText().toString();
myCTagUrl = getString(R.string.ctag_url);
mySTagUrl = getString(R.string.stag_url);
if ((mCoffee9ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_free_coffee).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())) {
Toast toast = Toast.makeText(this, "You Redeemed Your Free\n" +
"Cup of Coffee, Thank You\n" +
"For Your Patronage!", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee2ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee3ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee4ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee5ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee6ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee7ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee8ImageView.setImageResource(R.drawable.sbp_coffee);
mCoffee9ImageView.setImageResource(R.drawable.sbp_coffee);
return;
} else if ((mCoffee1ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())) {
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee_free);
//setCvisible();
cflag = true;
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee_free);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("selected", true).commit();
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Earned 1 Credit\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee1ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee2ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee2ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 2 Credits\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee2ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee3ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee3ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 3 Credits\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee3ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee4ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee4ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 4 Credits\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee4ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee5ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee5ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You're Over Half Way to\n" +
"a Free Cup of Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee5ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee6ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee6ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 6 Credits\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee6ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee7ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee7ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 7 Credits\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee7ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee8ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee8ImageView.setImageResource(R.drawable.sbp_coffee_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 8 Credits\n" +
"Towards a Free Coffee", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mCoffee8ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()) && myCTagUrl.equals(myTagView.getText().toString())
&& ((mCoffee9ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_coffee_free).getConstantState()))) {
mCoffee9ImageView.setImageResource(R.drawable.sbp_free_coffee);
Toast toast = Toast.makeText(this, "CONGRATULATION!\n" +
"Your Next\n" +
"Cup of Coffee is on US!", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
}
if ((mSandwich9ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_free_sandwich).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())) {
Toast toast = Toast.makeText(this, "You Redeemed Your Free\n" +
"Sandwich, Thank You\n" +
"For Your Patronage!", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
return;
} else if ((mSandwich1ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())) {
mSandwich1ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Earned 1 Credit\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich1ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich2ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich2ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 2 Credits\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich2ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich3ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich3ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 3 Credits\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich3ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich4ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich4ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 4 Credits\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich4ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich5ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich5ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You're Over Half Way to\n" +
"Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich5ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich6ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich6ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 6 Credits\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich6ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich7ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich7ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 7 Credits\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich7ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich8ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich8ImageView.setImageResource(R.drawable.sbp_sandwich_free);
Toast toast = Toast.makeText(this, "Thanks!\n" +
"You Have Earned 8 Credits\n" +
"Towards a Free Sandwich", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
} else if ((mSandwich8ImageView.getDrawable().getConstantState() == getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()) && mySTagUrl.equals(myTagView.getText().toString())
&& ((mSandwich9ImageView.getDrawable().getConstantState() != getResources().getDrawable(R.drawable.sbp_sandwich_free).getConstantState()))) {
mSandwich9ImageView.setImageResource(R.drawable.sbp_free_sandwich);
Toast toast = Toast.makeText(this, "CONGRATULATION!\n" +
"Your Next\n" +
"Sandwich is on US!", Toast.LENGTH_LONG);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if (v != null) v.setGravity(Gravity.CENTER);
toast.show();
break;
}
}
}
private void setCvisible(){
cflag = true;
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee_free);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("selected", true).commit();
}
private void setCInvisible(){
cflag = false;
mCoffee1ImageView.setImageResource(R.drawable.sbp_coffee);
// save the flag
}
@Override
public void onConfigurationChanged(final Configuration newConfig){
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onBackPressed(){
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
}
@Override
public void onSaveInstanceState(final Bundle outState){
super.onSaveInstanceState(outState);
outState.putInt(NAV_ITEM_ID, mNavItemId);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
final int id = item.getItemId();
if(id== android.support.v7.appcompat.R.id.home){
return mDrawerToggle.onOptionsItemSelected(item);
}
return super.onOptionsItemSelected(item);
}
/**
* -Handles clicks on the navigation menu
* @param menuItem
* @return
*/
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
//Update highlighted item in the navigation menu
menuItem.setChecked(true);
mNavItemId = menuItem.getItemId();
// Time gap after closing the navigation to see animation
mDrawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
@Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
return true;
}
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
resolveIntent(intent);
}
}
答案 0 :(得分:1)
最好的方法是在数据库中存储多个条目。 但是请确保您还能够将您的DrawableID(R.drawable.coffee ..)存储在SharedPreference中。 我认为这不是最好的方式,但它有效。
如果您不想使用数据库(或者您只有几个静态条目),您的解决方案可能是:
SharedPreferences setting = getSharedPreferences("Images", 0);
String image = setting.getString("Image"+(iterator), "empty");
// Set Image to ImageButton
if(!"empty".equals(image))
imageButton.setImageResource(Integer.parseInt(image));
这就是您阅读数据的方式。 以相同的方式保存您的drawableID,然后消失。