I'm writing an alarm app. When I save the alarm, it's displayed in a ListView in my MainActivity.
When I want to click the row and edit that reminder, the getIntent().getExtras()
is always null, so I have no value to go back to
This is my MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(MainActivity.this, AlarmChooser.class));
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if(row==null || row.getTag()==null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResource,null);
holder=new ViewHolder();
holder.mTitle=(TextView)row.findViewById(R.id.name);
holder.mDate=(TextView)row.findViewById(R.id.dateText);
holder.mTime=(TextView)row.findViewById(R.id.timeText);
row.setTag(holder);
}else{
holder = (ViewHolder)row.getTag();
}
holder.myAlarm = getItem(position);
holder.mTitle.setText(holder.myAlarm.getTitle());
holder.mDate.setText(holder.myAlarm.getRecordDate());
holder.mTime.setText(holder.myAlarm.getRecordTime());
final ViewHolder finalHolder = holder;
holder.mTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
String titles = extras.getString("title");
int day = extras.getInt("day");
int month = extras.getInt("month");
int year = extras.getInt("year");
int minute = extras.getInt("minute");
int hour = extras.getInt("hour");
String typeee = cursor.getString(cursor.getColumnIndex(Constants.COLUMN_TYPE));
Log.e("title", titles);
Log.e("year", "value=" + year);
Log.e("month", "value=" + month);
Log.e("day", "value=" + day);
Log.e("hour", "value=" + hour);
Log.e("minute", "value=" + minute);
Log.e("type", typeee);
Class next = null;
switch (typeee) {
case "TimeDateRem":
next = TimeDateRem.class;
break;
case "BirthdayRem":
next = BirthdayRem.class;
break;
case "HolidayReminderKuwait":
next = HolidayReminderKuwait.class;
break;
case "HolidayReminderLebanon":
next = HolidayReminderLebanon.class;
break;
case "HolidayReminderSaudiArabia":
next = HolidayReminderSaudiArabia.class;
break;
case "HolidayReminderUAE":
next = HolidayReminderUAE.class;
break;
}
if (next != null) {
Intent return_intent = new Intent(MainActivity.this, next);
return_intent.putExtra("alarm_title", titles);
return_intent.putExtra("alarm_month", month);
return_intent.putExtra("alarm_year", year);
return_intent.putExtra("alarm_day", day);
return_intent.putExtra("alarm_hour", hour);
return_intent.putExtra("alarm_minute", minute);
// next_intent.putExtra("alarm_content", contenttt);
return_intent.putExtra("FROM_ACTIVITY", "MainActivity");
setResult(RESULT_OK, return_intent);
finish();
} else {
Log.e("ERROR", "ERROR");
}
});
return row;
}
}
class ViewHolder{
MyAlarm myAlarm;
TextView mTitle;
TextView mDate;
TextView mTime;
TextView mContent;
TextView mID;
}
}
And this is my TimeDateRem, which is an alarm where im tring to get the data from - unsucessfully
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_view);
private void setAlarm(Calendar targetCal) {
Intent intent_ForResult = new Intent(TimeDateRem.this, MainActivity.class);
intent_ForResult.putExtra("title", title.getText().toString().trim());
intent_ForResult.putExtra("day", pickerDate.getDayOfMonth());
intent_ForResult.putExtra("month", pickerDate.getMonth());
intent_ForResult.putExtra("year", pickerDate.getYear());
intent_ForResult.putExtra("hour", pickerTime.getCurrentHour());
intent_ForResult.putExtra("minute", pickerTime.getCurrentMinute());
intent_ForResult.putExtra("type", "TimeDateRem");
startActivityForResult(intent_ForResult, REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST) {
if (resultCode == RESULT_OK) {
Bundle extras = getIntent().getExtras();
String previousActivity = extras.getString("FROM_ACTIVITY");
if (previousActivity.equals("MainActivity")) {
String titles = extras.getString("alarm_title");
int year_intent = extras.getInt("alarm_year");
int month_intent = extras.getInt("alarm_month");
int day_intent = extras.getInt("alarm_day");
int minute_intent = extras.getInt("alarm_minute");
int hour_intent = extras.getInt("alarm_hour");
String year = String.valueOf(year_intent);
String month = String.valueOf(month_intent);
String day = String.valueOf(day_intent);
String minute = String.valueOf(minute_intent);
String hour = String.valueOf(hour_intent);
title.setText(titles);
pickerTime.setCurrentHour(Integer.parseInt(hour));
pickerTime.setCurrentMinute(Integer.parseInt(minute));
pickerDate.init(
Integer.parseInt(year),
Integer.parseInt(month),
Integer.parseInt(day),
null
);
String result = data.getStringExtra("returnData");
Log.e("RESULTRESULRESULT ", result);
}
}
}
}
What I'm doing is getting the data from the TimeDateRem, sending it to main Activity and then sending it back to TimeDateRem.
If I want to edit the saved reminder, but bundle somthing = getIntent().getExtras()
always returns null...
You're using getIntent()
instead of the Intent data
... So, that's not the intent that you have passed via startActivityForResult
For example
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
String previousActivity = extras.getString("FROM_ACTIVITY");
Also, if getIntent().getExtras()
works outside of the click event, then make it a field.
private Bundle extras;
public void onCreate(Bundle savedInstanceState) {
....
this.extras = getIntent().getExtras();
}
And use those in the onClick
Ideally, though, I'd suggest not defining the entire adapter within the Activity code and instead create a separate class file. In which case, you cannot use the Activity Intent within the adapter code