我有一个带有ImageView的Activity,它可以单击。当我单击ImageView时,会出现一个对话框,其中包含3个选项。当我在对话框中选择3位置时,我将调用TimePicker,在其中指定时间并将其显示在TextView中。所有必须填写在数据库中的值都可以正常工作,它们会根据需要更新和更改。
问题在于,在TimePicker中选择时间后,它会正确显示在TextView中,并在数据库中输入了所需的数字,但是当我重新启动应用程序时,我需要在TextView中再次显示相同的数字-但这不会发生。 FireStore中的值-已更改,但重新启动时在TextView中的值-否。
代码:
public class ZoneOne extends AppCompatActivity implements View.OnClickListener{
private ImageView mSceneOne, mSceneTwo;
private TextView mTextOne, mTextTwo;
public static final String KEY_SCENE_ONE = "scene_one";
public static final String KEY_SCENE_TWO = "scene_two";
public static final String KEY_TIME_ONE = "time_one";
public static final String KEY_TIME_TWO = "time_two";
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private DocumentReference tables = db.collection("New Collection").document("New Document");
private ArrayList mSelectedItems;
private AlertDialog.Builder builder;
private TimePickerDialog time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zone_one);
mSceneOne = findViewById(R.id.scene_one);
mSceneTwo = findViewById(R.id.scene_two);
mTextOne = findViewById(R.id.time_scene_one);
mTextTwo = findViewById(R.id.time_scene_two);
mTextOne.setVisibility(View.GONE);
mTextTwo.setVisibility(View.GONE);
mSceneOne.setOnClickListener(this);
mSceneTwo.setOnClickListener(this);
tables.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
String scene_one = documentSnapshot.getString("scene_one");
String one = documentSnapshot.getString("time_one");
switch (scene_one) {
case "0":
mTableOne.setImageResource(R.drawable.one_green);
break;
case "1":
mTableOne.setImageResource(R.drawable.one_red);
break;
default:
mTableOne.setImageResource(R.drawable.one_grey);
mTextOne.setText(one);
mTextOne.setVisibility(View.VISIBLE);
break;
}
String seat_two = documentSnapshot.getString("seat_two");
String time_for_two = documentSnapshot.getString("time_two");
switch (seat_two) {
case "0":
mTableTwo.setImageResource(R.drawable.two_green);
break;
case "1":
mTableTwo.setImageResource(R.drawable.two_red);
break;
default:
mTableTwo.setImageResource(R.drawable.two_grey);
mTextTwo.setVisibility(View.VISIBLE);
mTextOne.setText(time_for_two);
break;
}
} else {
Map<String, Object> allData = new HashMap<>();
allData.put(KEY_SCENE_ONE, "0");
allData.put(KEY_SCENE_TWO, "0");
allData.put(KEY_TIME_ONE, "");
allData.put(KEY_TIME_TWO, "");
tables.set(allData);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
private void createDialog(int state_one, int state_two, int state_three, String key, ImageView view, TextView time, String time_key) {
mSelectedItems = new ArrayList();
builder = new AlertDialog.Builder(this);
final int finalState_one = state_one;
final int finalState_two = state_two;
final int finalState_three = state_three;
final String KEY = key;
final ImageView V = view;
final TextView Time = time;
final String TIME = time_key;
final Map<String, Object> note = new HashMap<>();
builder.setTitle("Please choose!")
.setSingleChoiceItems(R.array.tables, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
mSelectedItems.add(which);
break;
case 1:
mSelectedItems.add(which);
break;
case 2:
mSelectedItems.add(which);
break;
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (mSelectedItems.contains(0)){
V.setImageResource(finalState_one);
note.put(KEY, "0");
tables.update(note);
Time.setVisibility(View.INVISIBLE);
}else if (mSelectedItems.contains(1)){
V.setImageResource(finalState_two);
note.put(KEY, "1");
tables.update(note);
Time.setVisibility(View.INVISIBLE);
}else if (mSelectedItems.contains(2)){
V.setImageResource(finalState_three);
note.put(KEY, "2");
tables.update(note);
Time.setVisibility(View.VISIBLE);
showTimePicker(Time, TIME);
}
dialog.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
}).create();
AlertDialog dialog = builder.create();
dialog.show();
}
private void showTimePicker(TextView view, String key_time) {
final TextView V = view;
final String KEY_TIME = key_time;
final Map<String, Object> note_time = new HashMap<>();
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
time = new TimePickerDialog(ZoneOne.this, new TimePickerDialog.OnTimeSetListener() {
@SuppressLint("SetTextI18n")
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
V.setText(hourOfDay + ":" + minute);
String time = hourOfDay + ":" + minute;
note_time.put(KEY_TIME, time);
tables.update(note_time);
}
}, hour, minute, true );
time.show();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.table_one:
createDialog(R.drawable.one_green, R.drawable.one_red, R.drawable.one_grey, KEY_SCENE_ONE, mSceneOne, mTextOne, KEY_TIME_ONE);
break;
case R.id.table_two:
createDialog(R.drawable.two_green, R.drawable.two_red, R.drawable.two_grey, KEY_SCENE_TWO, mSceneTwo, mTextTwo, KEY_TIME_TWO);
break;
}
}
}
答案 0 :(得分:1)
使用看起来像您的switch语句时
switch (scene_one) {
case "0":
mTableOne.setImageResource(R.drawable.one_green);
break;
case "1":
mTableOne.setImageResource(R.drawable.one_red);
break;
default:
mTableOne.setImageResource(R.drawable.one_grey);
mTextOne.setText(one);
mTextOne.setVisibility(View.VISIBLE);
break;
}
这意味着您仅在特定情况下将字符串one
设置为TExtView
,即scene_one
不是 {{1} }或0
。正如我在您的数据库中所看到的那样,您的1
属性拥有scene_one
的值,这意味着将永远无法达到0
。如果要设置default case
,无论哪种情况,都需要在每种情况下复制这些代码行,TextView
和case "0"
。
答案 1 :(得分:1)
tables.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists()) {
String scene_one = documentSnapshot.getString("scene_one");
String one = documentSnapshot.getString("time_one");
mTextOne.setText(one);
mTextOne.setVisibility(View.VISIBLE);
//Try to set the text here above switch and see if it works
switch (scene_one) {
case "0":
mTableOne.setImageResource(R.drawable.one_green);
break;
case "1":
mTableOne.setImageResource(R.drawable.one_red);
break;
default:
mTableOne.setImageResource(R.drawable.one_grey);
break;
}
String seat_two = documentSnapshot.getString("seat_two");
String time_for_two = documentSnapshot.getString("time_two");
switch (seat_two) {
case "0":
mTableTwo.setImageResource(R.drawable.two_green);
break;
case "1":
mTableTwo.setImageResource(R.drawable.two_red);
break;
default:
mTableTwo.setImageResource(R.drawable.two_grey);
mTextTwo.setVisibility(View.VISIBLE);
mTextOne.setText(time_for_two);
break;
}
} else {
Map<String, Object> allData = new HashMap<>();
allData.put(KEY_SCENE_ONE, "0");
allData.put(KEY_SCENE_TWO, "0");
allData.put(KEY_TIME_ONE, "");
allData.put(KEY_TIME_TWO, "");
tables.set(allData);
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});