在android studio中读取没有DatePickeView的选定日期 [英] Reading Selected Date without DatePickeView in android studio

查看:31
本文介绍了在android studio中读取没有DatePickeView的选定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Parvanshu Sharma,我是编程初学者,我正在制作一个应用程序,其中我已经完成了在按钮点击一个日期选择器对话框将出现,按钮文本将被设置为用户选择的,目前有 3 个按钮,我什至有一个实时数据库用于在 Firebase 上上传这个日期,但现在我在获取每个唯一日期并上传时遇到问题,我无法获取所选日期.

I am Parvanshu Sharma and I am a beginner to programming, I am making an application in which I have done that on button click a date Picker dialog will appear and the button text will be set to the selected by the user, Currently there are 3 buttons for this and I even have a realtime database for uploading this date on Firebase, but now I am having problems in getting the every unique date and uploading it, I am not able to get the selected date.

正如我的每个计算器问题都有-

And as my every stackoverflow question has-

MainActivity.java(它太大了所以我没有显示导入)

MainActivity.java (Its too big so I haven't shown imports)

    public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, AdapterView.OnItemSelectedListener {
    
        String CurrentDateString;
        TextView mainDate;
        Integer OrderQuantity = 3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button SelectDate1 = findViewById(R.id.SelectDateButton1);
            Button SelectDate2 = findViewById(R.id.SelectDateButton2);
            Button SelectDate3 = findViewById(R.id.SelectDateButton3);
    
            SelectDate1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePicker = new DatePickerFragment();
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate1;
                }
            });
    
            SelectDate2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePicker = new DatePickerFragment();
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate2;
                }
            });
    
            SelectDate3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DialogFragment datePicker = new DatePickerFragment();
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate3;
                }
            });
    
            ArrayAdapter<CharSequence> FoodAdapter = ArrayAdapter.createFromResource(this, R.array.FoodList, android.R.layout.simple_spinner_item);
            FoodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            Spinner SelectItem1 = findViewById(R.id.SelectItem1);
            SelectItem1.setAdapter(FoodAdapter);
            SelectItem1.setOnItemSelectedListener(this);
    
            Spinner SelectItem2 = findViewById(R.id.SelectItem2);
            SelectItem2.setAdapter(FoodAdapter);
            SelectItem2.setOnItemSelectedListener(this);
    
            Spinner SelectItem3 = findViewById(R.id.SelectItem3);
            SelectItem3.setAdapter(FoodAdapter);
            SelectItem3.setOnItemSelectedListener(this);
    
            ArrayAdapter<CharSequence> QuantityAdapter = ArrayAdapter.createFromResource(this, R.array.Quantity, android.R.layout.simple_spinner_item);
            QuantityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            Spinner Quantity1 = findViewById(R.id.SelectQuantity1);
            Quantity1.setAdapter(QuantityAdapter);
            Quantity1.setOnItemSelectedListener(this);
    
            Spinner Quantity2 = findViewById(R.id.SelectQuantity2);
            Quantity2.setAdapter(QuantityAdapter);
            Quantity2.setOnItemSelectedListener(this);
    
            Spinner Quantity3 = findViewById(R.id.SelectQuantity3);
            Quantity3.setAdapter(QuantityAdapter);
            Quantity3.setOnItemSelectedListener(this);
    
            Button DoneButton = findViewById(R.id.DoneButton);
    
            EditText PersonName = findViewById(R.id.PersonName);
            EditText PersonPhone = findViewById(R.id.PersonPhone);
            EditText PersonAddress = findViewById(R.id.PersonAddress);
    
            FirebaseDatabase database = FirebaseDatabase.getInstance();
    
            DoneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatabaseReference Name = database.getReference(PersonPhone.getText().toString() + "/Name");
                    Name.setValue(PersonName.getText().toString());
    
                    DatabaseReference Phone = database.getReference(PersonPhone.getText().toString() + "/Phone");
                    Phone.setValue(PersonPhone.getText().toString());
    
                    DatabaseReference Address = database.getReference(PersonPhone.getText().toString() + "/Address");
                    Address.setValue(PersonAddress.getText().toString());
    
                    if (Quantity1.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    if (Quantity2.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    if (Quantity3.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    DatabaseReference OrderQuantities = database.getReference(PersonPhone.getText().toString()+"/OrderQuantity");
                    OrderQuantities.setValue(OrderQuantity);
    
                    if (Quantity1.getSelectedItem().toString() != "0") {
                        //I want some solution HERE
                    }
                }
            });
        }
    
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
    
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            String CurrentDateString = format.format(c.getTime());
            mainDate.setText(CurrentDateString);
        }
    
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        }
    
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    }```
    
    and here goes my activity_main.xml
    ```<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/NameTextView"
                    android:layout_width="93dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20sp"
                    android:layout_marginLeft="20sp"
                    android:layout_marginTop="10sp"
                    android:text="Name"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/PersonName"
                    android:layout_width="320dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="20sp"
                    android:layout_marginLeft="20sp"
                    android:ems="10"
                    android:hint="Name"
                    android:inputType="textPersonName" />
    
                <TextView
                    android:id="@+id/PhoneTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:text="Mobile Number"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/PersonPhone"
                    android:layout_width="325dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:ems="10"
                    android:hint="Mobile number"
                    android:inputType="phone" />
    
                <TextView
                    android:id="@+id/AddressTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:text="Address"
                    android:textColor="@color/black"
                    android:textSize="28sp"
                    android:textStyle="bold" />
    
                <EditText
                    android:id="@+id/PersonAddress"
                    android:layout_width="328dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20sp"
                    android:ems="10"
                    android:hint="Delivery Address"
                    android:inputType="textPostalAddress" />
    
                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="20sp"
                    android:text="                         Order Details"
                    android:textColor="@color/black"
                    android:textSize="20sp"
                    android:textStyle="bold" />
    
                <TableLayout
    
                    android:id="@+id/MainTable"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:stretchColumns="0,1,2"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000"
    
                        >
    
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_column="0"
                            android:layout_margin="1dp"
                            android:background="#FFFFFF"
                            android:gravity="center"
                            android:text=" Date "
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_column="1"
                            android:layout_margin="1dp"
                            android:background="#FFFFFF"
                            android:gravity="center"
                            android:text="Item"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_column="2"
                            android:layout_margin="1dp"
                            android:background="#FFFFFF"
                            android:gravity="center"
                            android:text="Quantity"
                            android:textAppearance="?android:attr/textAppearanceLarge"
                            android:textStyle="bold" />
                    </TableRow>
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000">
    
                        <Button
                            android:id="@+id/SelectDateButton1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Select" />
    
                        <Spinner
                            android:id="@+id/SelectItem1"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:layout_marginStart="3dp"
                            android:layout_marginLeft="3dp"
                            android:layout_marginEnd="2dp"
                            android:layout_marginRight="2dp"
                            android:background="@color/white"
                            android:gravity="center"
                            android:text="Select Item" />
    
                        <Spinner
                            android:id="@+id/SelectQuantity1"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:background="@color/white"
                            android:gravity="center" />
                    </TableRow>
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000">
    
                        <Button
                            android:id="@+id/SelectDateButton2"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Select" />
    
                        <Spinner
                            android:id="@+id/SelectItem2"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:layout_marginStart="3dp"
                            android:layout_marginLeft="3dp"
                            android:layout_marginEnd="2dp"
                            android:layout_marginRight="2dp"
                            android:background="@color/white"
                            android:gravity="center"
                            android:text="Select Item" />
    
                        <Spinner
                            android:id="@+id/SelectQuantity2"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:background="@color/white"
                            android:gravity="center" />
    
                    </TableRow>
    
                    <TableRow
                        android:layout_width="fill_parent"
                        android:layout_height="0dp"
                        android:layout_margin="1dp"
                        android:layout_weight="1"
                        android:background="#000000">
    
                        <Button
                            android:id="@+id/SelectDateButton3"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Select" />
    
                        <Spinner
                            android:id="@+id/SelectItem3"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:layout_marginStart="3dp"
                            android:layout_marginLeft="3dp"
                            android:layout_marginEnd="2dp"
                            android:layout_marginRight="2dp"
                            android:background="@color/white"
                            android:gravity="center" />
    
                        <Spinner
                            android:id="@+id/SelectQuantity3"
                            android:layout_width="40dp"
                            android:layout_height="43dp"
                            android:background="@color/white"
                            android:gravity="center" />
    
                    </TableRow>
    
                </TableLayout>
    
                <Button
                    android:id="@+id/DoneButton"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Done" />
    
            </LinearLayout>
        </ScrollView>
    
    
    </androidx.constraintlayout.widget.ConstraintLayout>

我还有一个用于创建 DatePickerDialog 的类,我学习和实现如何制作 DatePickerDialog 的来源是这里

and I have one more class for creating DatePickerDialog, the source from where I learnt and implemented that how to make DatePickerDialog is Here

在这里 - (DatePickerFragment.java)

which goes here- (DatePickerFragment.java)

    public class DatePickerFragment extends DialogFragment {
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
            return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
        }
    }

推荐答案

尝试使用接口.您将拥有主要活动中的所有 3 个日期,现在您可以对它们进行任何操作

Tried using an interface. You will have all 3 dates in your main activity, now you can do whatever with them

public class DatePickerFragment extends DialogFragment {

    private static applyDate mInterface;
    private static int buttonNumberHere;

    public interface applyDate {
        void setDate(int selectedYear, int selectedMonth, int selectedDay, int buttonNumber);
    }

    public static DatePickerFragment newInstance(int buttonNumber, applyDate context) 
    {
    
        DatePickerFragment fragment = new DatePickerFragment();
        mInterface = ((applyDate) context);
        buttonNumberHere = buttonNumber;
        return fragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getContext(), datePickerListener, year, month, day);
    }
    

    private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int selectedYear,
                          int selectedMonth, int selectedDay) {
            mInterface.setDate(selectedYear, selectedMonth, selectedDay, buttonNumberHere);
        }
    };

在主活动中

public class MainActivity extends AppCompatActivity implements applyDate, AdapterView.OnItemSelectedListener {
    
        String CurrentDateString;
        TextView mainDate;// Idk what is this
        Integer OrderQuantity = 3;
        String itemOneDate;
        String itemTwoDate;
        String itemThreeDate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button SelectDate1 = findViewById(R.id.SelectDateButton1);
            Button SelectDate2 = findViewById(R.id.SelectDateButton2);
            Button SelectDate3 = findViewById(R.id.SelectDateButton3);
    
            SelectDate1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatePickerFragment datePicker = DatePickerFragment.newInstance(1, MainActivity.this);
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate1;
                }
            });
    
            SelectDate2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatePickerFragment datePicker = DatePickerFragment.newInstance(2, MainActivity.this);
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate2;
                }
            });
    
            SelectDate3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatePickerFragment datePicker = DatePickerFragment.newInstance(3, MainActivity.this);
                    datePicker.show(getSupportFragmentManager(), "Pick item order date");
    
                    mainDate = SelectDate3;
                }
            });
    
            ArrayAdapter<CharSequence> FoodAdapter = ArrayAdapter.createFromResource(this, R.array.FoodList, android.R.layout.simple_spinner_item);
            FoodAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            Spinner SelectItem1 = findViewById(R.id.SelectItem1);
            SelectItem1.setAdapter(FoodAdapter);
            SelectItem1.setOnItemSelectedListener(this);
    
            Spinner SelectItem2 = findViewById(R.id.SelectItem2);
            SelectItem2.setAdapter(FoodAdapter);
            SelectItem2.setOnItemSelectedListener(this);
    
            Spinner SelectItem3 = findViewById(R.id.SelectItem3);
            SelectItem3.setAdapter(FoodAdapter);
            SelectItem3.setOnItemSelectedListener(this);
    
            ArrayAdapter<CharSequence> QuantityAdapter = ArrayAdapter.createFromResource(this, R.array.Quantity, android.R.layout.simple_spinner_item);
            QuantityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
            Spinner Quantity1 = findViewById(R.id.SelectQuantity1);
            Quantity1.setAdapter(QuantityAdapter);
            Quantity1.setOnItemSelectedListener(this);
    
            Spinner Quantity2 = findViewById(R.id.SelectQuantity2);
            Quantity2.setAdapter(QuantityAdapter);
            Quantity2.setOnItemSelectedListener(this);
    
            Spinner Quantity3 = findViewById(R.id.SelectQuantity3);
            Quantity3.setAdapter(QuantityAdapter);
            Quantity3.setOnItemSelectedListener(this);
    
            Button DoneButton = findViewById(R.id.DoneButton);
    
            EditText PersonName = findViewById(R.id.PersonName);
            EditText PersonPhone = findViewById(R.id.PersonPhone);
            EditText PersonAddress = findViewById(R.id.PersonAddress);
    
            FirebaseDatabase database = FirebaseDatabase.getInstance();
    
            DoneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //Idk how to set date but now you have all three dates

                    DatabaseReference dateOne = database.getReference(itemOneDate + "/DateOne");
                    dateOne.setValue(itemOneDate);

                    DatabaseReference dateTwo = database.getReference(itemOneDate + "/DateTwo");
                    dateTwo.setValue(itemTwoDate);

                    DatabaseReference dateThree = database.getReference(itemThreeDate + "/DateThree");
                    dateThree.setValue(itemThreeDate);

                    DatabaseReference Name = database.getReference(PersonPhone.getText().toString() + "/Name");
                    Name.setValue(PersonName.getText().toString());
    
                    DatabaseReference Phone = database.getReference(PersonPhone.getText().toString() + "/Phone");
                    Phone.setValue(PersonPhone.getText().toString());
    
                    DatabaseReference Address = database.getReference(PersonPhone.getText().toString() + "/Address");
                    Address.setValue(PersonAddress.getText().toString());
    
                    if (Quantity1.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    if (Quantity2.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    if (Quantity3.getSelectedItem().toString().equals("0")) {
                        OrderQuantity -= 1;
                    }
    
                    DatabaseReference OrderQuantities = database.getReference(PersonPhone.getText().toString()+"/OrderQuantity");
                    OrderQuantities.setValue(OrderQuantity);
    
                    if (Quantity1.getSelectedItem().toString() != "0") {
                        //I want some solution HERE
                    }
                }
            });
        }

        @Override
        public void setDate(int selectedYear, int selectedMonth, int selectedDay, int buttonNumber){
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, selectedYear);
            c.set(Calendar.MONTH, selectedMonth);
            c.set(Calendar.DAY_OF_MONTH, selectedDay);

            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            String CurrentDateString = format.format(c.getTime());
            mainDate.setText(CurrentDateString);

            if (buttonNumber == 1){
                itemOneDate = CurrentDateString;
            }
            else if (buttonNumber == 2){
                itemTwoDate = CurrentDateString;
            }
            else if (buttonNumber == 3){
                itemThreeDate = CurrentDateString;
            }
        }
}

这篇关于在android studio中读取没有DatePickeView的选定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆