浅谈对Android 中ListActivity与Activity的区别

2023-12-12 11:10

本文主要是介绍浅谈对Android 中ListActivity与Activity的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 (1)若一个类继承自ListActivity那么该类布局文件中搜涉及到的ListView的id必须

是 android:id="@android:id/list"

原因是:该list是Android内置的系统对象

(2)ListActivity不用写监听直接调用setListAdapter()方法

两个实列的对比:

ListActivity:

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"

android:orientation="vertical" >

<TextView

android:id="@+id/selection"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="" />

<ListView

android:id="@android:id/list"

android:layout_height="fill_parent"

android:layout_width="fill_parent"

android:drawSelectorOnTop="false"

>

</ListView>

</LinearLayout>

row.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"

android:orientation="horizontal" >

<ImageView

android:id="@+id/icon"

android:layout_width="50dp"

android:layout_height="50dp"

android:src="@drawable/ok"

></ImageView>

<TextView

android:id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:textSize="44sp" >

</TextView>

</LinearLayout>

package com.duchunxia;

import java.sql.Array;

import android.app.Activity;

import android.app.ListActivity;

import android.content.Context;

import android.database.DataSetObserver;

import android.graphics.Color;

import android.os.Bundle;

import android.text.Selection;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.TextView;

public class DongTaiListActivity extends ListActivity {

/** Called when the activity is first created. */

TextView selection;

String[] items = { "du", "chun", "xia", "ni", "hao", "wo", "ting", "hao" };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

setListAdapter(new IconicAdapter());

selection = (TextView) findViewById(R.id.selection);

}

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

// TODO Auto-generated method stub

// super.onListItemClick(items[position]);

selection.setText(items[position]);

}

class IconicAdapter extends ArrayAdapter {

IconicAdapter() {

super(DongTaiListActivity.this, R.layout.row, items);

// TODO Auto-generated constructor stub

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater in = getLayoutInflater();

// View row = in.inflate(R.layout.row, null, false);

View row = in.inflate(R.layout.row, null);

TextView label = (TextView) row.findViewById(R.id.label);

label.setText(items[position]);

ImageView icon = (ImageView) row.findViewById(R.id.icon);

if (items[position].length() > 3) {

icon.setImageResource(R.drawable.ic_launcher);

} else {

icon.setImageResource(R.drawable.ok);

}

if (position % 2 == 0) {

label.setBackgroundColor(Color.CYAN);

} else {

label.setBackgroundColor(Color.GREEN);

}

return (row);

}

}

}

显示结果:

 

 

Activity:

main.xml

<?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:textSize="25px"

android:gravity="center_horizontal"

android:text="魔乐科技(MLDN)视频列表" />

<ListView

android:id="@+id/datalist"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

</LinearLayout>

asdfg.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

</LinearLayout>

dataword.xml

<?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="wrap_content">

<ImageView

android:id="@+id/pic"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:padding="3px"/>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="190px"

android:layout_height="wrap_content"

android:gravity="left">

<TextView

android:id="@+id/title"

android:textSize="16px"

android:padding="3px"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<TextView

android:id="@+id/author"

android:padding="3px"

android:textSize="10px"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="200px"

android:layout_height="wrap_content"

android:gravity="left">

<TextView

android:id="@+id/type"

android:padding="3px"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<ImageView

android:id="@+id/score"

android:padding="3px"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

</LinearLayout>

package com.duchunxia;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.os.Bundle;

import android.widget.ListView;

import android.widget.SimpleAdapter;

public class MyListViewDemo extends Activity {

private int pic[] = new int[] { R.drawable.pic_oracle,

R.drawable.pic_javase, R.drawable.pic_javaweb,

R.drawable.pic_javaee, R.drawable.pic_android, R.drawable.pic_game };

private String data[][] = new String[][] { { "Oracle数据库", "魔乐科技" },

{ "Java SE基础课程", "李兴华" }, { "Java WEB综合开发", "MLDN" },

{ "Java EE高级开发", "李兴华" }, { "Android嵌入式开发", "李兴华" },

{ "Java 游戏开发", "李祺" } };

private ListView datalist = null; // 定义ListView组件

private List<Map<String, String>> list = new ArrayList<Map<String, String>>(); // 定义显示的内容包装

private SimpleAdapter simpleAdapter = null; // 进行数据的转换操作

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.main);

this.datalist = (ListView) super.findViewById(R.id.datalist); // 取得组件

for (int x = 0; x < this.data.length; x++) {

Map<String, String> map = new HashMap<String, String>(); // 定义Map集合,保存每一行数据

map.put("pic", String.valueOf(this.pic[x])); // 与data_list.xml中的TextView组加匹配

map.put("title", this.data[x][0]); // 与data_list.xml中的TextView组加匹配

map.put("author", this.data[x][1]); // 与data_list.xml中的TextView组加匹配

map.put("type", "免费");

map.put("score", String.valueOf(R.drawable.start_05));

this.list.add(map); // 保存了所有的数据行

}

this.simpleAdapter = new SimpleAdapter(this, this.list,

R.layout.datawrong, new String[] { "pic", "title", "author",

"type", "score" } // Map中的key的名称

, new int[] { R.id.pic, R.id.title, R.id.author, R.id.type,

R.id.score }); // 是data_list.xml中定义的组件的资源ID

this.datalist.setAdapter(this.simpleAdapter);

}

}


显示结果:


这篇关于浅谈对Android 中ListActivity与Activity的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/484403

相关文章

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

解读Pandas和Polars的区别及说明

《解读Pandas和Polars的区别及说明》Pandas和Polars是Python中用于数据处理的两个库,Pandas适用于中小规模数据的快速原型开发和复杂数据操作,而Polars则专注于高效数据... 目录Pandas vs Polars 对比表使用场景对比Pandas 的使用场景Polars 的使用

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

java中不同版本JSONObject区别小结

《java中不同版本JSONObject区别小结》本文主要介绍了java中不同版本JSONObject区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录1. FastjsON2. Jackson3. Gson4. org.json6. 总结在Jav

数据库使用之union、union all、各种join的用法区别解析

《数据库使用之union、unionall、各种join的用法区别解析》:本文主要介绍SQL中的Union和UnionAll的区别,包括去重与否以及使用时的注意事项,还详细解释了Join关键字,... 目录一、Union 和Union All1、区别:2、注意点:3、具体举例二、Join关键字的区别&php

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

2.1/5.1和7.1声道系统有什么区别? 音频声道的专业知识科普

《2.1/5.1和7.1声道系统有什么区别?音频声道的专业知识科普》当设置环绕声系统时,会遇到2.1、5.1、7.1、7.1.2、9.1等数字,当一遍又一遍地看到它们时,可能想知道它们是什... 想要把智能电视自带的音响升级成专业级的家庭影院系统吗?那么你将面临一个重要的选择——使用 2.1、5.1 还是