0

my Problem is I want to get the User Input from my Edittext android:id="@+id/eingUserAlarm" in my fragment.xml to display in the showing Notification. In my NotificationHelper.java I am using
titelAlarm = getResources(R.id.eingUserAlarm).toString(); String nameAlarm = titelAlarm.getText().toString();
But it cannot resolve findViewById. How can I inflate the layout? It showed erorrs when I tried to. Also FragmentTransaction didn't work because I couldn't write beginTransaction().
My AlarmTimePicker.java:

public class AlarmTimePicker extends DialogFragment {

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    return new TimePickerDialog(getActivity(), (TimePickerDialog.OnTimeSetListener) getParentFragment() , hour, minute, DateFormat.is24HourFormat(getActivity()));
     }
}

NotificationHelper.java: (here the error happens)

public class NotificationHelper extends ContextWrapper {
    public static final String channelID = "channelID";
    public static final String channelName = "Channel Name";
    private NotificationManager mManager;
    private EditText titelAlarm;

    public NotificationHelper(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannel();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChannel() {
        NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);

        getManager().createNotificationChannel(channel);
    }

    public NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }

        return mManager;
    }

    public NotificationCompat.Builder getChannelNotification() {
        titelAlarm = findViewById(R.id.eingUserAlarm);
        String nameAlarm = titelAlarm.getText().toString();
        return new NotificationCompat.Builder(getApplicationContext(), channelID)
                .setContentTitle("Erinnerung")
                .setContentText(nameAlarm)
                .setSmallIcon(R.drawable.ic_one);
    }
}

My Button:

    <Button
    android:id="@+id/button_timepicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Zeit stellen" />

AlertReceiver.java:

public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    NotificationHelper notificationHelper = new NotificationHelper(context);
    NotificationCompat.Builder nb = notificationHelper.getChannelNotification();
    notificationHelper.getManager().notify(1, nb.build());
}

}

AlarmFragment.java:

        final View fraAla = inflater.inflate(R.layout.fragment_alarm, container, false);

    edText = fraAla.findViewById(R.id.eingUserAlarm);
    mTextView = fraAla.findViewById(R.id.textView);

    Button buttonTimePicker = fraAla.findViewById(R.id.button_timepicker);
    buttonTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment timePicker = new AlarmTimePicker();
            timePicker.show(getChildFragmentManager(), "time picker");
        }
    });

    Button buttonCancelAlarm = fraAla.findViewById(R.id.button_cancel);
    buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
    return fraAla;
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);

    updateTimeText(c);
    startAlarm(c);
}

private void updateTimeText(Calendar c) {
    String timeText = "Wir erinnern dich um ";
    timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());

    mTextView.setText(timeText);
}

private void startAlarm(Calendar c) {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE, 1);
    }

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}

private void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    alarmManager.cancel(pendingIntent);
    mTextView.setText("Wir werden nicht stören!");
}

}

Thanks to everyone who is trying to help!

pga009
  • 3
  • 4
  • Show your [mcve], not just some code snippets. –  Feb 01 '19 at 18:24
  • post is updated @jdv – pga009 Feb 01 '19 at 18:32
  • You mention an error but forgot to show the full error here. –  Feb 01 '19 at 18:33
  • titelAlarm = findViewById(R.id.eingUserAlarm); --> error: cannot find symbol method findViewById(int) @jdv – pga009 Feb 01 '19 at 18:48
  • Errors should be provided as formatted, searchable, copyable text in the body of the question. Comments can be delete. Remember that SO is not a threaded forum. Also, you will be presented with a full stacktrace probably. Include all of that. –  Feb 01 '19 at 18:48
  • really dude, maybe if you would read the first sentences of my question you wouldn't even need to ask what is the problem. can't you just answer my question – pga009 Feb 01 '19 at 18:53
  • Really dude, you should read [ask]. Why not make it easy for people to help you? The community _requests_ that all new posts by new users be reviewed so we can guide people to make good choices. –  Feb 01 '19 at 18:56
  • So, for example: https://stackoverflow.com/q/30598871/1531971 (among others) –  Feb 01 '19 at 18:58
  • Really dude this question maybe isn't perfectly written but everyone understands it it is not difficult to answer. But of course there has to be this one person which needs everything to be perfect. I am so *** frustrated I am new af to coding and I could'nt find anything to solve this and that's why I am asking here. – pga009 Feb 01 '19 at 19:01
  • Yes but I need findViewById in my `NotificationCompat.Builder` and how can I inflate there? – pga009 Feb 01 '19 at 19:02
  • You seem to be taking this personally, probably because you are frustrated. However, this is the way it is; no one wants perfection, but meeting the basic requirements of a coding question is not much to ask. Unfortunately, SO is _not_ a good tutorial site, so you are encouraged to debug and research this yourself first. –  Feb 01 '19 at 19:03
  • How am I supposed to inflate in my `NotificationCompat.Builder` when everytime I code inflater it cannot be resolved neither. But wow you really helped me – pga009 Feb 01 '19 at 19:08
  • Sharing Activity contexts like this can be tricky, but the answer is for sure already here somewhere, but you might have to put it together for your specific case. For example: https://stackoverflow.com/q/27351005/1531971 –  Feb 01 '19 at 19:08
  • I mean, one solution that is often used is to supply the data you need in the Intent that raises the notification. –  Feb 01 '19 at 19:10
  • Like really I still think you don't get what I mean. I already tried this but it didn't work just for you I again coded this just to show you. https://prnt.sc/mfpo9x ; https://prnt.sc/mfpoio ; https://prnt.sc/mfpp5a ; Do you get it now? – pga009 Feb 02 '19 at 16:54
  • No one is going to visit those links. Your best bet is to _show_ what you have tried in the question. Most people are just not going to try and figure out how you have done it, but we've all done this sort of thing; it's hard to tell from your description what works for you. I mean, I _think_ you just want data from a UI component pushed into a Fragment or notification handler, and there are typical ways to do this described here and everywhere. Why it isn't working for you is the question, so concentrate on showing that. –  Feb 03 '19 at 15:35

0 Answers0