我在微调器上工作,微调器中有5个项目,我只想隐藏微调器中的第一个项目,而不是删除,只是隐藏。问题是当我点击微调器时,没有选择项目通过使用第一个item_id来获取api,我只是在第一个位置(0)的微调器中添加了空白场。它工作正常。唯一的问题是第一个空白项目的可见性。我想要隐藏该项目。我的代码如下:
JSONArray staff_array;
List<String> owner_list =new ArrayList<String>();
final List<String> owner_id_list = new ArrayList<String>();
try
{
isEnabled(0); //To disable First Item
owner_list.add("");
owner_id_list.add("");
for (int i = 0; i <staff_array.length(); i++)
{
JSONObject staff_obj=staff_array.getJSONObject(i);
String fname=staff_obj.getString(FIRST_NAME);
String lname=staff_obj.getString(LAST_NAME);
owner_id_list.add(staff_obj.getString(STAFF_ID));
String staff_name=fname.concat(" "+lname);
owner_list.add(staff_name);
}
owner_list.add((String) getText(R.string.unassigned));
owner_id_list.add("0");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayAdapter<String> owner_Adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,owner_list);
owner_Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(owner_Adapter);
int owner_Position = owner_Adapter.getPosition(tv_owner.getText().toString());
spin.setSelection(owner_Position);
spin.performClick();
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
String selected_owner = parent.getItemAtPosition(pos).toString();
String staff_id=owner_id_list.get(pos);
//*************************
Toast.makeText(getApplicationContext(),selected_owner+" "+staff_id , Toast.LENGTH_SHORT).show();
Log.d("selected owner : ",selected_owner);
Log.d("staff id is blank : ",staff_id);
if(staff_id!="")
{
String owner_filter="&vis_ticket_id="+Ticket_id+"&vis_action=staff&vis_update_id="+staff_id;
UPDATE_OWNER_URL=op.getUrl(getApplicationContext(),"ticket","update_properties",owner_filter);
JSONArray owner_array ;
}
//*************************
try
{
owner_array = new editProperties(UPDATE_OWNER_URL).execute().get();
String result=owner_array.toString();
if(result.equals("[\"success\"]"))
{
new ticketDetails().execute(); // parse other ticket details using AsyncTask
//tv_owner.setText(selected_owner);
}
else {Operation.showToast(getApplicationContext(), R.string.error);}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:5)
尝试这一点,重写的getCount
方法会将微调器项的数量减少1.但是重写此方法只会隐藏微调器中的最后一项。因此,我们将覆盖getDropDownView
方法以将所有项目偏移(向上推)1。最终结果将仅隐藏第0项(第一项)。
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.your_spinner_layout, spinnerArray) {
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return super.getDropDownView(position + 1, convertView, parent);
}
public int getCount() {
return spinnerArray.size - 1;
}
};