zoukankan      html  css  js  c++  java
  • AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表

    见归档项目ListActivityDemo.zip。

    基本步骤如下:

    1、创建一个TextView,用于指定每一个ListView的格式

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="20dp" 
        android:background="#0000ff">
    
    </TextView>

    2、创建主类

    package com.ljh.listactivitydemo;
    
    import android.app.ListActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.Toast;
    
    //(1)继承ListActivity
    public class MainActivity extends ListActivity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    
    		// (2)创建要显示的文本内容
    		String[] arr = { "java", "c/c++", "python", "ruby" };
    		// (3)创建ArrayAdapter,其中第二个参数resource:The resource ID for a layout file
    		// containing a TextView to use when instantiating views.是要以一个layout作为
    		// 参数,且此layout需要包含textview。
    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    				R.layout.list, arr);
    		// (4)为ListActivity设置adapter.
    		setListAdapter(adapter);
    	}
    	
    	//定义当某个选项被点击时的操作。
    	@Override
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		super.onListItemClick(l, v, position, id);
    		
    		Toast.makeText(this, position+" item is clicked.", Toast.LENGTH_LONG).show();
    	}
    
    }
    


  • 相关阅读:
    PAT 甲级 1126 Eulerian Path (25 分)
    PAT 甲级 1126 Eulerian Path (25 分)
    PAT 甲级 1125 Chain the Ropes (25 分)
    PAT 甲级 1125 Chain the Ropes (25 分)
    PAT 甲级 1124 Raffle for Weibo Followers (20 分)
    PAT 甲级 1124 Raffle for Weibo Followers (20 分)
    PAT 甲级 1131 Subway Map (30 分)
    PAT 甲级 1131 Subway Map (30 分)
    AcWing 906. 区间分组 区间贪心
    AcWing 907. 区间覆盖 区间贪心
  • 原文地址:https://www.cnblogs.com/eaglegeek/p/4557956.html
Copyright © 2011-2022 走看看