我动态创建一个表,在表中我动态创建一个微调器。当用户使用微调器选择一个字符串时,我想知道选择了什么。
动态创建的微调器:
TextView t7v = new TextView(this);
/**********ADD SPINNER*******/
ArrayList<String> spinnerArray = new ArrayList<String>();
if(tempBranch[i].equals("karmiel")) {
spinnerArray.add(" " + tempBranch[i] + " ");
spinnerArray.add("batyam");
spinnerArray.add("natbag");
}else if(tempBranch[i].equals("batyam")){
spinnerArray.add(" " + tempBranch[i] + " ");
spinnerArray.add("karmiel");
spinnerArray.add("natbag");
}else{
spinnerArray.add(" " + tempBranch[i] + " ");
spinnerArray.add("batyam");
spinnerArray.add("karmiel");
}
spinner = new Spinner(this);
spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
tbrow.addView(spinner);
/*********END ADD SPINNER*****/
final Button rowButton2 = new Button(this);// create a new button
//Button OK
rowButton2.setText("OK");
rowButton2.setId(i);//set the number of count
// b.addView(rowButton);
tbrow.addView(rowButton2);
// save a reference to the button for later
myButton[i] = rowButton2;
myButton[i].setOnClickListener(btnClick2(rowButton2));//click the button
我想要的按钮接收值:
private View.OnClickListener btnClick2(final Button button) {//clock button that create dynamic
return new View.OnClickListener(){
public void onClick(View v){
//create array
idEmpl = id[v.getId()];
tmpBranch = spinner.getSelectedItem().toString();//NOT WORK TEMPORARY
String type = "changeTempBranch";
/* ManagerListInBackground managerListInBackground = new ManagerListInBackground(v.getContext());
managerListInBackground.execute(type,idEmpl,tmpBranch);//send the data to valid*/
}
};
}
答案 0 :(得分:0)
试试这个答案:https://stackoverflow.com/a/1714426/9217218
然后在onItemSelected
方法中添加你可以得到的字符串值如下:
String selectedItemTitle = mySpinner.getSelectedItem().toString();
答案 1 :(得分:0)
借助IdleApps Inc及其链接:enter link description here
我解决了我的问题,就是回答:
enter code here/* Value Spinner */
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
/* Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();*/
tmpBranch = parent.getItemAtPosition(pos).toString();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
tmpBranch = spinner.getSelectedItem().toString();
}
});
/* End of Value Spinner */
构建动态表中的代码,所以要小心,不能在其他地方工作(对我来说最小) 谢谢大家!