在spinner setOnItemSelectedListener中的AlertDialog

时间:2012-11-10 21:39:28

标签: android spinner alertdialog

是否可以在更改微调器项目时创建警告消息,但是如果用户单击“否”,则可以 选择应该被取消,选定的项目应该保持原样。如果我存储最后选择的微调项目,并在用户选择否时将其传递给微调器 它会tirger onItemSelectedListener,我不想这样做。

我尝试使用OnTouchListener,但这没有用,因为在触摸微调器之后会立即显示微调器列表。

请参阅下面的代码以便更好地理解。

    spinnerSearch.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) {

            if (!ShoppingCart.isEmpty()) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(SearchProductActivity.this);
                dialog.setMessage("Selecting new finacier will empty your basket.").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //do some work
                        //storing lastSelectedFinancier
                    }

                }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                        //cancel selection
                        //spinnerSearch.setSelection(lastSelectedFinancier)

                    }

                });

                AlertDialog alert = dialog.show();

                TextView messageView = (TextView) alert.findViewById(android.R.id.message);
                messageView.setGravity(Gravity.CENTER);
            } else {
                //do some other work
                //lastSelectedFinancier
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

        //lastSelectedFinancier

    });

spinnerSearch.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) { if (!ShoppingCart.isEmpty()) { AlertDialog.Builder dialog = new AlertDialog.Builder(SearchProductActivity.this); dialog.setMessage("Selecting new finacier will empty your basket.").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //do some work //storing lastSelectedFinancier } }).setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //cancel selection //spinnerSearch.setSelection(lastSelectedFinancier) } }); AlertDialog alert = dialog.show(); TextView messageView = (TextView) alert.findViewById(android.R.id.message); messageView.setGravity(Gravity.CENTER); } else { //do some other work //lastSelectedFinancier } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } //lastSelectedFinancier });

2 个答案:

答案 0 :(得分:1)

我遇到了与复选框相同的问题:我设置了一个onCheckedChangeListener来监听用户输入,然后我更新了设置或警告对话框中的复选框,就像你正在做的那样,再次触发onCheckedChangeListener :)这就是我的工作方式围绕它:

1)使用全局声明创建新的OnItemSelectedListener对象,而不是“spinnerSearch.setOnItemSelectedListener(new OnItemSelectedListener(){...”。我将把它称为myClickListener

2)在此对象的主体内部,当您创建警告对话框时,在“否”按钮声明中,稍微更改您的代码以将侦听器设置为null,然后更改微调器值,然后设置再次监听myClickListener。

尝试修改这样的代码,并告诉我它是否有效:

    private OnItemClickListener myClickListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) {

            if (!ShoppingCart.isEmpty()) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(SearchProductActivity.this);
                dialog.setMessage("Selecting new finacier will empty your basket.").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //do some work
                        //storing lastSelectedFinancier
                    }

                }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                //Modified code:
                        spinnerSearch.setOnItemSelectedListener(null) //Disable the onItemClickListener
                        spinnerSearch.setSelection(lastSelectedFinancier) //Cancel selection, while the OnClickListener is disabled
                        spinnerSearch.setOnItemSelectedListener(myClickListener) //Enable onItemClickListener
                        dialog.dismiss();
                    }

                });

                AlertDialog alert = dialog.show();

                TextView messageView = (TextView) alert.findViewById(android.R.id.message);
                messageView.setGravity(Gravity.CENTER);
            } else {
                //do some other work
                //lastSelectedFinancier
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

        //lastSelectedFinancier

    }); //End of myClickListener declaration


    //This goes into your code:
    spinnerSearch.setOnItemSelectedListener(myClickListener)

private OnItemClickListener myClickListener = new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) { if (!ShoppingCart.isEmpty()) { AlertDialog.Builder dialog = new AlertDialog.Builder(SearchProductActivity.this); dialog.setMessage("Selecting new finacier will empty your basket.").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); //do some work //storing lastSelectedFinancier } }).setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //Modified code: spinnerSearch.setOnItemSelectedListener(null) //Disable the onItemClickListener spinnerSearch.setSelection(lastSelectedFinancier) //Cancel selection, while the OnClickListener is disabled spinnerSearch.setOnItemSelectedListener(myClickListener) //Enable onItemClickListener dialog.dismiss(); } }); AlertDialog alert = dialog.show(); TextView messageView = (TextView) alert.findViewById(android.R.id.message); messageView.setGravity(Gravity.CENTER); } else { //do some other work //lastSelectedFinancier } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } //lastSelectedFinancier }); //End of myClickListener declaration //This goes into your code: spinnerSearch.setOnItemSelectedListener(myClickListener)

答案 1 :(得分:0)

我找到了解决此问题的方法。我使用textbox(whit spinner样式)而不是微调器,并将其与用于创建列表的警报对话框结合使用。 也许这不是最好的解决方案,但它对我有用:)))

public class SpinnerItem {

String spinnerText;
String value;

public SpinnerItem(String text, String value) {
    this.spinnerText = text;
    this.value = value;
}

public String getSpinnerText() {
    return spinnerText;
}

public String getValue() {
    return value;
}

public String toString() {
    return spinnerText;
}

String spinnerText; String value; public SpinnerItem(String text, String value) { this.spinnerText = text; this.value = value; } public String getSpinnerText() { return spinnerText; } public String getValue() { return value; } public String toString() { return spinnerText; }

public class SearchProductActivity extends BaseActivity {

private EditText txtSearch;
private ListView listProducts;
private Button btnShoppingCart;
private EditText txtSelectFinancier;
private DevRestHelper rest;
private ListOfProducts adapter;
public static String financierRelationNumber;
private int numberOfItemsInBasket = 0;

private List<ProductModel> products = new ArrayList<ProductModel>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_product);

    initControls();
    getProjectSupplier();
    financierRelationNumber = String.valueOf(OrderData.Financiers.get(0).RelationNumber);
    txtSelectFinancier.setText(String.valueOf(OrderData.Financiers.get(0).Name));
    GetProducts(financierRelationNumber);
    setButtonActions();
    ShoppingCart.ProductOwnerId = getProjectSupplier().get(0).getValue();
    ShoppingCart.ProductOwner = getProjectSupplier().get(0).getSpinnerText();

}

    private void initControls() {
    btnShoppingCart = (Button) findViewById(R.id.activity_search_product_btnShoppingCart);
    listProducts = (ListView) findViewById(R.id.activity_search_product_listProducts);
    txtSearch = (EditText) findViewById(R.id.activity_search_product_txtName);
    txtSelectFinancier = (EditText) findViewById(R.id.activity_search_product_txtFinancier);

}

private void setButtonActions() {

    txtSelectFinancier.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (!ShoppingCart.isEmpty()) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(SearchProductActivity.this);
                dialog.setMessage(getString(R.string.err_selecting_new_finacier_will_empty_your_basket)).setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        listDialog(SearchProductActivity.this, "", getProjectSupplier(), txtSelectFinancier);
                        listProducts.setAdapter(null);
                        ShoppingCart.emptyCartItems();
                        ShoppingCart.ProductOwner = txtSelectFinancier.getText().toString();

                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();
                    }

                });

                AlertDialog alert = dialog.show();

                TextView messageView = (TextView) alert.findViewById(android.R.id.message);
                messageView.setGravity(Gravity.CENTER);

            } else
                listDialog(SearchProductActivity.this, "", getProjectSupplier(), txtSelectFinancier);
        }
    });

}

    private List<SpinnerItem> getProjectSupplier() {

    List<SpinnerItem> items = new ArrayList<SpinnerItem>();

    for (DetermineFinanciersModel finaciers : OrderData.Financiers) {
        items.add(new SpinnerItem(finaciers.Name, String.valueOf(finaciers.RelationNumber)));
    }

    return items;

}

private void listDialog(Context context, String title, final List<SpinnerItem> list, final TextView control) {

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);

    List<String> strings = new ArrayList<String>();

    for (SpinnerItem spinnerItem : list) {
        strings.add(spinnerItem.getSpinnerText());
    }

    final CharSequence[] items = strings.toArray(new String[strings.size()]);

    builder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int selectedItem) {
            control.setText(list.get(selectedItem).getSpinnerText());
            ShoppingCart.ProductOwnerId = list.get(selectedItem).getValue();
            GetProducts(list.get(selectedItem).getValue());
        }
    });

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

}