2016-08-18 18 views
1

私はスピナー(2)の内容を別のスピナー(1)から変更しようとしています。Java - スピナーでスピナーの値を変更

ここに私のコードです:

私は現在、『:(28、57)エラーArrayAdapter <のための型引数を推論することはできません>エラーは、』第二スピナのアレイアダプタのエラーを取得しています

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.Spinner; public class DetailsForm extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.details_form); final Spinner carBrand = (Spinner) findViewById(R.id.carBrand); String[] items = new String[]{"Honda", "Nissan", "Toyota"}; ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items); carBrand.setAdapter(adapter); carBrand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id){ String carBrand = parent.getSelectedItem().toString(); if (carBrand.equals("Nissan")) { final Spinner model = (Spinner) findViewById(R.id.model); String[] models = new String[]{"S14", "R34", "240SX"}; ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models); model.setAdapter(modeladapter); } else if (carBrand.equals("Honda")) { Spinner model = (Spinner) findViewById(R.id.model); String[] models = new String[]{"Civic", "NSX", "Accord"}; ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models); model.setAdapter(modeladapter); } else if (carBrand.equals("Toyota")) { Spinner model = (Spinner) findViewById(R.id.model); String[] models = new String[]{"Supra", "AE86", "GT86"}; ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models); model.setAdapter(modeladapter); } } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 

ご協力いただければ幸いです。

+0

に、このラインの変更thisでは、このリンクを参照してくださいhttp://stackoverflow.com/questions/34388547/cannot-infer-type-arguments-for-arrayadapter – ajantha

答えて

0
からあなたのコード

を変更

:TO

 @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id){ 
      String carBrand = parent.getSelectedItem().toString(); 
      if (carBrand.equals("Nissan")) { 
       final Spinner model = (Spinner) findViewById(R.id.model); 
       String[] models = new String[]{"S14", "R34", "240SX"}; 
       ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models); 
       model.setAdapter(modeladapter); 

      } else if (carBrand.equals("Honda")) { 
       Spinner model = (Spinner) findViewById(R.id.model); 
       String[] models = new String[]{"Civic", "NSX", "Accord"}; 
       ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models); 
       model.setAdapter(modeladapter); 
      } else if (carBrand.equals("Toyota")) { 
       Spinner model = (Spinner) findViewById(R.id.model); 
       String[] models = new String[]{"Supra", "AE86", "GT86"}; 
       ArrayAdapter<String> modeladapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, models); 
       model.setAdapter(modeladapter); 
      } 
     } 

 @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id){ 
      String carBrand = parent.getSelectedItem().toString(); 
      if (carBrand.equals("Nissan")) { 
       final Spinner model = (Spinner) findViewById(R.id.model); 
       String[] models = new String[]{"S14", "R34", "240SX"}; 
       ArrayAdapter<String> modeladapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_dropdown_item, models); 
       model.setAdapter(modeladapter); 

      } else if (carBrand.equals("Honda")) { 
       Spinner model = (Spinner) findViewById(R.id.model); 
       String[] models = new String[]{"Civic", "NSX", "Accord"}; 
       ArrayAdapter<String> modeladapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_dropdown_item, models); 
       model.setAdapter(modeladapter); 
      } else if (carBrand.equals("Toyota")) { 
       Spinner model = (Spinner) findViewById(R.id.model); 
       String[] models = new String[]{"Supra", "AE86", "GT86"}; 
       ArrayAdapter<String> modeladapter = new ArrayAdapter<>(YourActivity.this, android.R.layout.simple_spinner_dropdown_item, models); 
       model.setAdapter(modeladapter); 
      } 
     } 

YourActivityは、あなたのactivityクラス名です。 OnItemSelectedListenerの内部でがcontextと異なるタイプのリスナーのオブジェクトを表すため、ArrayAdapterコンストラクターを呼び出すには、オブジェクト参照がcontextまたはactivityである必要があるため、この変更が必要です。このソリューションはあなたのために働くはずです。

+0

これは、どうもありがとう、私の問題を解決しました私はそれを大いに感謝します! –

0

現在のアクティビティのコンテキストを渡す必要があります。 DetailsForm.this:

ArrayAdapter<String> modeladapter = new ArrayAdapter<>(DetailsForm.this, android.R.layout.simple_spinner_dropdown_item, models); 
関連する問題