Android:点击按钮获取Spinner的Textview值

时间:2016-04-19 08:48:10

标签: java android android-spinner

我有一个Spinner,我使用自定义SimpleCursorAdapter填充它。 Spinner项目布局包含两个TextView s,一个TextView用于项目ID,它不可见,其他用于项目名称。我想在按钮单击事件上获取此项ID,然后将其插入Sqlite数据库。我在setOnItemSelectedListener的{​​{1}}上获得了

的ID
Spinner

和Spinner项目布局

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) {


            }
        });

但是无法按下按钮。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

我认为你正在寻找这个

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) {

        }
    });

在一些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}    

            }
        });

答案 1 :(得分:0)

你有与你的Spinner关联的适配器,我想你有一些与Adapter相关的数据结构。您可以获取onItemSelected所需的数据(如下所示)

postData.filters

myData可以是“活动”字段,您可以稍后使用与您的按钮相关联的onClick回调。

答案 2 :(得分:0)

你可以试试这个

function validate()
{
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    var Userlist = [{"username":"asdf", "password":"123"}, {"username":"zxc", "password":"123"}];
    var logged = false;

    for (var i=0; i < Userlist.length; i++) {
      if ( username == Userlist[i].username && password == Userlist[i].password)
          logged=true;
    }

    if(logged)
        alert ("Login successfully");
    else
        alert("Invalid username and password");
}

其中,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) { } }); 是您传递给Spinner Adapter的ArrayList。希望这有效。

答案 3 :(得分:-1)

将您的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>

现在使用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) {


            }
        });