2012-11-10 234 views
1

是否可以在更改微调项目时创建警告消息,但是如果用户点击了否则应取消选择 选择项目应保持原来的状态。如果我存储最后选定的微调项目,并将其传递给微调,当用户选择否 它将更紧密onItemSelectedListener,我不想。AlertDialog inside spinner setOnItemSelectedListener

我试过使用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 

    }); 

回答

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

} 

}

1

我曾与复选框相同的问题:我设置onCheckedChangeListener听取用户的输入,然后我更新的设置复选框,或警告对话框,就像你在做什么,这,再次触发onCheckedChangeListener: )这是我的工作如何围绕它:

1)替代“spinnerSearch.setOnItemSelectedListener(新OnItemSelectedListener(){...”,创造新的OnItemSelectedListener对象与全局声明我要叫它myClickListener

2)在这个物体的内部,whe n在“否”按钮声明中创建警报对话框,稍微更改代码以将侦听器设置为空,然后更改微调器值,然后再将侦听器设置为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) 

+0

我做同样的事情,我得到以下错误:无法从全新AdapterView.OnItemSelectedListener转换(){ }到AdapterView.OnItemClickListener – Damir

相关问题