浅谈对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实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

浅谈主机加固,六种有效的主机加固方法

在数字化时代,数据的价值不言而喻,但随之而来的安全威胁也日益严峻。从勒索病毒到内部泄露,企业的数据安全面临着前所未有的挑战。为了应对这些挑战,一种全新的主机加固解决方案应运而生。 MCK主机加固解决方案,采用先进的安全容器中间件技术,构建起一套内核级的纵深立体防护体系。这一体系突破了传统安全防护的局限,即使在管理员权限被恶意利用的情况下,也能确保服务器的安全稳定运行。 普适主机加固措施:

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

native和static native区别

本文基于Hello JNI  如有疑惑,请看之前几篇文章。 native 与 static native java中 public native String helloJni();public native static String helloJniStatic();1212 JNI中 JNIEXPORT jstring JNICALL Java_com_test_g

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否