Aug 25, 2011

A simple ListView using android.R.layout.simple_list_item_1 layout

It's a very simple and standard example of ListView, using the built-in layout android.R.layout.simple_list_item_1 layout.

A simple ListView using android.R.layout.simple_list_item_1 layout

package com.AndroidListView;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AndroidListViewActivity extends Activity {

ListView myList;

String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView)findViewById(R.id.list);

ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listContent);
myList.setAdapter(adapter);

}
}


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


But...what's android.R.layout.simple_list_item_1. You can check it in <android-sdk>/platforms/<android-version>/data/res/layout/simple_list_item_1.xml. Such that you can copy and modify it for your own.





For example, the content of android-sdk-linux_x86/platforms/android-7/data/res/layout/layout/simple_list_item_1.xml is:
<?xml version="1.0" encoding="utf-8"?>

<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>






Related:

- Load ListView contents from XML resources

- ListView with multiple choice

- Simple GridView example




No comments:

Post a Comment

Infolinks In Text Ads