我想在String中存储微调器值并使用onItemselectedListener

时间:2016-02-19 12:19:01

标签: android

我的代码不起作用,我没有得到textview的值,假设我的微调器弹出,我选择其他值,而不是同时我的textview值应该改变,旋转器项目的字符串值被选中

    package com.vedicrishiastro.kundli.Screens.Extras;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.internal.widget.AdapterViewCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.vedicrishiastro.kundli.R;
import com.vedicrishiastro.kundli.Screens.AbstractActivity;

public class Settings extends AbstractActivity implements View.OnClickListener {

    private LinearLayout linearSelectLang,linearSetDefault,linearSelectPanch;
    private TextView txtSelectLang,txtSetDefault,txtSelectPanch;
    private Spinner spinner1,spinner2,spinner3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        linearSelectLang = (LinearLayout)findViewById(R.id.LinearSelectLang);
        linearSetDefault = (LinearLayout)findViewById(R.id.LinearSetDefault);
        linearSelectPanch = (LinearLayout)findViewById(R.id.LinearSelectPanch);

        spinner1 = (Spinner)findViewById(R.id.settingSpinner1);
        spinner2 = (Spinner)findViewById(R.id.settingSpinner2);
        spinner3 = (Spinner)findViewById(R.id.settingSpinner3);

        txtSelectLang = (TextView)findViewById(R.id.selectLangtext);
        txtSetDefault = (TextView)findViewById(R.id.setdefaulttext);
        txtSelectPanch = (TextView)findViewById(R.id.selectPanchtext);

        linearSelectLang.setOnClickListener(this);
        linearSetDefault.setOnClickListener(this);
        linearSelectPanch.setOnClickListener(this);
        spinner1.setVisibility(View.GONE);
        spinner2.setVisibility(View.GONE);
        spinner3.setVisibility(View.GONE);
    }

    public void onClick(View view){
        int id = view.getId();
        switch (id){
            case R.id.LinearSelectLang:
            {
                spinner1.performClick();
                spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {
                            txtSelectLang.setText("English");
                        }
                        else
                        {
                            txtSelectLang.setText("हिंदी");
                        }
                    }

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

                    }
                });

                spinner1.setVisibility(View.GONE);
            }
            break;
            case R.id.LinearSetDefault:
            {
                spinner2.performClick();
                String text = spinner2.getSelectedItem().toString();
                txtSetDefault.setText(text);
                spinner2.setVisibility(View.GONE);
            }
            break;
            case R.id.LinearSelectPanch:
            {
                spinner3.performClick();
                String text = spinner3.getSelectedItem().toString();
                txtSelectPanch.setText(text);
                spinner3.setVisibility(View.GONE);
            }
            break;
        }
    }

}

1 个答案:

答案 0 :(得分:0)

在您的情况下,您应首先在onItemSelectedListener之外设置onClick,以获得更好的语法。但这不是不开火的情况。情况是,您设置performClick() 之前您将spinner设置为ItemSelectedListener()。您必须第一次设置onItemSelectedListener,然后致电performClick()

并且对于您的spinner2spinner3,您尚未设置任何onItemSelectedListener()

修改

要清除,请设置执行点击 AFTER onItemSelectedListener()

 case R.id.LinearSelectLang:
            {

                spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {
                            txtSelectLang.setText("English");
                        }
                        else
                        {
                            txtSelectLang.setText("हिंदी");
                        }
                    }

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

                    }
                });

                spinner1.setVisibility(View.GONE);
            }

            spinner1.performClick() //here set performClick
            break;

但是真的....设置onItemSelectedListener OUTSIDE on Button