为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

梦里花落0921 2021-12-22 20:42:21
所以我一直在尝试这样做,它基本上说我试图获得的“内容”为空。这是我正在测试的代码(注释部分是我试图让它工作的实际代码,我只是添加了 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());}*此外,为每个创建 tablerows 的东西在同一个类中 *LinearLayout Thing: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);            }
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

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);

        }

    }

}


查看完整回答
反对 回复 2021-12-22
  • 1 回答
  • 0 关注
  • 226 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号