我有一个微调器,我是从一个activity动态创建的。我想根据服务器上的数据库选择一个值,所以我这样做
String[] items = { "Credit Card", "Cash"};
final Spinner TP = new Spinner(this);
TP.setId(20);
TP.setPadding(8,8,8,8);
TP.setBackgroundResource(R.drawable.text_bg);
TourExpenseListDetailRow.addView(TP);// add the column to the table row here
LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams)TP.getLayoutParams();
params1.setMargins(0, 0, 0, 0); //substitute parameters for left, top, right, bottom
TP.setLayoutParams(params1);
//Selecting value as per database for testing I am selecting Position 1
TP.setSelection(1);
ArrayAdapter aa = new ArrayAdapter(this, R.layout.spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
TP.setAdapter(aa);
但我的问题是我的价值永远不会被选中
答案 0 :(得分:3)
尝试移动
TP.setSelection(1);
后
TP.setAdapter(aa);
还要考虑遵循Java命名约定。变量名称应该是混合大小写,意味着它们以小写字母开头,后续单词以大写字母开头,所以
Spinner TP
将是
Spinner tp
或者
Spinner tP
答案 1 :(得分:0)
使用类似这样的内容
private void setUpSpinner() {
//TODO: for the medical profile
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),R.array.demoArray,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
medicalProfile.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()){
case R.id.medicalProfile:
String medicalStr = parent.getItemAtPosition(position).toString();
Log.d("MedSpinner",medicalStr);
break;
}
}