Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / PostActivity.java
package com.example.nemophila;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class PostActivity extends AppCompatActivity {
    private String pid;
    private String uid;
    private String name;
    private String genre;
    private String date;
    private int rate;
    private String comment;

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

        //左上のボタンでキャンセル(ShopActivityに戻る)
        //確認用ダイアログを入れたい
        Button returnButton = (Button)findViewById(R.id.postAcReturnButton);
        returnButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                Intent intent = new Intent(getApplication(), ShopActivity.class);
                startActivity(intent);
            }
        });

        //評価を入力するためのプルダウン
        ArrayAdapter<String> rateAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        rateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        rateAdapter.add("評価を選択");
        rateAdapter.add("1");
        rateAdapter.add("2");
        rateAdapter.add("3");
        rateAdapter.add("4");
        rateAdapter.add("5");
        Spinner rateSpinner = (Spinner) findViewById(R.id.inputRate);
        rateSpinner.setAdapter(rateAdapter);

        //プルダウンが変更されたら実行
        rateSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            //選択が何もなかった時の動作
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}

            //何か選択された時の動作
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String gettedRate = (String)rateSpinner.getSelectedItem();

                //プルダウンで取得した評価をintに変換してrateに入れる
                if(gettedRate != "評価を選択"){
                    rate = Integer.parseInt(gettedRate);
                    //表示用にStringの星に変換
                    String displayRate = "";
                    for (int j = 0; j < 5; j++){
                        if(rate > j ){
                            displayRate += "★";
                        }else{
                            displayRate += "☆";
                        }
                    }
                    TextView rateView = (TextView) findViewById(R.id.ratePostAc);
                    rateView.setText(displayRate);
                }
//                System.out.println(gettedRate);
//                System.out.println(rate);
            }
        });









    }
}