如何在微调器选择中更改默认选择时显示警报?

时间:2017-01-13 09:17:28

标签: android spinner selection

我有一个包含6个值的数组.. 我在spinner中设置该数组,默认选择为3 .. 当用户选择低于3时,我想显示一个警告对话框,如无法选择..如果用户选择上面3表示我想显示一个警告对话框,如你确定要改变.. 问题是在更改了上面的3之后,我将微调器的选择设置为更改位置..但是警报对话框在我的程序中显示了两倍的时间.. 请给我任何解决方案?..

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.android.c_fiber.Intro_Slider.SessionManagement;
import java.util.Arrays;
import java.util.HashMap;

public class spinnerDoubt extends AppCompatActivity{

    String[] plans,months;

    int changed_plan_position,changed_month_position;
    int plan_position,plan_month;

    String selectedItem;
    Spinner spinner_plan,spinner_month;

    String plan;
    String month = "1";

    boolean isChanged = false;
    boolean isMonthChanged = false;

    SessionManagement sessionManagement;

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

        plans = new String[]{"Start","Fast","Hot","Pace","Electric","Thunder"};

        months = new String[]{"1","3","6","12"};

        spinner_plan = (Spinner) findViewById(R.id.pay_plan);
        spinner_month = (Spinner) findViewById(R.id.pay_month);

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.show();

        Runnable progressRunnable = new Runnable() {

            @Override
            public void run() {

                sessionManagement = new SessionManagement(this);

                HashMap<String,String> hashMap = sessionManagement.getUserDetails();

                plan = hashMap.get(SessionManagement.KEY_PLAN_NAME);

                plan_position = Arrays.asList(plans).indexOf(plan);
                plan_month = Arrays.asList(months).indexOf(month);

                ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.layout_spinner,R.id.spinner_text,plans);
                spinner_plan.setAdapter(arrayAdapter);
                spinner_plan.setSelection(plan_position);

                ArrayAdapter arrayAdapter_month = new ArrayAdapter(this,R.layout.layout_spinner,R.id.spinner_text,months);
                spinner_month.setAdapter(arrayAdapter_month);
                spinner_month.setSelection(plan_month);

                progressDialog.dismiss();

            }
        };

        Handler pdCanceller = new Handler();
        pdCanceller.postDelayed(progressRunnable, 2000);

        spinner_plan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                selectedItem = parent.getItemAtPosition(position).toString();

                int selectPosition = parent.getSelectedItemPosition();

                Log.e("plan_position_1",plan_position+"");
                Log.e("selectedItem",selectPosition+"");

                if (!isChanged)
                {
                    if (selectPosition > plan_position)
                    {
                        dialogInterfacePlan("Are You Sure To Change!..");
                    }
                    else if (selectPosition < plan_position)
                    {
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);

                        builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
                                .setCancelable(false)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        spinner_plan.setSelection(plan_position);
                                        dialog.cancel();

                                    }
                                });

                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                }
                else {
                    if (selectPosition == plan_position)
                    {
                        isChanged = true;
                        dialogInterfacePlan("Are You Sure To Change!..");
                    }
                    else if (selectPosition>plan_position)
                    {
                        isChanged = true;
                        dialogInterfacePlan("Are You Sure To Change!..");
                    }
                    else if (selectPosition<plan_position) {

                        AlertDialog.Builder builder = new AlertDialog.Builder(this);

                        builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
                                .setCancelable(false)
                                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        isChanged = true;
                                        spinner_plan.setSelection(changed_plan_position);
                                        dialog.cancel();
                                    }
                                });

                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                }

            } // to close the onItemSelected
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });

        spinner_month.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                int selectPosition = parent.getSelectedItemPosition();

                if (!isMonthChanged)
                {
                    if (selectPosition>plan_month)
                    {
                        dialogInterfaceMonth("Are You Sure To Change?..");
                    }
                }
                else
                {
                    if (selectPosition == plan_month){
                        isChanged = true;
                        dialogInterfaceMonth("Are You Sure To Change?..");
                    }
                    else if (selectPosition > changed_month_position){
                        isChanged = true;
                        dialogInterfaceMonth("Are You Sure To Change?..");
                    }
                    else if (selectPosition<changed_month_position) {
                        isChanged = true;
                        dialogInterfaceMonth("Are You Sure To Change?..");
                    }
                }

            } // to close the onItemSelected
            public void onNothingSelected(AdapterView<?> parent)
            {

            }
        });
    }

    public void dialogInterfacePlan(String text)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(text)
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        changed_plan_position = Arrays.asList(plans).indexOf(selectedItem);
                        isChanged = true;

                        Log.e("After Changed",plan);
                        Log.e("After Changed",changed_plan_position+"");

                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        if (isChanged) {
                            spinner_plan.setSelection(changed_plan_position);
                            dialogInterface.cancel();
                        }
                        else
                        {
                            spinner_plan.setSelection(plan_position);
                            dialogInterface.cancel();
                        }
                    }
                });

        AlertDialog alert = builder.create();
        alert.show();
    }

    public void dialogInterfaceMonth(String text)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(text)
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        String change_month = spinner_month.getSelectedItem().toString();

                        changed_month_position = Arrays.asList(months).indexOf(change_month);

                        isMonthChanged = true;

                        if (changed_month_position == 0)
                        {
                            spinner_month.setSelection(changed_month_position);
                        }
                        else if (changed_month_position == 1)
                        {
                            spinner_month.setSelection(changed_month_position);
                        }
                        else if (changed_month_position == 2)
                        {
                            spinner_month.setSelection(changed_month_position);
                        }
                        else
                        {
                            spinner_month.setSelection(changed_month_position);
                        }

                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (isChanged) {
                            spinner_month.setSelection(changed_month_position);
                            dialogInterface.cancel();
                        }
                        else
                        {
                            spinner_month.setSelection(plan_month);
                            dialogInterface.cancel();
                        }
                    }
                });

        AlertDialog alert = builder.create();
        alert.show();
    }

}

1 个答案:

答案 0 :(得分:1)

由于行 -

,您看到对话框两次
spinner_plan.setSelection(plan_position);

这会触发对OnItemSelectedListener的调用,再次显示该对话框 您可以使用简单的布尔变量来控制何时显示对话框。 希望这有帮助 -

boolean isChanged = false;
boolean isMonthChanged = false;
boolean shouldShowDialog=true;
.......

将听众更新为此 -

 spinner_plan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedItem = parent.getItemAtPosition(position).toString();

            final int selectPosition = parent.getSelectedItemPosition();

            Log.e("plan_position_1", plan_position + "");
            Log.e("selectedItem", selectPosition + "");

            if(shouldShowDialog){
                if (!isChanged) {
                    if (selectPosition > plan_position) {
                        dialogInterfacePlan("Are You Sure To Change!..");
                    } else if (selectPosition < plan_position) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                        builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
                                .setCancelable(false)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        spinner_plan.setSelection(plan_position);
                                        shouldShowDialog = false;
                                        dialog.cancel();
                                    }
                                });

                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                } else {
                    if (selectPosition == plan_position) {
                        isChanged = true;
                        dialogInterfacePlan("Are You Sure To Change!..");
                    } else if (selectPosition > plan_position) {
                        isChanged = true;
                        dialogInterfacePlan("Are You Sure To Change!..");
                    } else if (selectPosition < plan_position) {

                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                        builder.setMessage("You Can't Downgrade.. \n Please Contact Our Customer Care Executive.. \n Thank You..")
                                .setCancelable(false)
                                .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        isChanged = true;
                                        spinner_plan.setSelection(changed_plan_position);
                                        shouldShowDialog = false;
                                        dialog.cancel();
                                    }
                                });

                        AlertDialog alert = builder.create();
                        alert.show();
                    }
                }
            }
            shouldShowDialog = true;


        } // to close the onItemSelected

        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

和方法dialogInterfacePlan到此 -

public void dialogInterfacePlan(String text) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setMessage(text)
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    changed_plan_position = Arrays.asList(plans).indexOf(selectedItem);
                    isChanged = true;

                    Log.e("After Changed", plan);
                    Log.e("After Changed", changed_plan_position + "");

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    if (isChanged) {
                        spinner_plan.setSelection(changed_plan_position);
                        shouldShowDialog=false;
                        dialogInterface.cancel();
                    } else {
                        spinner_plan.setSelection(plan_position);
                        dialogInterface.cancel();
                    }
                }
            });

    AlertDialog alert = builder.create();
    alert.show();
}