0

从我的微调器中选择某些内容后,我想在按钮中显示某个值。但它没有做它的事情。请看一下我的编码:

 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>

我在这里缺少什么?请帮我找出这里出了什么问题。谢谢。

4

1 回答 1

0

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

于 2013-01-20T18:52:16.300 回答