0

actionbarsherlock でタブ ナビゲーションを使用したいのですが、タブ レイアウトの 1 つにスピナーがあります。私の問題は、スピナーの .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 に答える