简体   繁体   中英

android: Problem with Spinner

In my main.xml, I have a Spinner (along with other components).

            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/SelProtocol_main" android:id="@+id/textView1_main"></TextView>
        <Spinner android:layout_width="wrap_content" android:layout_weight="1" 
        android:layout_height="wrap_content" android:id="@+id/cmb_protocol_main"
        android:drawSelectorOnTop="true"></Spinner>

In res/values I have protocol_array.xml with string-array name="protocols".

In my Activity, in a method called from onCreate I have,

        prtlSpinner = (Spinner)findViewById(R.id.cmb_protocol_main);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.protocols, R.layout.main);   // HERE I GET ERROR
    adapter.setDropDownViewResource(R.layout.main);
    prtlSpinner.setAdapter(adapter);
    /*
    prtlSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            selectedProtocolIndex = prtlSpinner.getSelectedItemPosition();
            selectedProtocol = prtlSpinner.getSelectedItem().toString();
            Log.i(TAG, "prtlSpinner tem Selected = " + selectedProtocol + " Index = " + selectedProtocolIndex);             
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            selectedProtocolIndex = -1;
            selectedProtocol = "";
        }

    });
    */

Exception I get : Need a TextView. I tried differnt ways, but nothing worked. With the above code, I get NullPointerException.Spinner code is coded using http://www.brighthub.com/mobile/google-android/articles/46782.aspx . I couldn't figure out from docs also.

Can anyone help me where am going wrong ? And why do we need a TextView to fill up dropbox ? Te listener code is correct or not - I just need to set the variable on selection with index and text of the selected item ! I couldn't get with Spinner. Any help, guidance is highly appreciated.

Thanks

your main.xml contains 2 members, a textview and a Spinner, whereas the arrayAdapter expects a textview. So you can either use android.R.layout.simple_spinner_item or try passing R.id.textView1_main

Oh Finally, worked out. main.xml as it is. And in java class,

        ArrayAdapter<CharSequence> adapter = null;
    adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, array_spinner);    
    prtlSpinner.setAdapter(adapter);        
    prtlSpinner.setOnItemSelectedListener(this);

That's it. array_spinner is a String[] in the class itself. All got well and working smoothly. No need for any other layout or so.

Thanks for trying to help me out. @frieza, I highly appreciate your support & guidance. Thanks alot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM