Android常用控件:ListView

ListView的简易实现

效果图:

Android常用控件:ListView_第1张图片

布局文件:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.csdn.MainActivity" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

JAVA文件:MainActivity.java

package com.example.csdn;

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

public class MainActivity extends Activity {

	private ListView listview;
	private String[] listItem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findBYId();
		setListView();
	}

	private void findBYId() {
		listview = (ListView) findViewById(R.id.listview);
	}

	private void setListView() {
		listItem = new String[] { "1", "1", "1", "1", "1", "1", "1", "1", "1",
				"1", "1", "1", "1" };

		listview.setAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, listItem));
	}
}



你可能感兴趣的:(Android常用控件:ListView)