gpt4 book ai didi

java - 如何将(代码生成的)TableRow 的内容与字符串进行比较?

转载 作者:行者123 更新时间:2023-12-02 10:38:21 25 4
gpt4 key购买 nike

所以我一直在尝试这样做,它基本上表明我试图获取的“内容”为空。这是我正在测试的代码(注释部分是我试图让它工作的实际代码,我刚刚添加了 println 以查看获取的数据是否正确,但事实并非如此)

public void FiltarBusqueda(String filtro) {
int count=0;
for (int r = 0; r < mTableLayout.getChildCount(); r++) {
TableRow trow = (TableRow) mTableLayout.getChildAt(r);
for(int c=0;c <= trow.getChildCount();c++){
System.out.println(""+trow.getChildAt(c));
/*if (trow.getChildAt(c).toString() != filtro) {
count++; }
if(count==3){
mTableLayout.removeView(trow); }*/
}
}
}

public void onClickFiltro(View v){
EditText filtro = (EditText)findViewById(R.id.txtproducto);
FiltarBusqueda(filtro.getText().toString());
}

*为每个创建表行的东西也属于同一个类*LinearLayout 的东西:

final LinearLayout layCustomer = new LinearLayout(this);
layCustomer.setOrientation(LinearLayout.VERTICAL);
layCustomer.setPadding(0, 10, 0, 10);
layCustomer.setBackgroundColor(Color.parseColor("#f8f8f8"));

final TextView tv3 = new TextView(this);
if (i == -1) {
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv3.setPadding(5, 5, 0, 5);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
} else {
tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv3.setPadding(5, 0, 0, 5);
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
}

tv3.setGravity(Gravity.TOP);


if (i == -1) {
tv3.setText("Productos");
tv3.setBackgroundColor(Color.parseColor("#f0f0f0"));
} else {
tv3.setBackgroundColor(Color.parseColor("#f8f8f8"));
tv3.setTextColor(Color.parseColor("#000000"));
tv3.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallTextSize);
tv3.setText(row.productName);
}
layCustomer.addView(tv3);

最佳答案

TextView#toString() 无法获取该 TextView 内的文本。它只是打印类名和实例哈希码。您需要使用TextView#getText()#toString()

使用这个:

public void FiltarBusqueda(String filtro) {
for (int r = 0; r < mTableLayout.getChildCount(); r++) {
TableRow trow = (TableRow) mTableLayout.getChildAt(r);

boolean hasMatch = false;

for (int c = 0; c <= trow.getChildCount(); c++) {
String text = ((TextView) trow.getChildAt(c)).getText().toString();

hasMatch = text.equals(filtro); //when comparing Strings, use `equals()` not `==`
if (hasMatch) break;
}

if (!hasMatch) {
mTableLayout.removeRow(trow);
}
}
}

关于java - 如何将(代码生成的)TableRow 的内容与字符串进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53108216/

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