1

是否可以在更改微调器项目时创建警告消息,但如果用户单击的不选择应该被取消并且所选项目应该保持以前的状态。如果我存储最后选择的微调器项目并在用户选择否时将其传递给微调器,它将触发我不想要的 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

    });

4

2 回答 2

1

我对复选框有同样的问题:我设置了一个 onCheckedChangeListener 来监听用户输入,然后我从设置或警报对话框中更新复选框,就像你正在做的那样,它再次触发 onCheckedChangeListener :) 这就是我解决它的方法:

1) 代替“spinnerSearch.setOnItemSelectedListener(new OnItemSelectedListener() {...”, 使用全局声明创建新的 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)

于 2012-11-10T22:10:18.713 回答
0

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

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;
}

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();

}

}

于 2012-11-16T14:20:57.653 回答