[英]How to compare the content of a TableRow (of a code-generated) Table with a String?
所以我一直在尝试这样做,它基本上说我正在尝试获取的“内容”为空。 这是我正在测试的代码(注释部分是我试图使其正常工作的实际代码,我只是添加了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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.