2017-11-28 24 views
0

私のスピナー(レイアウト)の選択したアイテムのビューへの参照を取得しようとしています。私はカスタムレイアウトでSimpleCursorAdapterを使用しています。ここでスピナーの選択したアイテムのビューを取得する方法

adapter = new SimpleCursorAdapter(getActivity(), 
      R.layout.task_row, null, 
      new String[] { DbHandler.TASK_TITLE, DbHandler.TASK_NOTE}, 
      new int[] { R.id.title, R.id.note}, 0); 
spinner.setAdapter(adapter); 

task_row.xmlされています:

<RelativeLayout> 

<ImageView 
    android:id="@+id/avatar" 
    android:src="@mipmap/ic_launcher" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/avatar" /> 

<TextView 
    android:id="@+id/title" 
    android:textStyle="bold" 
    android:layout_toRightOf="@id/avatar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/note" 
    android:layout_toRightOf="@id/avatar" 
    android:layout_below="@id/title" 
    android:layout_alignParentBottom="true" 
    android:gravity="bottom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</RelativeLayout> 

私はRelativeLayoutへの参照を取得しようとしている理由は、私ができることであるここに私のアダプタのコードの一部ですたとえばTextView子のtext属性にアクセスします。私はgetItemSelected()というメソッドがあり、私はSpinnerで使用できると知っていますが、このメソッドはオブジェクトを返して、それが何であるかわかりません。

+0

こんにちは私は、カスタムアダプタを持っているし、アダプタからあなたがスピナーを取得したいと思います値 –

+0

こんにちは。私はSimpleCursorAdapter(https://developer.android.com/reference/android/widget/SimpleCursorAdapter.html)を使用しています。ここでは、スピナーの "値"は意味がありません。なぜなら、表示されるのはImageViewと2つのTextViewを持つ私のカスタムレイアウトです。私が欲しいのは、選択した項目のTextView子のテキスト属性です。それがアダプタから取得することが可能な場合は、確かに。 –

+0

あなたはビューから価値を得ようとしましたか? –

答えて

0

、nullを返し

spinner.getSelectedItem() 

は、そう、これを使用します。

((TextView)(spinner.getSelectedView().findViewById(R.id.title))).getText() 

私はそれがあなたのために働くと思います。この場合

、あなたは私がでを考えても、全体選択したビュー、あなたの要件である を持つことになります。

spinner.getSelectedView() 
+0

これは私が探していたものです。我々はほぼ同時に回答を掲載した。私はあなたを受け入れています。ありがとう。 –

+0

ありがとうございます。それもアップロードしてください。 –

0

あなたは、あなたのレイアウトでテキストビューへの参照を取得したい場合は、私はそうと仮定してい

    spinnner.setSelection(arraylist.indexOf(selectedString)); 
+0

申し訳ありませんが、私はあなたの答えを理解していません。これはどのように私にレイアウトへの参照を与えるのですか?さらに、あなたのarraylist変数は何ですか? –

+0

ここで配列リストは、リストデータ全体を格納する場所を参照します。 –

+0

selectedStringは、どの文字列またはデータをスピナーの最初の位置で選択するかを示します –

0

スピナーインデックスの上部で選択した値を表示するために役立つだろう、このコードを使用することができますあなたは、ユーザが選択し、選択スピナー項目に基づいて、文字列リソースを設定することができる、スピナーを持っているあなたのonCreateメソッドでこのコードを配置:

// create a reference to the spinner, replace mySpinnerId with actual spinner id 
final Spinner mySpinner = (Spinner) findViewById(R.id.mySpinnerId); 
// create a reference to the text view, replace myTextViewId with actual text view id 
final TextView textView = (TextView) findViewById(R.id.myTextViewId); 

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
    { 
     @Override 
     public void onItemSelected(AdapterView<?> parameter, View view, int position, long id) 
     { 
      // get the string value of the spinner item that is selected 
      String selectedSpinnerItem = String.valueOf(mySpinner.getSelectedItem()); 

      // set the description in the Text View if it matches some 
      // string resource in your array, replace option1 with actual 
      // string resource to obtain 
      if (selectedSpinnerItem.equals(getString(R.string.option1))) 
      { 
       // replace myStringResource with actual string resource name 
       textView.setText(R.string.myStringResource); 
      } 

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

これはこれはあなたの文字列では、ということです作業するための注意点。 xmlファイルで、スピナーの文字列配列を以下のように定義します:

<string name="option1">An option:</string> 
<string name="option2">Another option:</string> 
<string-array name="spinner_options"> 
    <item>@string/option1</item> 
    <item>@string/option2</item> 
</string-array> 
+0

このコードはどこにありますか?アクセスしようとしているTextViewがスピナーと同じレイアウトに属していないため、レイアウトにスピナーが含まれているアクティビティに移動することはできません。 –

+0

@JalilCompaoréああ、あなたが今何を言っているのか分かります。しかし、あなたがidによってテキストビューへの参照を持っているので、それは問題ではないと思います、それを試しましたか? –

+0

@JalilCompaoréだから、私はこのコードがまだ適用されていると思う、あなたはそれを適用することはできませんスピナー –

0

私は私の問題を解決しました。 Spinnerクラスは、という名前のメソッドを持つAdapterView<SpinnerAdapter>を拡張しています。代わりに使用したの

関連する問題