浅谈对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

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Go之errors.New和fmt.Errorf 的区别小结

《Go之errors.New和fmt.Errorf的区别小结》本文主要介绍了Go之errors.New和fmt.Errorf的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考... 目录error的基本用法1. 获取错误信息2. 在条件判断中使用基本区别1.函数签名2.使用场景详细对

Redis中哨兵机制和集群的区别及说明

《Redis中哨兵机制和集群的区别及说明》Redis哨兵通过主从复制实现高可用,适用于中小规模数据;集群采用分布式分片,支持动态扩展,适合大规模数据,哨兵管理简单但扩展性弱,集群性能更强但架构复杂,根... 目录一、架构设计与节点角色1. 哨兵机制(Sentinel)2. 集群(Cluster)二、数据分片

一文带你迅速搞懂路由器/交换机/光猫三者概念区别

《一文带你迅速搞懂路由器/交换机/光猫三者概念区别》讨论网络设备时,常提及路由器、交换机及光猫等词汇,日常生活、工作中,这些设备至关重要,居家上网、企业内部沟通乃至互联网冲浪皆无法脱离其影响力,本文将... 当谈论网络设备时,我们常常会听到路由器、交换机和光猫这几个名词。它们是构建现代网络基础设施的关键组成

redis和redission分布式锁原理及区别说明

《redis和redission分布式锁原理及区别说明》文章对比了synchronized、乐观锁、Redis分布式锁及Redission锁的原理与区别,指出在集群环境下synchronized失效,... 目录Redis和redission分布式锁原理及区别1、有的同伴想到了synchronized关键字

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

浅谈MySQL的容量规划

《浅谈MySQL的容量规划》进行MySQL的容量规划是确保数据库能够在当前和未来的负载下顺利运行的重要步骤,容量规划包括评估当前资源使用情况、预测未来增长、调整配置和硬件资源等,感兴趣的可以了解一下... 目录一、评估当前资源使用情况1.1 磁盘空间使用1.2 内存使用1.3 CPU使用1.4 网络带宽二、

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

JAVA覆盖和重写的区别及说明

《JAVA覆盖和重写的区别及说明》非静态方法的覆盖即重写,具有多态性;静态方法无法被覆盖,但可被重写(仅通过类名调用),二者区别在于绑定时机与引用类型关联性... 目录Java覆盖和重写的区别经常听到两种话认真读完上面两份代码JAVA覆盖和重写的区别经常听到两种话1.覆盖=重写。2.静态方法可andro