Search code examples
javaandroidandroid-arrayadapter

How to apply ArrayAdapter parameters as programatically made layout?


For ArrayAdapter,

ArrayAdapter <String> arrayAdapter = new ArrayAdapter(Context , resId , viewId , String[]) 

We need to have resId and viewId but how can i use Custom programmatically made Android Views ?? Is there any way so that i can apply programatically made layouts in this ArrayAdapter ??


Solution

  • I did not quite get the question, hope this helps:

    How to get the view id

    If you programatically created a view and want to get its id, you need to call this:

    view.setId(View.generateViewId());
    int id = view.getId();
    

    Same goes for Layouts (classes that extend Viewgroup), because they are views (ViewGroup extends View).

    Code example:

    layout.setId(View.generateViewId());
    view.setId(View.generateViewId());
    ArrayAdapter<?> adapter = new ArrayAdapter(this, layout.getId(), view.getId());
    

    How to apply an ArrayAdapter

    Every object that extends AbsListView has a setAdapter()-Method, in that you can pass an ArrayAdapter.