Hide first item in the spinner

北慕城南 提交于 2019-12-11 02:25:01

问题


I working on the spinner , there are 5 items in the spinner , i just want to hide the first item in the spinner , not to remove ,just hide.Problem is that when i click on the spinner ,without selecting an item the api get hit by using first item_id , i just added the blank feild in the spinner at first position(0). it is working properly .Only issue is that the visibility of the first blank item.I want to hide that item. My code is as follows :

            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:


Try this, the overridden getCount method will reduce the number of spinner items by 1. But overriding this method only will hide only the last item in the spinner. So we will override the getDropDownView method to offset (push up) all items by 1. The end result will be only item 0 (the first item) is hidden.

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;
        }
    };


来源:https://stackoverflow.com/questions/40786822/hide-first-item-in-the-spinner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!