我正在尝试从Web API获取值并将其更新为适配器。我从API获得了值,它也显示在下拉列表中,但是当我从下拉列表中选择任何值时,它没有显示所选值
这是我的代码: -
public class ApplyLeave extends BaseActivity implements OnItemSelectedListener {
List<String>Leavetype=new ArrayList<String>();
String selState=null;// variable used for store the value of selected value in drop down
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
context=this;
getLeaveType();// method fetching the value from API and updating it in leaveType
final Spinner spinerLeavetype = (Spinner)findViewById(R.id.spLeavetype);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,Leavetype);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinerLeavetype.setAdapter(adapter);
spinerLeavetype.setOnItemSelectedListener(this);
}
private void getLeaveType()
{
new Thread( new Runnable() {
@Override
public void run() {
LeaveTypeResponse jsonResponse=ServiceManager.CompanyLeaveType(context,app);
if(jsonResponse!=null){
if(jsonResponse.Response!=null){
for(Leave leaveTypeName:jsonResponse.Response)
{
Leavetype.add(leaveTypeName.Name);// here Leavetype is updating with value
}
}
}
else{
CommonTasks.ShowToast(getApplicationContext(),handler,getString(R.string.NETWORKCONNECTIONEXCEPTION_MESSAGE));
}
}
}).start();
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
selState= String.valueOf(adapterView.getSelectedItem());// selected value storing in selState variable .
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
虽然下拉列表与
等硬编码值一起使用 string[] Leavetype=new String[]{"Medical","Casual"};
请有人告诉我我在哪里做错了。谢谢。
答案 0 :(得分:0)
小错误只是在从像这个
这样的websrvice检索到的数据后应用适配器private void getLeaveType()
{
new Thread( new Runnable() {
@Override
public void run() {
LeaveTypeResponse jsonResponse=ServiceManager.CompanyLeaveType(context,app);
if(jsonResponse!=null){
if(jsonResponse.Response!=null){
for(Leave leaveTypeName:jsonResponse.Response)
{
Leavetype.add(leaveTypeName.Name);// here Leavetype is updating with value
}
runOnUiThread(new Runnable() {
@Override
public void run() {
spinerLeavetype.setAdapter(adapter);
spinerLeavetype.setOnItemSelectedListener(ApplyLeave.this);
}
});
}
}
else{
CommonTasks.ShowToast(getApplicationContext(),handler,getString(R.string.NETWORKCONNECTIONEXCEPTION_MESSAGE));
}
}
}).start();
}