2016-04-12 3 views
1

を使用してSpinnerで押されたアイテムを削除するActivityを作成しようとしています。私はまだ何も削除していない、私はちょうどそれが動作することを確認するトーストを追加しました。AlterDialogを使用してスピナーからアイテムを削除する方法

これは私のコードです:私は、コードを実行すると

public class SpinnerActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_spinner); 

    final Spinner spinner = (Spinner) findViewById(R.id.spinner); 
    final Context context = getApplicationContext(); 

    // Create an ArrayAdapter using the string array and a default spinner layout 
    final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.planets_array, android.R.layout.simple_spinner_item); 

    // Specify the layout to use when the list of choices appears 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    // Apply the adapter to the spinner 
    spinner.setAdapter(adapter); 

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     int selectionCurrent = spinner.getSelectedItemPosition(); 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      if (selectionCurrent != position) { 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

       // set title 
       alertDialogBuilder.setTitle(R.string.dialogtitle); 

       //set dialog message 
       alertDialogBuilder.setMessage(R.string.dialogtext).setCancelable(false) 
         .setPositiveButton(R.string.si,new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog,int id) { 
           // if this button is clicked, 
           Toast.makeText(context, "Eliminar", Toast.LENGTH_SHORT).show(); 
          } 
         }) .setNegativeButton(R.string.no 
         , new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // if this button is clicked, do nothing 
         dialog.cancel(); 
        } 
       }); 
       alertDialogBuilder.setView(spinner); 
       AlertDialog alertDialog = alertDialogBuilder.create(); 
       // show it 
       alertDialog.show(); 
      } 
      selectionCurrent = position; 
     } 

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

     } 
    }); 

} 

、次のエラーが表示されますjava.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

私はalertDialog.show();((ViewGroup)spinner.getParent()).removeView(spinner);を使用しようとしたが、それはまだ動作していません。

それは言う:android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

誰もが問題を解決する方法を知っていますか?

+0

AlertDialog.Builder alertDialogBu​​ilder = new AlertDialog.Builder(SpinnerActivity.this) ' –

+0

@MDで' Activity context'を使用しましたが、今は完全にスピナーを削除しています。 –

+0

私はあなたがそれらを削除しようとしていないため、正確にこのエラーが発生していますか? [自分のリストを作成するここを見てください](http://stackoverflow.com/questions/19820803/android-spinner-using-arrayliststring/19820912#19820912)、リストからアイテムを削除してアダプタを更新することができますビュー自体を削除しようとしないでください。 – codeMagic

答えて

1

まずArrayAdapterを膨らませるための変数を作成します。

String[] mTestArray; 

は、リソースからデータを取得します。

mTestArray = getResources().getStringArray(R.array.planets_array); 

は、この配列をArrayAdapterを膨らま:

final ArrayList<String> list =new ArrayList<String>(Arrays.asList(mTestArray)); 
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, list); 

そして最後に削除をあなたのダイアログに:

final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
     mTestArray , android.R.layout.simple_spinner_item); 

     String item = list.get(position); 


@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    if (selectionCurrent != position) { 
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 

     // set title 
     alertDialogBuilder.setTitle(R.string.dialogtitle); 

     //set dialog message 
     alertDialogBuilder.setMessage(R.string.dialogtext).setCancelable(false) 
       .setPositiveButton(R.string.si,new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         list.remove(position); 
         adapter.notifyDataSetChanged(); 
         // if this button is clicked, 
         Toast.makeText(context, "Eliminar", Toast.LENGTH_SHORT).show(); 
        } 
       }) .setNegativeButton(R.string.no 
       , new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // if this button is clicked, do nothing 
       dialog.cancel(); 
      } 
     }); 
     alertDialogBuilder.setView(spinner); 
     AlertDialog alertDialog = alertDialogBuilder.create(); 
     // show it 
     alertDialog.show(); 
    } 
    selectionCurrent = position; 
}   
+0

intをexpateし、mTestArrayがString []であるため、配列adaperを膨らませることはできません。 –

+0

私の失敗アダプタを作成している方法を確認しませんでした:P、変更を確認して、作業中 –

+0

要素がクリックされたとき(ダイアログがポップアップするとき)エラーを表示し続けます。 (ViewGroup)spinner.getParent())を使ってビューを削除すると、removeView(spinner);それは、私が説明で言ったようにtoenはnullだと言います。 –

関連する問題