浅谈对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 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

go 指针接收者和值接收者的区别小结

《go指针接收者和值接收者的区别小结》在Go语言中,值接收者和指针接收者是方法定义中的两种接收者类型,本文主要介绍了go指针接收者和值接收者的区别小结,文中通过示例代码介绍的非常详细,需要的朋友们下... 目录go 指针接收者和值接收者的区别易错点辨析go 指针接收者和值接收者的区别指针接收者和值接收者的