Java Android以编程方式创建列表

Java Android以编程方式创建列表,java,android,Java,Android,我需要一个scrollview中有两个ListView,这在Android中显然是不可能的,但这正是客户需要的。因此,我试图通过编程方式扩大我的列表。我的表格单元格未显示在当前设置中。如果我调整xml文件中的设置,我可以在顶部显示一行,但不能显示其他行。我做错了什么 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV

我需要一个scrollview中有两个ListView,这在Android中显然是不可能的,但这正是客户需要的。因此,我试图通过编程方式扩大我的列表。我的表格单元格未显示在当前设置中。如果我调整xml文件中的设置,我可以在顶部显示一行,但不能显示其他行。我做错了什么

@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>
@覆盖
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.alertsview);
充气器=(LayoutFlater)this.getSystemService(Context.LAYOUT\u充气器\u服务);
topRows=newarraylist();
topLayout=(LinearLayout)findViewById(R.id.topListLayout);
bottomRows=新的ArrayList();
bottomLayout=(LinearLayout)findViewById(R.id.bottomListLayout);
addRecip=(按钮)findViewById(R.id.addRecipient);
addRecip.setOnClickListener(此);
如果(SugarLoafContext.currentSite.alertRecipients.size()>=5)
addRecip.setVisibility(View.GONE);
其他的
addRecip.setVisibility(View.VISIBLE);
订阅(配置AlertsNotification.class,此);
发布(新ViewAlertsNotification());
}
public void setTopList()
{
topLayout.removeAllViews();
topRows.clear();
List camList=新的ArrayList(SitesOrchestrator.getInstance().currentSite.cameraList);
对于(int i=0;i
(是的,正在调用setTopList()方法)

“我在这里做错了什么?” 您将ListView放在ScrollView中,但不要这样做。您应该在列表视图上观看Romain Guys google io会话。(在42:40,说明了滚动视图中的列表视图)

我不知道客户想要什么。但我怀疑他是否告诉过你将列表视图放在滚动视图中。很可能还有其他解决方案来归档客户想要的内容

列表视图将包含多少项?如果没有太多,您可以使用滚动视图中的两个线性布局


请更具体地说明布局应该是什么样子。

Yeh,你能解释一下客户想要什么吗?他们想看什么?它将显示一个文本视图,然后是一个项目列表,然后是另一个文本视图,然后是另一个项目列表,然后是一个按钮。项目列表需要完全展开,整个屏幕可以滚动。在任何情况下,我绕过Android的限制,计算每个listview中行的高度,将每个listview扩展为该高度,它们在scrollview中工作得很好。您是否设置了绝对高度,即px或dip?您确定所有设备上的文本视图高度相等吗?在不同的屏幕分辨率上会有不同数量的换行符,在不同的设备上可能会有不同高度的不同字体,等等。