简体   繁体   English

Android:单击按钮时获取 Spinner 的 Textview 值

[英]Android: Get Textview value of Spinner on Button click

I have a Spinner and i am populating it with custom SimpleCursorAdapter .我有一个Spinner ,我正在用自定义SimpleCursorAdapter填充它。 Spinner item layout contains two TextView s, One TextView for item id and it is not visible other is for item name. Spinner 项目布局包含两个TextView ,一个 TextView 用于项目 id,它不可见,另一个用于项目名称。 I want to get this item id on button click event then insert it to Sqlite database.我想在按钮单击事件上获取此项目 ID,然后将其插入到 Sqlite 数据库中。 I get the id on setOnItemSelectedListener of Spinner as我在Spinner setOnItemSelectedListener上得到 id 作为

companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                // Get selected row data to show on screen
                String companyId = ((TextView) view.findViewById(R.id.spinnerItemIdTv)).getText().toString();
                Toast.makeText(getActivity(), companyId, Toast.LENGTH_LONG).show();
                Log.w(TAG, "companyId:" + companyId);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {


            }
        });

and Spinner Item Layout和微调项目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/spinnerItemIdTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#888"
        android:textSize="20sp"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/spinnerItemNameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#888"
        android:textSize="20sp" />

</LinearLayout>

But couldnt make it on button click.但无法通过按钮点击。 Any help would be appreciated.任何帮助,将不胜感激。

I think you are searching for this我想你正在寻找这个

View selectedView = null;  //Declare it as a class level variable so that you don't need to make it final  

spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedView = view;
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

And Inside some Button's click event, do like this在一些 Button 的点击事件中,这样做

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {    
                if(selectedView!=null){                        
                    String companyId = ((TextView) selectedView.findViewById(R.id.spinnerItemIdTv)).getText().toString();
                }
               else{//Something}    

            }
        });

You have adapter associated with your Spinner and I suppose you have some data structure associated with Adapter.您有与 Spinner 相关联的适配器,我想您有一些与 Adapter 相关联的数据结构。 You can get data you need onItemSelected (something like below)您可以在 ItemSelected 上获取您需要的数据(如下所示)

public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    myData = myArrayList.get(i);
//OR
    myData = myAdapter.getItem(i);
}

myData can be Activity field and you can use later onClick callback assosiated with your button. myData 可以是 Activity 字段,您可以稍后使用与您的按钮相关联的 onClick 回调。

You may try this你可以试试这个

companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            Spinner spinComp = (Spinner) view.findViewById(R.id.spinner_company);
            int selCompIndex = spinComp .getSelectedItemPosition();
            String compID = companyList.get(selCompIndex).toString();
            Toast.makeText(getActivity(), compID, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {


        }
    });

Where, companyList is ArrayList which you are passing to Spinner Adapter.其中, companyList是您传递给 Spinner Adapter 的 ArrayList。 Hope this works.希望这有效。

Modify your xml to single TextView and use tag for ID.将您的xml修改为单个TextView并使用tag作为 ID。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/spinnerItemNameTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="yourId"
        android:textColor="#888"
        android:textSize="20sp" />

</LinearLayout>

Now use setText to set the spinner row value and use setTag to set the ID of row and then get the ID like this.现在使用setText设置微调器行值并使用setTag设置行的 ID,然后像这样获取 ID。

companySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


                String companyId = ((TextView) view.findViewById(R.id.spinnerItemNameTv)).getTag().toString(); //use parent instead.
                Toast.makeText(getActivity(), companyId, Toast.LENGTH_LONG).show();
                Log.w(TAG, "companyId:" + companyId);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {


            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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