I want the right side of the activity to show different items depending which item of the listview on the left is selected. I was able to follow the Android ListView example where the listview takes up the whole activity screen, but was unable to expand that to split screen. Here is my xml with the list view on the left and a simple button on the right for now. I'm having trouble figuring out the code that will populate my listview.
system_status.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity开发者_运维知识库_system_status"
android:title="@string/system_status"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
<ListView
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
<ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content"></ToggleButton>
</LinearLayout>
</LinearLayout>
UPDATE: I figured it out with the help of Scythe's post. Here is the code that got it working. The xml above works as is.
public class SystemStatusActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView lv = (ListView) findViewById(R.id.my_list);
String[] stats = getResources().getStringArray(R.array.array_system_status);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, stats));
}
}
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
You have to set up and adapter for that ListView. Search for tutorials on creating custom Adapter classes, you can find dozens of those. You absolutely must understand custom adapters if you plan to develop for this platform.
But anyways, you can get away with the standard ArrayAdapter to get this to work:
String[] countries = <put your strings here>
yourList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries));
After this, set up an ItemClickListener for the list, and handle the content change on the right pane as needed.
Try something like
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
The above block was new
<LinearLayout
android:layout_width="0dp" <--BIG CHANGE HERE
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp" Big change here
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1">
<ToggleButton android:text="ToggleButton" android:id="@+id/toggleButton1" android:layout_width="wrap_content" android:layout_height="wrap_content"></ToggleButton>
</LinearLayout>
</Linearlayout> NEW
The big changes here were to change the layout_width for the linearlayouts you had, so they evenly share the space, and I also put them both in a horizontal linear layout.
From http://www.anddev.org/view-layout-resource-problems-f27/two-listviews-side-by-side-t11199.html
Pretty much just need to wrap what you have in a LinearLayout
with android:orientation="horizonal"
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<ListView android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:entries="@array/colors"
/>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Hello World, AndroidTest"
/>
</LinearLayout>
</LinearLayout>
精彩评论