2017-07-19 3 views
0

私は、ボタンリスナーで 'selectedTeam'にアクセスしたいという次のコードを持っています。スピナーリスナーから変数にアクセスする方法は?

 //Adding setOnItemSelectedListener method on spinner. 
     sTeams.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, 
             int position, long id) { 
       selectedTeam = parent.getItemAtPosition(position).toString(); 
       editText.setText(selectedTeam, TextView.BufferType.EDITABLE); 
      } 

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

     buttonApply.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       String editedName = editText.getText().toString(); 
       // Here I want to access selectedTeam 
      } 
     }); 

Iメソッド外変数を宣言しようとしたが、それはエラーを内部クラス内からアクセス「変数 『selectedTeam'isを与え、』最終的な宣言される必要があります。私はそれを試みましたが、最終的な文字列は変更できないので、これは機能しません。

答えて

1

クラスメンバを代わりに使用してください。 JLS 8.1.3. Inner Classes and Enclosing Instances

(宣言静的 コンテキストでは発生しない)内部クラスは 字句的に取り囲むクラスのメンバーであるインスタンス変数を参照し、対応の可変字句的に のインスタンスを使用しています。

ローカル変数、仮パラメータ、または例外パラメータが使用されますが、内部クラスで宣言されていない は、final宣言する必要があります。

それはあなただけ匿名内部クラスにfinal外の変数や包含するクラスのメンバーを使用できることを意味します。

[...] 
private String selectedTeam; 
[...] 

//Adding setOnItemSelectedListener method on spinner. 
    sTeams.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 
            int position, long id) { 
      selectedTeam = parent.getItemAtPosition(position).toString(); 
      editText.setText(selectedTeam, TextView.BufferType.EDITABLE); 
     } 

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

    buttonApply.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      String editedName = editText.getText().toString(); 
      if (selectedTeam != null && !"".equals(selectedTeam)) { 
       // Do something 
      } 
     } 
    }); 
+0

ありがとう(XMLファイルを使用する場合は、あなたのアプリのパッケージを変更すると思います)説明。私は今、私の問題を解決する方法はまだ分かっていませんが、理解しています。メソッドの外側から 'selectedTeam'にアクセスするにはどうすればいいですか? –

+0

申し訳ありませんが悪いです。私は 'selectedTeam'に編集しました。'selectedTeam'はクラスメンバーを囲んでいますので、あなたは匿名クラスの内側と外側にアクセスできます。 –

+0

@ no-nameシャドウイングの問題を防ぐために、 'selectedTeam'宣言を外側に置かないようにしてください。あるいは単に内部クラスで' selectedTeam'の代わりに 'ClassName.this.selectedTeam'を使うだけです。 –

1

変数グローバルを宣言する必要があります。

public class MainActivity extends AppCompatActivity { 

String selectedteam; 
... 
buttonApply.setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 
      String editedName = editText.getText().toString(); 
      selectedteam = editedName; // or whatever you want 
     } 
    }); 
0

私の場合は、値を保存して使用する値を取得するために共有プリファレンスを使用します。値を取得する方法

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
editor.putString("name", "selectedTeam"); 
editor.apply(); 

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("name", null); 

を、それはあなたが別の方法でそれを行う必要があり

0

に役立ちます願っています。 これは私にとっては、AdapterView.OnItemSelectedListenerとView.OnClickListenerでアクティビティを実装し、メソッドをオーバーライドしています。

これで、OnClickメソッドからselectedTeamにアクセスできるようになりました。

MainActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener,View.OnClickListener{ 

    Spinner sTeam; 
    EditText editText; 
    Button button; 
    String selectedTeam; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     sTeam = (Spinner) findViewById(R.id.spinner_team); 
     button = (Button) findViewById(R.id.confirmation_button); 
     editText = (EditText) findViewById(R.id.edittext_team); 


     List<String> spinnerArray = new ArrayList<String>(); 
     spinnerArray.add("team1"); 
     spinnerArray.add("team2"); 
     spinnerArray.add("team3"); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       this, android.R.layout.simple_spinner_item, spinnerArray); 

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     sTeam.setAdapter(adapter); 

     button.setOnClickListener(this); 
     sTeam.setOnItemSelectedListener(this); 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     selectedTeam = parent.getItemAtPosition(position).toString(); 
     editText.setText(selectedTeam, TextView.BufferType.EDITABLE); 
    } 

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

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()){ 
      case (R.id.confirmation_button) :{ 
       //You can access to selected team here 
      }break; 
     } 
    } 


} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.yourpackage.test"> 

    <Spinner 
     android:id="@+id/spinner_team" 
     android:layout_width="368dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintHorizontal_bias="0.0" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <Button 
     android:id="@+id/confirmation_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_marginTop="8dp" 
     app:layout_constraintTop_toBottomOf="@+id/edittext_team" 
     app:layout_constraintLeft_toRightOf="@+id/edittext_team" 
     android:layout_marginLeft="8dp" /> 

    <EditText 
     android:id="@+id/edittext_team" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginTop="8dp" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:text="Name" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/spinner_team" /> 

</android.support.constraint.ConstraintLayout> 
関連する問題