将数组中的项添加到操作栏微调器

时间:2014-12-15 15:55:17

标签: android android-actionbar

我的android应用程序在选择导航项后出错。它无法将数组中的项添加到微调器。我的微调器在操作栏上。有人可以帮忙吗?

下面是失败的方法,其他人员工作除了hymn_num_spin.setAdapter(dataAdapter1); line:

        //adding items to the spinner
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>
                (getActionBar().getThemedContext(), android.R.layout.simple_spinner_item, Array_Hym_number);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
        //displaying all array items(Array_Hym_number) on the spinner(hymn_num_spin)
        hymn_num_spin.setAdapter(dataAdapter1); //

main_activity代码:

public class main_activity extends Activity {
    //Declaration
    private DrawerLayout drawerLayoutview;
    private ListView ListView_navDrawer;
    public ArrayList Array_Hym_number;
    public ArrayList Array_fillHymVerses;
    private ArrayList HymnLanglist;
    private ActionBarDrawerToggle drawerToggleListener;
    public static final String DB_NAME = "yourdbsqlite3.db";
    private SQLiteDatabase database;
    public Spinner hymn_num_spinner;
    public Spinner hymn_num_spin;
    private TextView Display_Hymn;
    private String TABLE_NAME="English";
    public TextView Dsp_verse;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity);

        drawerLayoutview = (DrawerLayout) findViewById(R.id.drawer_Layout);
        ListView_navDrawer = (ListView) findViewById(R.id.drawer_listview);
        hymn_num_spinner = (Spinner) findViewById(R.id.action_spinner);
        hymn_num_spin = (Spinner) findViewById(R.id.hymno_spinner);
        Dsp_verse = (TextView) findViewById(R.id.Dsp_verses);
        //Display_Hymn=(TextView) findViewById(R.id.Display_song);
        //Our key helper
        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.openDataBase();

        //populate Hymn laugages from array
        final List<String> HymnLanglist = new ArrayList<String>();
        HymnLanglist.add("English");
        HymnLanglist.add("Venda");
        HymnLanglist.add("Zulu");
        HymnLanglist.add("Tsonga");
        HymnLanglist.add("Sotho");

        hymn_num_spin.setOnItemSelectedListener(new HymnNumberOnItemSelectedListener());

        final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, HymnLanglist);
        ListView_navDrawer.setAdapter(dataAdapter);

        ListView_navDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Toast.makeText(getApplicationContext(), HymnLanglist.get(position) + " Selected", Toast.LENGTH_SHORT).show();
                drawerLayoutview.closeDrawer(ListView_navDrawer);//this method will close navigation drawer when the item is selected
                setTitle(HymnLanglist.get(position)); //this method will change the title to selected item on listview

                TABLE_NAME = HymnLanglist.get(position);//Set the table name before calling fillHymNumber() method
                fillHymNumber();

            }

        });
        //navigation drawer open and closed behaviour
        drawerToggleListener = new ActionBarDrawerToggle(this, drawerLayoutview, R.drawable.drawerbar, R.string.Open_drawer, R.string.Closed_drawer) {
            public void onDrawerOpened(View DrawerView) {
                hymn_num_spin.setVisibility(View.GONE);
                Dsp_verse.setVisibility(View.GONE);
                super.onDrawerOpened(DrawerView);

                // Toast.makeText(getApplicationContext(),"Drawer Opened", Toast.LENGTH_SHORT).show();

            }

            public void onDrawerClosed(View DrawerView) {

                super.onDrawerClosed(DrawerView);
                hymn_num_spin.setVisibility(View.VISIBLE);
                Dsp_verse.setVisibility(View.VISIBLE);
                //Toast.makeText(getApplicationContext(),"Drawer Closed", Toast.LENGTH_SHORT).show();
            }
        };
        drawerLayoutview.setDrawerListener(drawerToggleListener);
        getActionBar().setHomeButtonEnabled(true);//make action bar icon/title clickable
        getActionBar().setDisplayHomeAsUpEnabled(true);// Enabling Up / Back navigation
    }

 private void fillHymNumber() {
        TABLE_NAME="English"
        Toast.makeText(getApplicationContext(), TABLE_NAME + " Table Selected", Toast.LENGTH_SHORT).show();
        Array_Hym_number = new ArrayList<String>();
        //reading data from the database
        Cursor friendCursor = database.query(TABLE_NAME, new String[]{"hymn_org_no",
                "hymn_new_no"}, null, null, null, null, "hymn_new_no");
        friendCursor.moveToFirst();
        if (!friendCursor.isAfterLast()) {
            do {
                //adding only hymn_new_no on the array
                Array_Hym_number.add(friendCursor.getString(1));
            } while (friendCursor.moveToNext());
        }
        friendCursor.close();
        //adding items to the spinner
        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>
                (getActionBar().getThemedContext(), android.R.layout.simple_spinner_item, Array_Hym_number);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
        //displaying all array items(Array_Hym_number) on the spinner(hymn_num_spin)
        hymn_num_spin.setAdapter(dataAdapter1);
    }

activity_main_activity代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Spinner
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/action_spinner"
        android:entries="@array/Array_entries"


        />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

我试图从android布局添加微调器,我设法让旋转器显示在操作栏上,但它不允许我在运行时从数组中添加项目.Android自带的旋转器项目中的buld无需添加它来自布局。唯一需要的是使用ActionBar.setNavigationMode(ActionBar.NAVIGATION_LIST)启用它,然后你需要设置ListNavigationCallbacks和OnNavigationItem。之后我设法在运行时从数组中添加项目