Newer
Older
CosmosClient / app / src / main / java / com / example / cosmosclient / views / AddRequestActivity.java
package com.example.cosmosclient.views;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.cosmosclient.R;
import com.example.cosmosclient.entities.Request;
import com.example.cosmosclient.yolp.Yolp;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;


public class AddRequestActivity extends AppCompatActivity {

    private ImageButton calenderButton;

    private int curYear;
    private int curMonth;
    private int curDayOfMonth;

    private EditText editTextProduct;
    private EditText editTextDeadline;

    private Spinner category;
    private Spinner subCategory;

    private String selectedSubCategory;

    private Button addButon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_request);

        calenderButton = findViewById(R.id.calenderButton);

        editTextProduct = findViewById(R.id.editTextProduct);
        editTextDeadline = findViewById(R.id.editTextDeadline);

        category = findViewById(R.id.spinnerLocationCategory);
        subCategory = findViewById(R.id.spinnerLocationSubCategory);

        setSpinner(category, Yolp.getInstance().getCategoryNames());

        addButon = findViewById(R.id.buttonAdd);

        calenderButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //Calendarインスタンスを取得
                 final Calendar calender = Calendar.getInstance();

                 //DatePickerDialogインスタンスを取得
                 DatePickerDialog datePickerDialog = new DatePickerDialog(AddRequestActivity.this, new DatePickerDialog.OnDateSetListener() {

                     @Override
                             public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                 curYear = year;
                                 curMonth = month;
                                 curDayOfMonth = dayOfMonth;

                                 //setした日付を取得して表示
                                 editTextDeadline.setText(String.format("%d / %02d / %02d", year, month+1, dayOfMonth));
                             }
                         },
                         calender.get(Calendar.YEAR),
                         calender.get(Calendar.MONTH),
                         calender.get(Calendar.DATE)
                 );

                 //dialogを表示
                 datePickerDialog.show();

             }
         });

        // 追加ボタン
         addButon.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 // エディットテキストのテキストを取得
                 String inputProduct = editTextProduct.getText().toString();

                 // 取得したテキストを TextView に張り付ける
                 RequestListActivity.requestlist.add(0, (new Request(inputProduct, new Date(curYear, curMonth, curDayOfMonth), selectedSubCategory, "a-hongo")));
                 finish();
             }
         });

        // リスナーを登録
        category.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            // アイテムが選択された時
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner)parent;
                String selectedCategory = (String)spinner.getSelectedItem();
                SearchSubCategory(selectedCategory);
            }

            // アイテムが選択されなかった
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        // リスナーを登録
        subCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            // アイテムが選択された時
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner)parent;
                selectedSubCategory = (String)spinner.getSelectedItem();
            }

            // アイテムが選択されなかった
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

    }

    private void SearchSubCategory(String selectedCategory) {
        switch(selectedCategory) {
            case "家電・携帯電話":
                setSpinner(subCategory, Yolp.getInstance().getCategoryByName("家電・携帯電話").getSubCategoryNames().toArray(new String[0]));
                break;
            case "コンビニ・スーパー":
                setSpinner(subCategory, Yolp.getInstance().getCategoryByName("コンビニ・スーパー").getSubCategoryNames().toArray(new String[0]));
                break;
        }
    }

    private void setSpinner(Spinner spinner, String[] arr){
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arr);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }


}