微调器 - 所选项目未进入textview

时间:2014-06-13 05:55:51

标签: android android-arrayadapter android-spinner

我正在创建一个应用程序,其中我使用了微调器和textview。 我希望所选项目应出现在textview中。

我已经制作了应用程序,但问题是它没有获取在字符串中选择的项目的值,并且应用程序正在强制关闭。 我该怎么办?

这是 MainActivity.class

public class MainActivity extends Activity {

            TextView tv;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Spinner spinner = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                    this, R.array.access_level,
                    android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                //  spinner.setPrompt("Choose any one");

            spinner.setAdapter(new NothingSelectedSpinnerAdapter(adapter,
                    R.layout.row_spinner, this));
            adapter.getItem(spinner.getSelectedItemPosition()); 

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> parent, View view,
                        int position, long id) {
                    Object item = parent.getItemAtPosition(position);
                    String str = ((TextView) view.findViewById(R.id.tv1)).getText().toString();
                    System.out.println(item);
                    System.out.println(str);
                }

                public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

}

activity_main.xml中

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

            <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            </Spinner>

            <TextView
            android:id="@+id/tv1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="" />
        </LinearLayout>

    </RelativeLayout>

这是我为微调器所采用的适配器 -

        NothingSelectedSpinnerAdapter.class - 


            public class NothingSelectedSpinnerAdapter implements SpinnerAdapter,
            ListAdapter {

                protected static final int EXTRA = 1;
                protected SpinnerAdapter adapter;
                protected Context context;
                protected int nothingSelectedLayout;
                protected int nothingSelectedDropdownLayout;
                protected LayoutInflater layoutInflater;

                public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
                        int nothingSelectedLayout, Context context) {

                    this(spinnerAdapter, nothingSelectedLayout, -1, context);
                }

                public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter,
                    int nothingSelectedLayout, int nothingSelectedDropdownLayout,
                    Context context) {
                this.adapter = spinnerAdapter;
                this.context = context;
                this.nothingSelectedLayout = nothingSelectedLayout;
                this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout;
                layoutInflater = LayoutInflater.from(context);
                }

                @Override
                public final View getView(int position, View convertView, ViewGroup parent) {

                    if (position == 0) {
                    return getNothingSelectedView(parent);
                    }
                    return adapter.getView(position - EXTRA, null, parent); // Could re-use

                }

                protected View getNothingSelectedView(ViewGroup parent) {
                return layoutInflater.inflate(nothingSelectedLayout, parent, false);
                }

                @Override
                public View getDropDownView(int position, View convertView, ViewGroup parent) {

                    if (position == 0) {
                        return nothingSelectedDropdownLayout == -1 ? new View(context)
                            : getNothingSelectedDropdownView(parent);
                    }

                // Could re-use the convertView if possible, use setTag...
                return adapter.getDropDownView(position - EXTRA, null, parent);
                }

                protected View getNothingSelectedDropdownView(ViewGroup parent) {
                return layoutInflater.inflate(nothingSelectedDropdownLayout, parent,
                        false);
                }

                @Override
                public int getCount() {
                int count = adapter.getCount();
                return count == 0 ? 0 : count + EXTRA;
                }

                @Override
                public Object getItem(int position) {
                return position == 0 ? null : adapter.getItem(position - EXTRA);
                }

                @Override
                public int getItemViewType(int position) {

                return position == 0 ? getViewTypeCount() - EXTRA : adapter
                        .getItemViewType(position - EXTRA);
                }

                @Override
                public int getViewTypeCount() {
                return adapter.getViewTypeCount() + EXTRA;
                }

                @Override
                public long getItemId(int position) {
                return adapter.getItemId(position - EXTRA);
                }

                @Override
                public boolean hasStableIds() {
                return adapter.hasStableIds();
                }

                @Override
                public boolean isEmpty() {
                return adapter.isEmpty();
                }

                @Override
                public void registerDataSetObserver(DataSetObserver observer) {
                adapter.registerDataSetObserver(observer);
                }

                @Override
                public void unregisterDataSetObserver(DataSetObserver observer) {
                adapter.unregisterDataSetObserver(observer);
                }

                @Override
                public boolean areAllItemsEnabled() {
                return false;
                }

                @Override
                public boolean isEnabled(int position) {
                return position == 0 ? false : true; // Don't allow the 'nothing
                                                        // selected'
                                                        // item to be picked.
                }

            }

这是微调器的xml -

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Select Access Level"
android:textColor="#808080"
android:textSize="18sp" />

3 个答案:

答案 0 :(得分:0)

  

我希望所选项目应显示在textview中。

setContentView()

之后放置以下行
tv = (TextView)findViewById(R.id.tv1);

并使用以下代码:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                String itemText = parent.getItemAtPosition(position).toString();
                tv.setText(itemText);
            }

            public void onNothingSelected(AdapterView<?> parent) {
        }
    });

希望它有所帮助。

答案 1 :(得分:0)

试试这个

MainActivity.class -

    public class MainActivity extends Activity {

        TextView tv;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,android.R.layout.simple_spinner_item,R.array.access_level);
                spinner.setAdapter(adapter);


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
               String item =(String) parent.getItemAtPosition(position);
                tv = (TextView) findViewById(R.id.tv1);
                tv.setText(item);
            }

            public void onNothingSelected(AdapterView<?> parent) {
        }
    });
}

答案 2 :(得分:0)

您在此处收到错误,因为您打印了对象&#34; item&#34;。同样您没有从微调器中获取所选项目,仍未将其设置为文本视图。将您的代码更改为:

tv = (TextView)findViewById(R.id.tv1);
 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                tv.setText(parent.getItemAtPosition(position).toString());
            }

            public void onNothingSelected(AdapterView<?> parent) {
        }
    });