2015-03-26 5 views
0

В следующем коде я добавляю TextView в alertDialog и удаляю их (управляемый двумя кнопками). Добавление прекрасных работ, но удаление удаляет все ранее добавленные TextView. Почему это происходит, а не только последний TextView удален?removeView приводит к исчезновению всех видов.

final int DIALOG_ADD = 1; 
final int DIALOG_REMOVE = 2; 

LinearLayout view; 
ArrayList<TextView> textViews; 

@Override 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder adb = new AlertDialog.Builder(this); 
    adb.setTitle("Custom dialog"); 

    view = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog, null); 

    adb.setView(view); 
    return adb.create(); 

} 

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 

    super.onPrepareDialog(id, dialog); 

    switch (id) { 

    case DIALOG_ADD: 
     TextView tv = new TextView(this); 
     tv.setText("TextView " + textViews.size()); 
     view.addView(tv,new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
     textViews.add(tv); 

     break; 

    case DIALOG_REMOVE: 

     TextView tv1 = textViews.get(textViews.size() - 1); 
     view.removeView(tv1); 
     textViews.remove(tv1); 

     break; 

    }; 

    TextView tvCount = (TextView)dialog.getWindow().findViewById(R.id.tvCount); 
    tvCount.setText("Text view count: " + textViews.size()); 

} 

ответ

0

Попробуйте так:

view.removeViewAt(textViews.size() - 1); 

вместо

view.removeView(tv1); 
Смежные вопросы