从按钮中显示的微调器中选择的项目

时间:2013-01-20 18:44:34

标签: android button spinner

我想在我的微调器中选择一些东西时在按钮上显示某个值。但它并没有做到这一点。请看一下我的编码:

 public void onItemSelected(AdapterView<?> parent, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
    selected = parent.getItemAtPosition(position).toString();
    int index = parent.getSelectedItemPosition();

    if(index == 0){
        strCalories = "188 calories";
        calories.setText(strCalories);
    }
    else if ( index == 1 ){
        strCalories = "185 calories";
        calories.setText(strCalories);
    }
    else if ( index == 2 ){
        strCalories = "126 calories";
        calories.setText(strCalories);
    }
    else if ( index == 3 ){
        strCalories = "149 calories";
        calories.setText(strCalories);
    }
    else if ( index == 4 ){
        strCalories = "81 calories";
        calories.setText(strCalories);
    }
    else if ( index == 5 ){
        strCalories = "66 calories";
        calories.setText(strCalories);
    }
    else if ( index == 6 ){
        strCalories = "46 calories";
        calories.setText(strCalories);
    }
    else if ( index == 7 ){
        strCalories = "89 calories";
        calories.setText(strCalories);
    }

}

        public void onClick(View v) {
    // TODO Auto-generated method stub

        switch( v.getId() ){
    case R.id.btBreadWhiteSave:
        String mealname = selected;

        String servng = calories.getText().toString();

        if ( ( mealname.isEmpty() || servng.isEmpty() ) ){

            // call for custom toast
            viewErrorToast();
        }

        else {

        boolean didItWork = true;

        try{

            BreakFastLog entry = new BreakFastLog(Bread_White.this);
            entry.open();

            entry.createEntry(mealname, servng);

            entry.close();
            }
            catch(Exception e){
                didItWork = false;
                viewErrorToast();
            }finally{
                if (didItWork){
                    viewBMRSavedToast();
                }

            }
        } // end of if else statement
        break;

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/ihealthfirst" >

<include
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    layout="@layout/tabs_menu" />

<TextView
    android:id="@+id/tvWhiteBread"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/whitebread"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
    android:id="@+id/spWhiteBread"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/white_bread_prompt"
    android:entries="@array/white_bread_arrays" />

<TextView
    android:id="@+id/btBreadWhiteCalories"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btBreadWhiteSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/save" />

</LinearLayout>

我在这里想念的是什么?请帮我找到这里出了什么问题。感谢。

1 个答案:

答案 0 :(得分:0)

使用传入的position作为onItemSelected回调的参数作为索引。不是getSelectedItemPosition()