【问题标题】:How to Show alert when Default Selection Changed in Spinner Selection?如何在微调器选择中更改默认选择时显示警报?
【发布时间】:2017-01-13 21:28:27
【问题描述】:

我有一个包含 6 个值的数组.. 我在微调器中设置了该数组,默认选择是 3.. 当用户选择低于 3 时,我想显示一个警报对话框,如 Can't able to Select.. 如果用户选择高于 3 意味着我想显示一个警报对话框,如 Are You Sure to Change.. 问题是在更改了上述 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();
    }

}

【问题讨论】:

  • 请展示你的努力。你试过什么?
  • 我在这里附上..请检查出来..
  • 我基本上得到了输出..但事情是在选择高于 3 之后,降级到 3 到 6 之间,警报对话框显示双倍时间..
  • Else 告诉我任何其他方法来设置带有警报对话框的微调器的选择..
  • 这是我的完整程序..

标签: android spinner selection


【解决方案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();
}

【讨论】:

    猜你喜欢
    • 2022-07-01
    • 2015-06-11
    • 2017-10-31
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多