如何删除微调Android的默认值? [英] How to remove default value in spinner android?

查看:131
本文介绍了如何删除微调Android的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中使用JSON和将它们插入到微调获取的值。这个过程被成功地运行。 但是我收到一个微调提示两次,当我点击微调;我怎么可以删除飞旋......

第一个默认值

这是快照 - 希望你明白我的问题:

这是我的code: -

 私有类GetCategories扩展的AsyncTask<虚空,虚空,虚空> {
    在preExecute保护无效(){
        super.on preExecute();
    }

    @覆盖
    保护无效doInBackground(虚空......为arg0){
        ServiceHandler jsonParser =新ServiceHandler();
        JSON字符串= jsonParser.makeServiceCall(URL_CATEGORIES,
                ServiceHandler.GET);
        如果(JSON!= NULL){
            尝试 {
                JSONObject的jsonObj =新的JSONObject(JSON);
                如果(jsonObj!= NULL){
                    JSONArray类= jsonObj
                            .getJSONArray(类);
                    categoriesList.clear();
                    的for(int i = 0; I< categories.length();我++){
                        JSONObject的catObj =(的JSONObject)categories.get(我);
                        类别猫=新的类别(
                                catObj.getString(bus_type_id),
                                catObj.getString(bus_type));
                        categoriesList.add(猫);
                    }
                }
            }赶上(JSONException E){
                e.printStackTrace();
            }
        } 其他 {
            Log.e(JSON数据,未从服务器接收到任何数据!);
        }
        返回null;
    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        super.onPostExecute(结果);
        populateSpinner();
    }
}




私人无效populateSpinner(){
    名单<字符串>标贴=新的ArrayList<字符串>();
    lables.add(选择型);
    的for(int i = 0; I< categoriesList.size();我++){
        lables.add(categoriesList.get(ⅰ).getbus_type());
    }
    //创建适配器微调
    ArrayAdapter<字符串> spinnerAdapter =新的ArrayAdapter<字符串>(这一点,
            android.R.layout.simple_spinner_item,标贴);
    //下拉布局样式 - 列表视图单选按钮
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //连接数据适配器微调器
    typeSpinner.setAdapter(spinnerAdapter);
    typeSpinner.setOnItemSelectedListener(typeSelectedListener);
}

私人OnItemSelectedListener typeSelectedListener =新OnItemSelectedListener(){
    @覆盖
    公共无效onItemSelected(适配器视图<>母公司视图中查看,
            INT位置,长的id){
        如果(typeSpinner.getSelectedItem()的toString()==选择型){
            海峡=;
            typeSpinnercity.getSelectedView();
            typeSpinnercity.setEnabled(假);
        } 其他 {
            ok.setEnabled(真正的);
            海峡= typeSpinner.getSelectedItem()的toString()。
            typeSpinnercity.getSelectedView();
            typeSpinnercity.setEnabled(真正的);
            如果(Utils.checkInternet(CutomerOfferActivity.this)){
                新GetCategoriesCity()执行()。
            }
        }
    }

    @覆盖
    公共无效onNothingSelected(适配器视图<>母公司){
    }
};
 

解决方案

使用一个ArrayAdapter和覆盖的方法 - getDropDownView ..

  ArrayAdapter<字符串> spinnerAdapter =新的ArrayAdapter<字符串>(这一点,
            android.R.layout.simple_spinner_item,标贴){
        @覆盖
        公共查看getDropDownView(INT位置,查看convertView,ViewGroup中父){

            视图V = NULL;

            如果(位置== 0){
                TextView的电视=新的TextView(的getContext());
                tv.setHeight(0);
                tv.setVisibility(View.GONE);
                V =电视;
            }
            其他 {

                V = super.getDropDownView(位置,空,父母);
            }

            parent.setVerticalScrollBarEnabled(假);
            返回伏;
        }
    };
 

使用上述code,并在下拉列表中删除默认值。

I am fetching the values from Database using JSON and inserting them into spinner. This process is running successfully. But I am receiving a Spinner hint two times, when I click on spinner; how can I remove the first default value from spinner...

This is snapshot :- hope you understand my problem :

This is my code:-

private class GetCategories extends AsyncTask<Void, Void, Void> {
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES,
                ServiceHandler.GET);
        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");
                    categoriesList.clear();
                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(
                                catObj.getString("bus_type_id"),
                                catObj.getString("bus_type"));
                        categoriesList.add(cat);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        populateSpinner();
    }
}




private void populateSpinner() {
    List<String> lables = new ArrayList<String>();
    lables.add("Choose Type");
    for (int i = 0; i < categoriesList.size(); i++) {
        lables.add(categoriesList.get(i).getbus_type());
    }
    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);
    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // attaching data adapter to spinner
    typeSpinner.setAdapter(spinnerAdapter);
    typeSpinner.setOnItemSelectedListener(typeSelectedListener);
}

private OnItemSelectedListener typeSelectedListener = new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
            int position, long id) {
        if (typeSpinner.getSelectedItem().toString() == "Choose Type") {
            str = "";
            typeSpinnercity.getSelectedView();
            typeSpinnercity.setEnabled(false);
        } else {
            ok.setEnabled(true);
            str = typeSpinner.getSelectedItem().toString();
            typeSpinnercity.getSelectedView();
            typeSpinnercity.setEnabled(true);
            if (Utils.checkInternet(CutomerOfferActivity.this)) {
                new GetCategoriesCity().execute();
            }
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
};

解决方案

Use the ArrayAdapter and Override the method - getDropDownView..

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables){
        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {             

            View v = null;

            if (position == 0) {
                TextView tv = new TextView(getContext());
                tv.setHeight(0);
                tv.setVisibility(View.GONE);
                v = tv;
            } 
            else { 

                v = super.getDropDownView(position, null, parent);
            } 

            parent.setVerticalScrollBarEnabled(false);
            return v;
        }
    };

Use the above mentioned code and remove the default value in the dropdown.

这篇关于如何删除微调Android的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆