我正在尝试使用Activity
删除Spinner
上按下的项目AlertDialog
。我还没有删除任何东西,我只是添加了Toast以确保它能够正常工作。
这是我的代码:
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.
我尝试在((ViewGroup)spinner.getParent()).removeView(spinner);
之前使用alertDialog.show();
,但它仍无效。
它说:android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
任何人都知道如何解决这个问题?
答案 0 :(得分: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;
}