Recommendations for variable length ListBox

by Peter Webb » Thu, 21 Oct 2010 13:59:15 GMT

Sponsored Links
 All of the examples of the use of ListView and similar controls that I
can find assume a fixed number of field names coded into @strings or
similar, and retrieved through R.layout functions.

I want to dynamically create ListViews, Linear Layouts, Spinners etc
of variable length, and contents determined at run time.

I assume this is possible. Various appraoches occur to me:

Extend ListView with a custom creator that pre-populates with the
runtime determined field names. 
gives a hint on how to do it, whilst not directly addressing the

I could try and take out the requirement to go through R.layout, and
directly reference some other data structure

And some others.

However, this must be a very common design pattern, can anyone tell me
the recommended approach for allocating the number of items in (say) a
List View and their content at runtime ?


Peter Webb


Recommendations for variable length ListBox

by Kumar Bibek » Thu, 21 Oct 2010 16:08:12 GMT

 Sure you can do that. There can be as many number of ViewTypes you
want. And as many layouts. Also, you can create the views at runtime,
depending on the requirement.

There are a few methods in the BaseAdapter class which will help you.
Search for them (getItemViewType or something)

Sponsored Links

Other Threads

1. Tabs: Error while using TabHost


I am trying to write a simple application that creates a new tab. The
view structure is:

   -- TabHost

The code is shown below:

public class P4Main extends Activity {

    public void onCreate(Bundle savedInstanceState) {

    public void onStart() {

        //We can try to add tabs and related views programmatically here

    private void createAdditionalViews()
        TextView tv = new TextView(this);
        tv.setText("Test view");

        TabHost thRoot = new TabHost(this);

        TabWidget tabs = new TabWidget(this);

        FrameLayout tabcontent = new FrameLayout(this);


        TabHost.TabSpec tabSetup = thRoot.newTabSpec("Setup");
        TextView tv1 = new TextView(this);
        tv1.setText("This is the Setup tag.");


        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams
        this.addContentView(thRoot, lp);

When I run it, it crashes at the line:

The debug call stack is as below:

line: 2266
line: 2284
ActivityThread.access$1800(ActivityThread, ActivityThread
$ActivityRecord) line: 112
ActivityThread$H.handleMessage(Message) line: 1692
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3948
Method.invokeNative(Object, Object[], Class, Class[], Class, int,
boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$ line: 782
ZygoteInit.main(String[]) line: 540
NativeStart.main(String[]) line: not available [native method]

What am I missing/doing wrong?



2. External Jars


I'm using Eclipse IDE, and my Android project has four external jar
libraries. For each time I build my project the jars file recompiling
too. Even my eclipse IDE hang sometime and get and "Out of memory".

Is there any way to avoid recompiling the external jar file, I'm sure
that I have done something wrong. Please, could you help me.

Thanks in advance.



3. internet doesn't work during call

4. scale image around arbitrary center: iPhone style zoom?

5. Translating an ImageView from x to y

6. Rebuild External Jars

7. Dialog Update Issues