How to remove all LinearLayouts from LinearLayout?
I create many LinearLayouts programmatically and put them in another LinearLayout:
public void loadList(){
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearShowing);
if(linearLayout.getChildCount() > 0)
linearLayout.removeAllViews(); // THIS LINE DOESN'T WORK FOR LINEARLAYOUTS INSIDE LINEARLAYOUTS
List<Mineral> showMineralsList = dbHandler.getAllMinerais("external");
for (Mineral mineral : showMineralsList){
id = mineral.getID();
//testIds.add(id);
int favorite = mineral.getFavorite();
String nome = mineral.getName(),
formulaQuimica = mineral.getFormulaQuimica();
LinearLayout linearVertical = new LinearLayout(this);
linearVertical.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams lvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearVertical.setLayoutParams(lvParams);
LinearLayout linearHorizontalOuter = new LinearLayout(this);
linearHorizontalOuter.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lhoParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lhoParams.gravity = Gravity.CENTER_VERTICAL;
linearHorizontalOuter.setLayoutParams(lhoParams);
LinearLayout linearHorizontalInner = new LinearLayout(this);
linearHorizontalInner.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams lhiParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
lhiParams.gravity = Gravity.CENTER_VERTICAL;
linearHorizontalInner.setLayoutParams(lhiParams);
ImageButton ibStar = new ImageButton(this);
LinearLayout.LayoutParams ibsParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ibsParams.setMargins(0, 0, vinteMargin, 0);
ibStar.setLayoutParams(ibsParams);
if(favorite == 0)
ibStar.setBackgroundResource(R.drawable.ic_star_gray_24dp);
else
ibStar.setBackgroundResource(R.drawable.ic_star_blue_24dp);
vId = generateViewId();
vIds.put(vId, id);
linearHorizontalInner.setId(vId);
linearHorizontalInner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
i = new Intent(Main.this, Detalhes.class);
i.putExtra("id", vIds.get(view.getId()));
startActivity(i);
}
});
vId = generateViewId();
vIds.put(vId, id);
ibStar.setId(vId);
ibStar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setFavorite(vIds.get(view.getId()));
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
TextView tvName = new TextView(this);
LinearLayout.LayoutParams tvnParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
tvName.setText(nome);
tvName.setLayoutParams(tvnParams);
tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
TextView tvFormula = new TextView(this);
LinearLayout.LayoutParams tvfParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
tvFormula.setText(formulaQuimica);
tvFormula.setLayoutParams(tvfParams);
tvFormula.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
TableLayout.LayoutParams lpDiv = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 5f);
lpDiv.setMargins(0, dezMargin, 0, dezMargin);
ImageView ivDiv = new ImageView(this);
ivDiv.setBackgroundResource(R.drawable.divider);
ivDiv.setLayoutParams(lpDiv);
linearHorizontalInner.addView(tvName);
linearHorizontalInner.addView(tvFormula);
linearHorizontalOuter.addView(ibStar);
linearHorizontalOuter.addView(linearHorizontalInner);
linearVertical.addView(ivDiv);
linearVertical.addView(linearHorizontalOuter);
linearLayout.addView(linearVertical);
}
}
Sometimes in my work I have to reload this list ... So I tried to remove all views from "linearLayout" (my main layout set in my XML layout file) using linearLayout.removeAllViews (); but it throws me:
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
+3
source to share
2 answers
I solved it by following these steps:
- Setting an id for my "linearVertical" (which corresponds to the first child of my main parent LinearLayout);
- Adding this id to the list every time a new linearVertical is created;
- Iterate through this list and set the visibility of all views that have their ID in this list to GONE.
code:
private ArrayList<Integer> layoutsIds = new ArrayList<>();
public void loadList(){
resetViews();
(...)
(...)
layoutId = generateViewId();
linearVertical.setId(layoutId);
layoutsIds.add(layoutId);
(...)
(...)
}
public void resetViews(){
for(int a=0; a<layoutIds.size(); a++){
LinearLayout toRemove = (LinearLayout)findViewById(layoutIds.get(a));
toRemove.setVisibility(View.GONE);
}
}
Of course, this could be improved, but basically this is my solution (:
0
source to share