Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ListView Height after setadapter

well, I'm trying to get my listview height after I change it data, but it always return the prior height, not the actual.

So when I set the setadapter, and it get the old value. e.g:

 ActualHeight = 100
 Change data (filter) -> NewHeight = 60
 ListView.GetHeight still returns 100.

 Again

 ActualHeight = 60
 Change data (filter) -> NewHeight = 20
 ListView.GetHeight still returns 60.

The code i'm using is it:

                    int width, height = 0;

                    EditText edt_search;
                    ListView lv_marca;

                    List<Marca>  list_marca        = new ArrayList<Marca>();
                    List<Marca>  list_marca_search = new ArrayList<Marca>();

                    String text = edt_search.getText().toString(); 


                    list_marca_search.clear();

                    if(text.startsWith(".")){
                        text = text.replace(".", "");
                        for (Marca m : list_marca) {
                            if (String.valueOf(m.getCd_marca()).equals(text)){
                                list_marca_search.add(m);
                            }
                        }
                    } else {
                        for (Marca m : lista_marca) {
                            if (m.getDs_marca().contains(text)){
                                list_marca_search.add(m);
                            }
                        }
                    }

                    ArrayAdapter<Marca> adapter_marca = new CustomAdapter_Marca(MyDialog.this, R.layout.layout_consulta_estoque_marca_lista, list_marca_search);

                    lv_marca.setAdapter(adapter_marca);

                    int height_window  = getWindowManager().getDefaultDisplay().getHeight();
                    height             = lv_marca.getHeight() + getSupportActionBar().getHeight();

                    if (height >= height_window) {
                        height = (int) (height_window * 0.95);
                    }

                    getWindow().setLayout(width, height);
like image 898
Cechinel Avatar asked Dec 09 '22 21:12

Cechinel


1 Answers

Ok, I found the solution.

When I change the SetAdapter in my ListView I get the measure height that means one line, and multiply by the line numbers plus the divider height by the line numbers too.

Here is a example how I did, I don't know if is the better way but it works perfectly ^^:

    ListView lv_marca; 

    lv_marca.setAdapter(adapter_marca);

    int list_height = getListViewHeight(lv_marca);



    private int getListViewHeight(ListView list) {
          ListAdapter adapter = list.getAdapter();

          int listviewHeight = 0;

          list.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), 
                       MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

          listviewHeight = list.getMeasuredHeight() * adapter.getCount() + (adapter.getCount() * list.getDividerHeight());

          return listviewHeight;
    }
like image 154
Cechinel Avatar answered Jan 13 '23 02:01

Cechinel