Why are dynamically added widgets not displaying?

My code RUNS (see source below), but the only widgets that are displaying are those from xml layout file. The dynamically created widgets, which code now runs without a hicc[ough,up], don't show themselves. Does anybody know why that would be? BTW, there are Contacts (4), and stepping through the code the values returned are valid.

The only thing in the vals that is strange is that two of the four Contact names has a "\t" between the first and last name, a la "Barney\t Rubble" (I doubt this is a problem, but wonder why two would have it, and two wouldn't, when I entered the Contact names in exactly the same manner...)

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

    ScrollView svh = (ScrollView) findViewById(R.id.scrollViewHost);

    // Create a Linear Layout for each contact?
    LinearLayout llay = new LinearLayout(this);
    llay.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams llp = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    llp.weight = 1.0f;

    svh.addView(llay);

    // Contacts data snippet adapted from
    // http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur
                    .getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // The previous LinearLayout was added to the ScrollView; these
            // in the loop will be added to that one
            LinearLayout llayDynamic = new LinearLayout(this);
            llayDynamic.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams llpDynamic = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            llpDynamic.weight = 1.0f;

            CheckBox cbOnDemand = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbOnDemand.setLayoutParams(llp);
            llayDynamic.addView(cbOnDemand);

            CheckBox cbTime = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbTime.setLayoutParams(llp);
            llayDynamic.addView(cbTime);

            CheckBox cbSpace = new CheckBox(getApplicationContext());
            cbOnDemand.setTag(id);
            cbSpace.setLayoutParams(llp);
            llayDynamic.addView(cbSpace);

            TextView tv = new TextView(getApplicationContext());
            tv.setTag(id);
            tv.setText(name);
            tv.setLayoutParams(llp);
            llayDynamic.addView(tv);

            llayDynamic.invalidate();

        }
        // One cat on stackOverflow said to do this, another said it
        // would be unnecessary
        svh.invalidate();
    }
}

Update: OK, I fixed the problem with the widgets not displaying by adding:

            llay.addView(llayDynamic);

...before the possibly unnecessary call to llayDynamic.invalidate()

HOWEVER, the widgets as displayed are still a mess. See http://warbler.posterous.com/widgets-are-still-deformedimitating-brain-dea to see what I mean.

I reckon I can get things to space better by experimenting with padding properties and such, but the first child LinearLayout appearing to the right (on the same row) with the parent LinearLayout (child of the ScrollView) is a problem.

Why would it do this, and how can I solve it?

Upvotes: 2

Views: 170

Answers (1)

wsanville
wsanville

Reputation: 37516

You never add anything to the LinearLayout called llay, which is inside your ScrollView.

Try adding this inside your while loop:

llay.addView(llayDynamic);

Upvotes: 3

Related Questions