Android的微调默认值问题 [英] Android spinner default value problem

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

问题描述

在我的应用我使用微调含有1至60项来自哪个用户必须选择them.My问题的人是当用户再次调用包含微调的活动,用户可以能够看到他的previously选择的项目作为离心器的缺省值。
我曾尝试使用spineer.setselection(),但我不能够得到。
所以如果有人对此有任何理想的话,请给一些提示,以解决我的问题。

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    ViewGroup中的ViewGroup =(ViewGroup中)findViewById(R.id.myprofilelinear);
    ViewGroup.inflate(这一点,R.layout.spinner,
            ViewGroup中);    LoadUI();
}
公共无效LoadUI()
{
    imgtoggle =(ImageView的)findViewById(R.id.btntoggle);
    imgtoggle.setOnClickListener(本);
    串参数= JsonDataProcessor.MYPROFILELIST.get(0).getIsTracking();
    如果(param.equalsIgnoreCase(真))
    {
        imgtoggle.setImageResource(R.drawable.on_button);
    }
    其他
    {
        imgtoggle.setImageResource(R.drawable.off_button);
    }    btnsave =(按钮)findViewById(R.id.btnSavechanges);
    btnsave.setOnClickListener(本);    的String [] S =新的String [60];
    INT I;
    对于(i = 0; I< 60;我++){
        S [I] = Integer.toString第(i + 1);
    }     旋=(微调)findViewById(R.id.TimeSpinner);
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(TimeSpin.this,
            android.R.layout.simple_spinner_item,S);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(适配器);}


解决方案

您应该点燃OnItemselectedListener这并设置微调所选值

勾选此code:

 布尔flag_default_country = FALSE;    spin_country.setOnItemSelectedListener(新OnItemSelectedListener(){        @覆盖
        公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
                INT ARG2,长ARG3){
            // TODO自动生成方法存根
                的for(int i = 0; I< s.length;我++){
                    如果(flag_default_country == FALSE){
                        如果(S [I]的ToString()。equalsIgnoreCase(US)){
                            spin_country.setSelection(ⅰ);
                            flag_default_country = TRUE;
                        }
                    }
                }
            }        @覆盖
        公共无效onNothingSelected(适配器视图<>为arg0){
            // TODO自动生成方法存根
        }
    });

spin_country是微调对象

首先初始化flag_default_country为false,然后检查由用户已存储在某些值中选择的字符串值。如果在第i个位置的阵列值等于用户选择的值将在微调在我设置的值。

有关保存用户选择的值可以使用共享preference。

勾选此链接共享preference

让,如果你发现任何困难我知道了。

In my Application i am using Spinner which contain 1 to 60 items from which user have to select anyone of them.My problem is that when user again call the Activity which contain spinner, user can able to see his previously selected item as the default value of spinner. I have try to use the spineer.setselection() but i am not able to get. so if anyone have any ideal about this then please give some hint to solve my problem.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ViewGroup viewgroup = (ViewGroup) findViewById(R.id.myprofilelinear);
    ViewGroup.inflate(this, R.layout.spinner,
            viewgroup);

    LoadUI();
}
public void LoadUI()
{
    imgtoggle = (ImageView) findViewById(R.id.btntoggle);
    imgtoggle.setOnClickListener(this);
    String param=JsonDataProcessor.MYPROFILELIST.get(0).getIsTracking();
    if(param.equalsIgnoreCase("True"))
    {
        imgtoggle.setImageResource(R.drawable.on_button);
    }
    else
    {
        imgtoggle.setImageResource(R.drawable.off_button);
    }

    btnsave = (Button)findViewById(R.id.btnSavechanges);
    btnsave.setOnClickListener(this);

    String[] s = new String[60];
    int i;
    for (i = 0; i < 60; i++) {
        s[i] = Integer.toString(i+1);
    }

     spin = (Spinner) findViewById(R.id.TimeSpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(TimeSpin.this,
            android.R.layout.simple_spinner_item, s);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin.setAdapter(adapter);

}

解决方案

You should Ignite OnItemselectedListener for this and set the selected value in Spinner

Check this Code :

    boolean flag_default_country=false;

    spin_country.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
                for (int i = 0; i < s.length; i++) {
                    if (flag_default_country == false) {
                        if (s[i].toString().equalsIgnoreCase("US")) {
                            spin_country.setSelection(i);
                            flag_default_country = true;
                        }
                    }
                }
            }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

spin_country is Spinner Object

first initialize the flag_default_country to false , then check for the String value selected by User that was stored in Some values. if the Array value at i'th position equals to the user selected value it will set the value at i in the Spinner.

For Saving the user selected value you can use the Shared preference.

Check this link for Shared Preference.

Let me know if you find any difficulty.

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

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