如何在我的应用程序中使用对话框?

时间:2018-03-28 15:19:55

标签: java android dialog realm

我正在尝试在我的对话框中使用我的应用程序的日期选择器,但是现在我只能输入事件名称,但我也希望能够输入日期以及日期的描述事件。到目前为止,我将代码包括在内。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ButterKnife.bind(this);
        timelineName = getIntent().getExtras().getString("timelineNameExtra");
        timelineId = getIntent().getExtras().getInt("timelineIdExtra");
        this.setTitle(timelineName);
        FloatingActionButton fab = findViewById(R.id.new_event_fab);
        fab.setOnClickListener((view) -> {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");


            new MaterialDialog.Builder(this)
                    .title("New event")
                    .content("Event name")
                    .input("", "", false, (dialog, input) -> {
                        Date d = new Date();
                        try {
                            d = sdf.parse("10/10/2017");
                        } catch (ParseException e) {
                            //
                        }
                        createEvent(input.toString(), "test desc", d);
                    })
                    .negativeText("Cancel")
                    .show();
        });
        setupRecyclerView();
    }

    private void setupRecyclerView() {
        Realm realm = Realm.getDefaultInstance();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(
                new EventAdapter(
                        realm.where(Event.class)
                                .equalTo("timelines.timelineId", timelineId)
                                .sort("eventDate")
                                .findAllAsync())
        );
        recyclerView.setHasFixedSize(true);
    }

    public static void createEvent(String eventName, String eventDesc, Date eventDate) {
        Log.d("DEBUG", timelineName);

        if (timelineName.isEmpty()) {
            return;
        }

        Realm realm = Realm.getDefaultInstance();
        realm.executeTransaction((r) -> {
            Timeline timeline = realm.where(Timeline.class).equalTo("timelineId", timelineId).findFirst();

            Number currentIdNum = r.where(Event.class).max("eventId");

            int nextId;
            if(currentIdNum == null) {
                nextId = 1;
            } else {
                nextId = currentIdNum.intValue() + 1;
            }

            // Create new department timeline
            Event event = new Event();
            event.setEventId(nextId);
            event.setEventDate(eventDate);
            event.setEventDesc(eventDesc);
            event.setEventName(eventName);
            timeline.addEvent(event);
            // Insert
            r.insertOrUpdate(timeline);
        });
        realm.close();
    }

}

0 个答案:

没有答案