2016-11-24 59 views
1

我在微調器上工作,微調器中有5個項目,我只想隱藏微調器中的第一項,不能刪除,只是隱藏。問題是,當我點擊微調器,沒有選擇api通過使用第一個item_id命中的項目,我只是在第一個位置(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 

      } 

      });     

回答

1

由1試試這個,重寫的getCount方法將減少微調的項目個數,但重寫此方法只將隱藏只在微調中的最後一項。因此,我們將覆蓋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; 
     } 
    }; 
+0

謝謝你本傑明 –

相關問題