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. 
 http://developer.android.com/resources/tutorials/views/hello-listview.html 
gives a hint on how to do it, whilst not directly addressing the
question.

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 ?

Thanks


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

Hi,

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

FrameLayout
   -- TabHost
   -------TabWidget
   -------FrameLayout

The code is shown below:

public class P4Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onStart() {
        super.onStart();

        //We can try to add tabs and related views programmatically here
        createAdditionalViews();
    }

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

        TabHost thRoot = new TabHost(this);

        TabWidget tabs = new TabWidget(this);
        tabs.setId(android.R.id.tabs);
        thRoot.addView(tabs);

        FrameLayout tabcontent = new FrameLayout(this);
        tabcontent.setId(android.R.id.tabcontent);
        thRoot.addView(tabcontent);

        thRoot.setup();

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

        thRoot.setCurrentTabByTag("Setup");

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

When I run it, it crashes at the line:
        tabSetup.setContent(tv1.getId());

The debug call stack is as below:

ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord)
line: 2266
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord)
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$MethodAndArgsCaller.run() line: 782
ZygoteInit.main(String[]) line: 540
NativeStart.main(String[]) line: not available [native method]

What am I missing/doing wrong?

Thanks,
AS

--~--~---------~--~----~------------~-------~--~----~

2. External Jars

Hi,

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.

Frank.

--~--~---------~--~----~------------~-------~--~----~

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