`
java-mans
  • 浏览: 11414126 次
文章分类
社区版块
存档分类
最新评论

Android开发—数据库应用—添加列表活动(ListActivity)--分析记事本程序

 
阅读更多
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:添加列表活动--分析记事本程序

* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月 08 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:

* 程序头部的注释结束

*/

①创建一个名为“DummyNote”的新项目,DummyNote.java程序如下:

<span style="font-size: 24px;">package com.demo.android.dummynote;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class DummyNote extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setAdapter();
    }
    private String[] note_array = {
    		"gasolin",
    		"crota",
    		"louk",
    		"magicion"
    };
    private void setAdapter(){
    	ListAdapter adapter = new ArrayAdapter<String>(this,
    			android.R.layout.simple_list_item_1,note_array);
    	setListAdapter(adapter);
    }
}
</span>

运行结果:

② 自定义ListView组件

可以通过改写“res/layout/main.xml”文件,来自定义ListView组件,程序如下“

main.xml

<span style="font-size: 24px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView  
	android:id = "@+id/android:list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
</LinearLayout>
</span>


在“DummyNote.java”文件的“onCreate”方法中添加 setContentView(R.layout.main)这条语句,这样“DummyNote”应用程序就会使用“res/layout/main.xml”文件作为主要的XML界面说明文件。

DummyNote.java

package com.demo.android.dummynote;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class DummyNote extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setAdapter();
    }
    private String[] note_array = {
    		"gasolin",
    		"crota",
    		"louk",
    		"magicion"
    };
    private void setAdapter(){
    	ListAdapter adapter = new ArrayAdapter<String>(this,
    			android.R.layout.simple_list_item_1,note_array);
    	setListAdapter(adapter);
    }
}

③自定义空列表显示内容:

方法:通过改写“res/layout/main.xml”文件,来添加当列表是空的时候所显示的内容。

程序如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView  
	android:id = "@+id/android:list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
<TextView  
	android:id = "@+id/empty"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical"
    android:text="No Notes"
    />
</LinearLayout>



同时“DummyNote.java”文件修改如下:

package com.demo.android.dummynote;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;

public class DummyNote extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Tell the list view which view to display when the list is empty
        getListView().setEmptyView(findViewById(R.id.empty));
        setAdapter();
    }
    private String[] note_array = {
    	/*	"gasolin",
    		"crota",
    		"louk",
    		"magicion"*/
    };
    private void setAdapter(){
    	ListAdapter adapter = new ArrayAdapter<String>(this,
    			android.R.layout.simple_list_item_1,note_array);
    	setListAdapter(adapter);
    }
}

 


运行结果:

解释:可以使用“getListView”的“setEmptyView”方法,来指定列表内容为空的时候的显示内容。当“ListView”界面组件(list)中没有资料时(empty),就会显示出以“empty”为识别代号的界面内容。如上图所示。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics