actionbar sherlock tabs with spinner and .setOnIte

2019-08-22 07:18发布

I would like to use the tab navigation on actionbarsherlock and I have a spinner in one of the tab layouts. My Problem is: the .setOnItemSelectedListener() for the spinner is never called. what I am doing wrong?

BtW. the array adapter does not fill in the items, I must put

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

attached the class:

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;
    }

}
}

thanks

1条回答
Bombasti
2楼-- · 2019-08-22 07:59

solution found!

the definition of the view items should go to the onTabSelected method:

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());

should go here:

@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);
    }
}
查看更多
登录 后发表回答