I cannot pass and retrieve intent boolean extra
我试图让我的代码通过boolean额外的意图,但我失败了。我调试并观察到,我的活动"b"分配EDOCX1的值(0)作为
以下是我所说的活动A:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | package com.example.recepinanc.whenfreedeneme; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; /** * Created by recepinanc on 10/10/15. */ public class WorksActivity extends AppCompatActivity { private Boolean isAdded; private ListView todoListView; private ArrayList<Work> works; private Context mContext; private int year; private int month; private int day; private int hour; private int minute; private String title; private String category; //TODO Add school social todo overall buttons. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.works_activity_layout); Log.i("WorksActivity","works activity'te"); mContext = getApplicationContext(); works = new ArrayList<Work>(); } @Override protected void onResume() { super.onResume(); isAdded = getIntent().getBooleanExtra(AddWorkActivity.EXTRA_IS_ADDED, false); year = getIntent().getIntExtra(AddWorkActivity.EXTRA_YEAR, 0); month = getIntent().getIntExtra(AddWorkActivity.EXTRA_MONTH, 0); day = getIntent().getIntExtra(AddWorkActivity.EXTRA_DAY,0); hour = getIntent().getIntExtra(AddWorkActivity.EXTRA_HOUR,0); minute = getIntent().getIntExtra(AddWorkActivity.EXTRA_MINUTE,0); title = getIntent().getStringExtra(AddWorkActivity.EXTRA_TITLE); category = getIntent().getStringExtra(AddWorkActivity.EXTRA_CATEGORY); //TODO isAdded True pass edilemiyor Log.i("is_Added",isAdded.toString()); if (isAdded) { //TODO examine the true isAdded case. Log.i("isAddedTrue",isAdded.toString()); Work work = new Work(); Log.i("isAddedTrue","work creationdan sonra"); work.setmTitle(title); work.setmCategory(category); work.setmYear(year); work.setmMonth(monthConverter(month)); work.setmDay(day); work.setmHour(hour); work.setmMinute(minuteConverter(minute)); Log.i("isAddedTrue","work.setlerden sonra"); //works.add(work); updateArray(works, work); Log.i("isAddedTrue","work.array addden sonra"); Toast.makeText(WorksActivity.this,"NEWBIE ADDED.", Toast.LENGTH_SHORT).show(); isAdded = false; Log.i("isAddedTrue","work is added false setden sivvaliiiiiim sonra"); } Log.i("isAddedTrue","listview found"); todoListView = (ListView) findViewById(R.id.listView); Log.i("isAddedTrue","listview founddan sonra"); todoListView.setAdapter(new WorkAdapter(getApplicationContext(), works)); Log.i("isAddedTrue","listAdapter setten sonra"); } public void updateArray(ArrayList<Work> works, Work work) { //Add a new item add its date and update the array works.add(work); Toast.makeText(WorksActivity.this,"A new work is created!" + " " + work.getmTitle() + " " + work.getmCategory() + " " + work.getmDay() + " " + work.getmMonth() + " " + work.getmYear() + " " + work.getmHour() + " " + work.getmMinute(), Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_action: Intent intent = new Intent(WorksActivity.this, AddWorkActivity.class); Log.i("WorksActivity","Created intent"); startActivity(intent); Log.i("WorksActivity","Started Activity"); break; default: Log.e("WorksActivity","INVALID BUTTON"); break; } return true; } public String monthConverter(int month) { switch (month + 1) { case 1: return"January"; case 2: return"February"; case 3: return"March"; case 4: return"April"; case 5: return"May"; case 6: return"June"; case 7: return"July"; case 8: return"August"; case 9: return"September"; case 10: return"October"; case 11: return"November"; case 12: return"December"; default: return"UNKNOWN"; } } public String minuteConverter(int minute) { if (minute / 10 <= 0) return"0" + minute; else return minute +""; } } |
这是我的第二次-B-活动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | package com.example.recepinanc.whenfreedeneme; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.TimePicker; import java.util.Calendar; /** * Created by recepinanc on 11/10/15. */ public class AddWorkActivity extends AppCompatActivity { public static final String EXTRA_TITLE ="title"; public static final String EXTRA_CATEGORY ="category"; public static final String EXTRA_YEAR ="year"; public static final String EXTRA_MONTH ="month"; public static final String EXTRA_DAY ="day"; public static final String EXTRA_HOUR ="hour"; public static final String EXTRA_MINUTE ="minute"; public static final String EXTRA_IS_ADDED ="isAdded"; static final int DATE_DIALOG_ID = 999; static final int TIME_DIALOG_ID = 111; private EditText title; private RadioGroup categoryRadioGroup; private TextView datePicker; private TextView timePicker; private int year; private int month; private int day; private int hour; private int minute; private boolean is24HourView = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_work_acitivity_layout); Log.i("AddWorkActivity","addwork'te"); title = (EditText) findViewById(R.id.todoTitle_editText); categoryRadioGroup = (RadioGroup) findViewById(R.id.category_radioGroup); datePicker = (TextView) findViewById(R.id.dateSpinner_textView); timePicker = (TextView) findViewById(R.id.timeSpinner_textView); Calendar calendar = Calendar.getInstance(); year = calendar.get(Calendar.YEAR); month = calendar.get(Calendar.MONTH); day = calendar.get(Calendar.DAY_OF_MONTH); hour = calendar.get(Calendar.HOUR_OF_DAY); minute = calendar.get(Calendar.MINUTE); setDateOnView(); datePicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //todo call the create dialog method here. showDialog(DATE_DIALOG_ID); } }); timePicker.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //todo call create dialog for time picker. showDialog(TIME_DIALOG_ID); } }); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID: //set date as the current date return new DatePickerDialog(this, dateSetListener, year, month, day); case TIME_DIALOG_ID: //set time as the current time return new TimePickerDialog(this, timeSetListener, hour, minute, is24HourView); } return null; } private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { //assign selected values year = selectedYear; month = selectedMonth; day = selectedDay; //update the textView to selected date setDateAsText(datePicker); } }; private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { //assign the selected time hour = selectedHour; minute = selectedMinute; setTimeAsText(timePicker); } }; private void setDateOnView() { setDateAsText(datePicker); setTimeAsText(timePicker); } @Override public boolean onOptionsItemSelected(MenuItem item) { // handle item selection switch (item.getItemId()) { case R.id.cancel_action: //TODO Make this return to its activity finish(); break; case R.id.save_action: //TODO Get info and pass it to the activity's updateArrayMethodString category =""; String category =""; int checkedRadioID = categoryRadioGroup.getCheckedRadioButtonId(); switch (checkedRadioID) { case R.id.todo_radioButton: category ="To-Do"; break; case R.id.school_radioButton: category ="School"; break; case R.id.social_radioButton: category ="Social"; break; default: Log.e("WorksActivity","Unexpected RadioButton ID"); break; } //pass data via intent Intent intent = new Intent(getBaseContext(), WorksActivity.class); intent.putExtra(EXTRA_TITLE, title.getText().toString()); intent.putExtra(EXTRA_CATEGORY, category); intent.putExtra(EXTRA_HOUR, hour); intent.putExtra(EXTRA_MINUTE, minute); intent.putExtra(EXTRA_YEAR, year); intent.putExtra(EXTRA_MONTH, month); intent.putExtra(EXTRA_DAY, day); intent.putExtra(EXTRA_IS_ADDED, true); Log.i("Addworkacitivty","add work activity intent"); startActivity(intent); break; default: break; } return true; } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_add_activity, menu); return true; } public String monthConverter(int month) { switch (month + 1) { case 1: return"JANUARY"; case 2: return"FEBRUARY"; case 3: return"MARCH"; case 4: return"APRIL"; case 5: return"MAYY"; case 6: return"JUNE"; case 7: return"JULY"; case 8: return"AUGUST"; case 9: return"SEPTEMBER"; case 10: return"OCTOBER"; case 11: return"NOVEMBER"; case 12: return"DECEMBER"; default: return"UNKNOWN"; } } public String minuteConverter(int minute) { if (minute / 10 <= 0) return"0" + minute; else return minute +""; } public void setDateAsText(TextView datePicker) { datePicker.setText(new StringBuilder().append(day).append("") .append(monthConverter(month)).append("").append(year)); } public void setTimeAsText(TextView timePicker) { timePicker.setText(new StringBuilder().append(hour).append(":").append(minuteConverter(minute))); } } |
这是我的适配器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | package com.example.recepinanc.whenfreedeneme; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; /** * Created by recepinanc on 11/10/15. */ public class WorkAdapter extends BaseAdapter { private Context mContext; ArrayList<Work> works; public WorkAdapter(Context applicationContext, ArrayList<Work> works) { this.mContext = applicationContext; this.works = works; } @Override public int getCount() { return works.size(); } @Override public Object getItem(int position) { return works.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //Upgrade this to view holder. View v; if (convertView == null) { LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = layoutInflater.inflate(R.layout.work_item_layout, parent, false); TextView title = (TextView) v.findViewById(R.id.title_textView); TextView category = (TextView) v.findViewById(R.id.category_textView); TextView date = (TextView) v.findViewById(R.id.date_textView); TextView time = (TextView) v.findViewById(R.id.time_textView); Work work = works.get(position); titleResizer(title, work.getmTitle()); category.setText(work.getmCategory()); date.setText(new StringBuilder().append(work.getmDay()).append("") .append(work.getmMonth()).append("").append(work.getmYear())); time.setText(new StringBuilder().append(work.getmHour()) .append(":").append(work.getmMinute())); } else { v = convertView; } return v; } public void titleResizer(TextView title, String titleText){ if (titleText.length() > 25) title.setTextSize(12); else if (titleText.length() > 20) title.setTextSize(15); else if (titleText.length() > 15 ) title.setTextSize(18); else if (titleText.length() > 10 ) title.setTextSize(20); else title.setTextSize(25); title.setText(titleText); } } |
我已将原始评论移至答案,以便您将问题标记为已解决。;)
注意,getBoolean返回的是Boolean(基元类型)而不是Boolean。
来自文档:
public boolean getBooleanExtra (String name, boolean defaultValue)
Added in API level 1 Retrieve extended data from the intent.
Parameters name The name of the desired item. defaultValue the value
to be returned if no value of the desired type is stored with the
given name. Returns the value of an item that previously added with
putExtra() or the default value if none was found. See Also
putExtra(String, boolean)
变化
到
1 | private boolean isAdded; |