簡體   English   中英

Android:單擊按鈕時獲取 Spinner 的 Textview 值

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

我有一個Spinner ,我正在用自定義SimpleCursorAdapter填充它。 Spinner 項目布局包含兩個TextView ,一個 TextView 用於項目 id,它不可見,另一個用於項目名稱。 我想在按鈕單擊事件上獲取此項目 ID,然后將其插入到 Sqlite 數據庫中。 我在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) {


            }
        });

和微調項目布局

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

但無法通過按鈕點擊。 任何幫助,將不勝感激。

我想你正在尋找這個

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}    

            }
        });

您有與 Spinner 相關聯的適配器,我想您有一些與 Adapter 相關聯的數據結構。 您可以在 ItemSelected 上獲取您需要的數據(如下所示)

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

myData 可以是 Activity 字段,您可以稍后使用與您的按鈕相關聯的 onClick 回調。

你可以試試這個

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


        }
    });

其中, companyList是您傳遞給 Spinner Adapter 的 ArrayList。 希望這有效。

將您的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) {


            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM