0

So i've been trying to do that and it basically says that the "content" that i'm trying to get is null. This is the code that i'm testing (the commented part is the actual code that i'm trying to get it to work i just added the println to see if the data that was getting was right, which it wasn't)

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

*Also the thing that creates the tablerows for each is in the same class *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);
            }

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

1 Answers1

1

TextView#toString() doesn't get the text inside that TextView. It just prints the classname and instance hashcode. You need to use TextView#getText()#toString().

Use this:

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);
        }
    }
}
TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • It worked with one of my tables, but maybe you can help me, the other one is actually filled with LinearLayouts, and then those are added to a table Row, and those are added to the Table Layout, and when i try to run it with this same code it says "java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView" – Sarcastic Hawke Nov 01 '18 at 20:21
  • What do the LinearLayouts contain? – TheWanderer Nov 01 '18 at 20:22
  • They contain a TextView, check the edited question to kind of see how one of them works – Sarcastic Hawke Nov 01 '18 at 20:25
  • I don't understand how your code is structured. You really should consider using a RecyclerView combined with a SearchView https://stackoverflow.com/questions/30398247/how-to-filter-a-recyclerview-with-a-searchview – TheWanderer Nov 01 '18 at 20:30
  • After what i edited, i create a TableRow named tr, and just go tr.addView(layCustomer), and finally add it to the table layout by adding mTableLayout.addView(tr) – Sarcastic Hawke Nov 01 '18 at 20:36