gpt4 book ai didi

java - 如何从 LinearLayout 中删除所有 LinearLayouts?

转载 作者:太空狗 更新时间:2023-10-29 14:43:14 29 4
gpt4 key购买 nike

我以编程方式创建了很多 LinearLayout 并将它们放在另一个 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);
}
}

有时,在我的 Activity 中,我必须重新加载该列表...所以我试图使用 linearLayout.removeAllViews(); 从“linearLayout”(我的 XML 布局文件中设置的主要布局)中删除所有 View ,但是它让我:

java.lang.IllegalArgumentException:从给定 View 中找不到合适的父对象。请提供有效 View 。

最佳答案

试试这段代码

if(linearLayout.getChildCount() > 0)
linearLayout.removeAllViews();

关于java - 如何从 LinearLayout 中删除所有 LinearLayouts?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44489289/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com