0

我想在 actionbarsherlock 上使用选项卡导航,并且我在其中一个选项卡布局中有一个微调器。我的问题是:永远不会调用微调器的 .setOnItemSelectedListener() 。我做错了什么?

顺便提一句。数组适配器没有填写项目,我必须把

<Spinner 
        android:id="@+id/sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:entries="@array/Types" />

附上课程:

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;

public class Main extends SherlockActivity implements ActionBar.TabListener,
    OnClickListener {
static int THEME = R.style.Theme_Sherlock_Light_DarkActionBar;
private Spinner sp;
private TextView tvtest;
private Button btTest;

@Override
public void onCreate(Bundle savedInstanceState) {
    setTheme(THEME); // Used for theme switching in samples
    super.onCreate(savedInstanceState);

    setContentView(R.layout.tab_dataentry);
    sp = (Spinner) findViewById(R.id.sp);
    tvtest = (TextView) findViewById(R.id.tvTest);



    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, getResources()
                    .getStringArray(R.array.Types));

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(aa);
    sp.setOnItemSelectedListener(new mySpinnerListener());
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = getSupportActionBar().newTab();
    tab1.setText("Data Entry");
    tab1.setTabListener(this);
    getSupportActionBar().addTab(tab1);

    ActionBar.Tab tab2 = getSupportActionBar().newTab();
    tab2.setText("Calculation");
    tab2.setTabListener(this);
    getSupportActionBar().addTab(tab2);

    ActionBar.Tab tab3 = getSupportActionBar().newTab();
    tab3.setText("Results");
    tab3.setTabListener(this);
    getSupportActionBar().addTab(tab3);

}

class mySpinnerListener implements Spinner.OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> parent, View v, int position,
            long id) {
        // TODO Auto-generated method stub
        Toast.makeText(parent.getContext(), "&quot;The day is &quot; "+
        parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // TODO Auto-generated method stub
        // Do nothing.
    }

}

@Override
public void onTabReselected(Tab tab, FragmentTransaction transaction) {

}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
    if (tab.getText().equals("Data Entry")) {
        setContentView(R.layout.tab_dataentry);
    }
    if (tab.getText().equals("Calculation")) {
        setContentView(R.layout.tab_calc);
        btTest = (Button) findViewById(R.id.btCalc);
        btTest.setOnClickListener(this);
    }
    if (tab.getText().equals("Results")) {
        setContentView(R.layout.tab_result);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction transaction) {
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.btCalc:
        Toast.makeText(this, "" + spSoil.getSelectedItem().toString(),
                Toast.LENGTH_LONG).show();
        break;
    }

}
}

谢谢

4

1 回答 1

1

找到解决方案!

视图项的定义应该去 onTabSelected 方法:

sp = (Spinner) findViewById(R.id.sp);
tvtest = (TextView) findViewById(R.id.tvTest);



ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, getResources()
                .getStringArray(R.array.Types));

aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(new mySpinnerListener());

应该去这里:

@Override
public void onTabSelected(Tab tab, FragmentTransaction transaction) {
    if (tab.getText().equals("Data Entry")) {
        setContentView(R.layout.tab_dataentry);

        sp = (Spinner) findViewById(R.id.sp);
                    tvtest = (TextView) findViewById(R.id.tvTest);



                    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
                      android.R.layout.simple_spinner_item, getResources()
                     .getStringArray(R.array.Types));

                    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    sp.setAdapter(aa);
                    sp.setOnItemSelectedListener(new mySpinnerListener());

    }
    if (tab.getText().equals("Calculation")) {
        setContentView(R.layout.tab_calc);
        btTest = (Button) findViewById(R.id.btCalc);
        btTest.setOnClickListener(this);
    }
    if (tab.getText().equals("Results")) {
        setContentView(R.layout.tab_result);
    }
}
于 2013-07-30T11:39:07.340 回答