我的代码遇到问题,我会对一个概念的具体解释感兴趣。基本上,我了解SharedPreferences是如何工作的,我知道键值的概念,但是尽管我在官方文档和其他网站上进行了搜索,但是我无法弄清楚映射的工作方式。
我正在制作一个类似“ M.Mood”的应用。当用户向上或向下滚动时,心情和心情的颜色都在变化。单击历史记录时,新活动中将出现一个新屏幕,该屏幕显示最近7天的值。
这是我的问题:我从HistoryActivity.java中的MainActivity.java中检索了SharedPreferences,尝试对其进行映射,但是TextViews的颜色仍然保持红色。作为Java的初学者,我想就为什么它不显示我在colors.xml资源中注册的颜色发表您的看法。
这是我的MainActivity.java文件:
package com.lepanda.studioneopanda.moodtracker;
// all my imports
public class MainActivity extends AppCompatActivity implements
SimpleGestureDetector.SimpleGestureListener {
ImageButton comment;
ImageButton history;
EditText input;
ImageView iv;
RelativeLayout container;
int tableMood[] = {R.drawable.smiley_sad, R.drawable.smiley_disappointed, R.drawable.smiley_normal, R.drawable.smiley_happy, R.drawable.smiley_super_happy};
int currentMood;
int tableColor[] = {R.color.faded_red, R.color.warm_grey, R.color.cornflower_blue_65, R.color.light_sage, R.color.banana_yellow};
int currentColor;
int currentComColor;
int currentHisColor;
private SimpleGestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.iv);
currentMood = 3;
iv.setImageResource(tableMood[currentMood]);
container = findViewById(R.id.container);
currentColor = 3;
container.setBackgroundResource(tableColor[currentColor]);
comment = findViewById(R.id.comment);
currentComColor = 3;
comment.setBackgroundResource(tableColor[currentComColor]);
history = findViewById(R.id.history);
currentHisColor = 3;
history.setBackgroundResource(tableColor[currentHisColor]);
// Detect touched area
detector = new SimpleGestureDetector(MainActivity.this, this);
//history button click + sharedpref and manage calendar
//Creating the dialog box prompting user for a message
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add comment");
builder.setMessage("Comment vous sentez-vous ?");
input = new EditText(this);
builder.setView(input);
//Create PositiveButton
builder.setPositiveButton("Enregistrer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String txt = input.getText().toString();
Toast.makeText(getApplicationContext(), txt, Toast.LENGTH_LONG).show();
}
});
//Create NegativeButton
builder.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog ad = builder.create();
//Button
comment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.show();
}
});
ImageButton history = findViewById(R.id.history);
history.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startHistory();
}
});
if (getMood() != tableMood[currentMood]){
iv.setImageResource(tableMood[getMood()]);
}
}
private void startHistory() {
startActivity(new Intent(MainActivity.this, HistoryActivity.class));
}
@Override
public boolean dispatchTouchEvent(MotionEvent me) {
// Call onTouchEvent of SimpleGestureFilter class
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
//Detect the swipe gestures and display toast
String showToastMessage = "";
switch (direction) {
case SimpleGestureDetector.SWIPE_UP:
showToastMessage = "You have Swiped Up.";
if (currentMood == 4) {
showToastMessage = "Vous êtes vraiment si content ?";
} else {
currentMood++;
iv.setImageResource(tableMood[currentMood]);
currentColor++;
container.setBackgroundResource(tableColor[currentColor]);
currentComColor++;
comment.setBackgroundResource(tableColor[currentComColor]);
currentHisColor++;
history.setBackgroundResource(tableColor[currentHisColor]);
storeMood(currentMood);
}
break;
case SimpleGestureDetector.SWIPE_DOWN:
showToastMessage = "You have Swiped Down.";
if (currentMood == 0) {
showToastMessage = "Vous êtes vraiment si malheureux ?";
} else {
currentMood--;
iv.setImageResource(tableMood[currentMood]);
currentColor--;
container.setBackgroundResource(tableColor[currentColor]);
currentComColor--;
comment.setBackgroundResource(tableColor[currentComColor]);
currentHisColor--;
history.setBackgroundResource(tableColor[currentHisColor]);
storeMood(currentMood);
}
break;
}
Toast.makeText(this, showToastMessage, Toast.LENGTH_SHORT).show();
}
public void storeMood(int mood) {
SharedPreferences msharedPreferences = getSharedPreferences("monP3", MODE_PRIVATE);
SharedPreferences.Editor mEditor = msharedPreferences.edit();
String dayNumberKey = android.text.format.DateFormat.format("yyyyMMddhms", new java.util.Date()).toString();
mEditor.putInt(dayNumberKey, mood);
mEditor.apply();
}
private int getMood(){
SharedPreferences msharedPreferences = getSharedPreferences("monP3", MODE_PRIVATE);
String dayNumberKey = android.text.format.DateFormat.format("yyyyMMddhms", new java.util.Date()).toString();
int actualMood = msharedPreferences.getInt(dayNumberKey, currentMood);
return actualMood;
}
}
这是我的HistoryActivity.java文件:
package com.lepanda.studioneopanda.moodtracker;
// imports
public class HistoryActivity extends AppCompatActivity {
int tableMood[] = {R.drawable.smiley_sad, R.drawable.smiley_disappointed, R.drawable.smiley_normal, R.drawable.smiley_happy, R.drawable.smiley_super_happy};
int tableColor[] = {R.color.faded_red, R.color.warm_grey, R.color.cornflower_blue_65, R.color.light_sage, R.color.banana_yellow};
int currentColor;
int currenMood;
TextView view1;
TextView view2;
TextView view3;
TextView view4;
TextView view5;
TextView view6;
TextView view7;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
SharedPreferences msharedPreferences = getSharedPreferences("monP3", MODE_PRIVATE);
view1 = findViewById(R.id.textView1);
view2 = findViewById(R.id.textView2);
view3 = findViewById(R.id.textView3);
view4 = findViewById(R.id.textView4);
view5 = findViewById(R.id.textView5);
view6 = findViewById(R.id.textView6);
view7 = findViewById(R.id.textView7);
TextView[] histos = {view1, view2, view3, view4, view5, view6, view7};
Map<String, ?> allEntries = msharedPreferences.getAll();
String dayNumberKey = android.text.format.DateFormat.format("yyyyMMddhms", new java.util.Date()).toString();
int myCol = msharedPreferences.getInt(dayNumberKey, currenMood);
if (myCol == 0){
currentColor = 0;
}
else if (myCol == 1){
currentColor = 1;
}
else if (myCol == 2){
currentColor = 2;
}
else if (myCol == 3){
currentColor = 3;
}
else if (myCol == 4){
currentColor = 4;
}
int i = 0;
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
histos[i].setText(entry.getValue().toString());
i++;
if(i > 6) break;
histos[i].setBackgroundResource(tableColor[currentColor]);
}
}
}
Voilà,我真的很想了解为什么当我单击历史记录按钮时为什么只显示红色。 我敢肯定这有点少,但是正如我所说,我是一个初学者。
谢谢!
解决方案:我删除了尝试在不同位置使用不同值的动态密钥
答案 0 :(得分:0)
您正在使用当前日期和时间作为键来存储颜色,并使用另一个日期和时间来检索颜色。 android.text.format.DateFormat.format("yyyyMMddhms", new java.util.Date()).toString()
的两次调用的返回值将不同。使用相同的值(常数值)存储和检索要使用的数据。