动态微调器无法正常工作

时间:2016-05-29 03:53:57

标签: android android-studio android-spinner

我有一个spinner2,它有日期和月份,当用户选择日期时,s1和s2将分别显示小时和分钟。

在活动启动时,我每天都会选择。 问题是我每天都要继续烤面包。这表明它无休止地被选中。此外,我无法在s1和s2中选择值,微调器在它们之间不断刷新。怎么解决这个问题?

public class try_spinner extends Activity implements AdapterView.OnItemSelectedListener {
Spinner spinner1, spinner2, s1, s2, s3;
String app_name, room_name;
TextView app, room;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.try_spinner);

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;
int height = dm.heightPixels;

getWindow().setLayout((int) (width * .9), (int) (height * .7));

Bundle extras = getIntent().getExtras();
final String app_string = extras.getString("APPLIANCE_NAME");
final String room_string = extras.getString("APPLIANCE_ROOM");

app = (TextView) findViewById(R.id.app_name);
app.setText(app_string);
room = (TextView) findViewById(R.id.room_name);
room.setText(room_string);

spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);

spinner2 = (Spinner) findViewById(R.id.spinner2);
s1 = (Spinner) findViewById(R.id.s1);
s2 = (Spinner) findViewById(R.id.s2);
s3 = (Spinner) findViewById(R.id.s3);
spinner2.setOnItemSelectedListener(this);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
s3.setOnItemSelectedListener(this);

if (app_string.equals("Lighting")) {
    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lighting));
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter2.notifyDataSetChanged();
    spinner1.setAdapter(dataAdapter2);
} else if (app_string.equals("Shower Heater")) {
    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.shower_heater));
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter2.notifyDataSetChanged();
    spinner1.setAdapter(dataAdapter2);
} else {
    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter2.notifyDataSetChanged();
    spinner1.setAdapter(dataAdapter2);
}

}


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                       long arg3) {
// TODO Auto-generated method stub
String txt_spinner2 = String.valueOf(spinner2.getSelectedItem());
Toast.makeText(this, txt_spinner2, Toast.LENGTH_SHORT).show();
if (txt_spinner2.contentEquals("Daily")) {
    List<String> list = new ArrayList<String>();
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.days));
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter.notifyDataSetChanged();
    s1.setAdapter(dataAdapter);
    String txt_s1 = String.valueOf(s1.getSelectedItem());

    //set for daily hours usage
    List<String> list3 = new ArrayList<String>();
    ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
    dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter3.notifyDataSetChanged();
    s2.setAdapter(dataAdapter3);

    //set for daily minutes usage
    List<String> list4 = new ArrayList<String>();
    ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
    dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter4.notifyDataSetChanged();
    s2.setAdapter(dataAdapter4);

    //set for daily empty thrd space
    List<String> list5 = new ArrayList<String>();
    ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
    dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter5.notifyDataSetChanged();
    s3.setAdapter(dataAdapter5);
}

else if (txt_spinner2.contentEquals("Monthly")) {
    List<String> list = new ArrayList<String>();
    list.add("Conveyance");//you should add items from db here(2nd spinner)
    list.add("the end");
    ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    dataAdapter6.notifyDataSetChanged();
    s3.setAdapter(dataAdapter6);
}

}

1 个答案:

答案 0 :(得分:1)

首先将适配器首先设置为spinner,然后为它调用setOnItemSelectedListener()。 b'z当spinner值初始化时,它甚至调用setOnItemSelectedListener方法。你可以把调试器和检查。

其次总是检查你得到哪个微调器或小部件事件,在你的情况下你可以这样做的旋转器

public void onItemSelected(AdapterView<?> parent, View view, int position,
                               long id) {

    Spinner clickedSpinner = (Spinner) parent;

    switch (clickedSpinner.getId()) {
        case R.id.spinner1:
            // your code for spinner1
            String str = spinner1.getSelectedItem().toString();
            break;
    }
}

所以更改您的代码并查看其是否正常工作。希望它会帮助你...