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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Before和BeforeClass的区别及说明

《Before和BeforeClass的区别及说明》:本文主要介绍Before和BeforeClass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Before和BeforeClass的区别一个简单的例子当运行这个测试类时总结Before和Befor

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Linux中的more 和 less区别对比分析

《Linux中的more和less区别对比分析》在Linux/Unix系统中,more和less都是用于分页查看文本文件的命令,但less是more的增强版,功能更强大,:本文主要介绍Linu... 目录1. 基础功能对比2. 常用操作对比less 的操作3. 实际使用示例4. 为什么推荐 less?5.

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

解读@ConfigurationProperties和@value的区别

《解读@ConfigurationProperties和@value的区别》:本文主要介绍@ConfigurationProperties和@value的区别及说明,具有很好的参考价值,希望对大家... 目录1. 功能对比2. 使用场景对比@ConfigurationProperties@Value3. 核