Android Create List Programmatically
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)
"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.
精彩评论