を使用して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
誰もが問題を解決する方法を知っていますか?
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SpinnerActivity.this) ' –
@MDで' Activity context'を使用しましたが、今は完全にスピナーを削除しています。 –
私はあなたがそれらを削除しようとしていないため、正確にこのエラーが発生していますか? [自分のリストを作成するここを見てください](http://stackoverflow.com/questions/19820803/android-spinner-using-arrayliststring/19820912#19820912)、リストからアイテムを削除してアダプタを更新することができますビュー自体を削除しようとしないでください。 – codeMagic