3

I need two listviews in a scrollview, this obviously isn't possible with Android, but this is what the customer needs. So I am trying to inflate my list programmatically. My table cells aren't showing up with my current setup. If I adjust settings in the xml file I can get one row in the top to show up, but not the other ones. What am I doing wrong here?

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

    inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    topRows = new ArrayList<View>();
    topLayout = (LinearLayout)findViewById(R.id.topListLayout);
    bottomRows = new ArrayList<View>();
    bottomLayout = (LinearLayout)findViewById(R.id.bottomListLayout);

    addRecip = (Button) findViewById(R.id.addRecipient);
    addRecip.setOnClickListener(this);

    if (SugarLoafContext.currentSite.alertRecipients.size() >= 5)
        addRecip.setVisibility(View.GONE);
    else
        addRecip.setVisibility(View.VISIBLE);


    EventBus.subscribe(ConfigureAlertsNotification.class, this);
    EventBus.publish(new ViewAlertsNotification());
}


public void setTopList()
{
    topLayout.removeAllViews();
    topRows.clear();
    List<Camera> camList = new ArrayList<Camera>(SitesOrchestrator.getInstance().currentSite.cameraList);

    for (int i = 0; i < camList.size(); i++)
    {
        String index = Integer.toString(i);
        Camera cam = camList.get(i);
        View row = inflater.inflate(R.layout.cameraalertrow, null);
        TextView name = (TextView)row.findViewById(R.id.cameraNameAlerts);
        CheckBox chk = (CheckBox)row.findViewById(R.id.camAlertChk);
        name.setText(cam.name);
        name.setTextColor(Color.GRAY);
        chk.setOnCheckedChangeListener(this);
        chk.setTag(index);

        if (cam.isOnline)
        {
            chk.setEnabled(true);

            if (cam.alertsEnabled && !chk.isChecked())
                chk.setChecked(true);
            else if (cam.alertsEnabled == false && chk.isChecked())
                chk.setChecked(false);
        }
        else
        { 
            chk.setChecked(cam.alertsEnabled);
            chk.setEnabled(false);
            name.setTextColor(Color.DKGRAY);
        }

        topRows.add(row);
        topLayout.addView(row);
    }

}

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:fillViewport="true"
android:id="@+id/alertsTopScroll">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:id="@+id/alertsTopText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:text="@string/alerts_top_title"
android:gravity="center"
android:textColor="#000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</TextView>
<LinearLayout
android:id="@+id/topListLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</LinearLayout>
<TextView
android:id="@+id/alertsBottomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#CCCCCC"
android:text="@string/alerts_bottom_title"
android:gravity="center"
android:textColor="#000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</TextView>
<LinearLayout
android:id="@+id/bottomListLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</LinearLayout>

<Button
android:id="@+id/addRecipient"
android:layout_height="50dp"
android:layout_width="fill_parent"
android:layout_centerHorizontal="true"
android:text="@string/addRecipient"/>
</LinearLayout>
</ScrollView>

(And yes the method setTopList() is being called)

1 Answer 1

1

"What am I doing wrong here?" You put ListViews in a ScrollView - just don't do that. You should watch Romain Guys google io session on list views world of listview. (At 42:40 list views in scroll views is explained)

I don't know what the customer wants. But I doubt he has told you to put ListViews in ScrollViews. There is most probably an other solution to archive what the customer wants.

How many items will the list views contain? If there are not too many you can use i.e. two LinearLayouts in a ScrollView.

Please be more specific about how the layout should look like.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeh, can you explain what the client wants? What do they want to see?
It will show a textview, then a list of items, then another textview, then another list of items, then a button. The list of items need to be fully expanded and the entire screen scrollable. In any case I went around Android's limitations and calculated the height of the rows in each listview, expanded each listview to be that height, and they work great in a scrollview.
Did you set the height absolut i.e. in px or dip? Are you sure the textviews will have equeal height on all devices? On different screen resolutions there will be different numbers of line breaks, on different devices may be different fonts with different heights, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.