android studio   Listview简单实例

//layout 布局

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

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

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

>

<ListView

android:layout_width="match_parent"

android:layout_height="match_parent"

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

/>

</RelativeLayout>

//代码

package com.example.test.testlistview;

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class ListViewArrayadapter extends ListActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_list_view_arrayadapter);

List<String>ls = new ArrayList<String>();

ls.add("测试数据1");

ls.add("测试数据2");

ls.add("测试数据1");

ls.add("测试数据2");

ls.add("测试数据1");

ls.add("测试数据2");

ls.add("测试数据1");

ls.add("测试数据2");

ls.add("测试数据1");

ls.add("测试数据2");

ls.add("测试数据1");

ls.add("测试数据2");

ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,ls);

setListAdapter(adapter);  //继承ListActivity才有此函数

}

@Override

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

String s =((TextView)v).getText().toString();

Toast.makeText(this,"提示"+position+s,Toast.LENGTH_LONG).show();

super.onListItemClick(l, v, position, id);

}

}

//效果图

简单的listview,关键是ArrayAdapter中布局和数据的适配。android.R.layout.simple_list_item_1系统自带的样式,可以根据自己的要求更改布局样式。List<String>ls = new ArrayList<String>() 用于显示的数据。

//SimpleAdapter 适配Listview

//layout

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

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

android:orientation="horizontal" android:layout_width="match_parent"

android:layout_height="match_parent">

<ImageView

android:id="@+id/img2"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

/>

<LinearLayout

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical">

<TextView

android:id="@+id/title2"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<TextView

android:id="@+id/price"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

</LinearLayout>

</LinearLayout>

//关键代码设置listview

SimpleAdapter adapter1 =new SimpleAdapter(this,lm,R.layout.simple2, new String[]{"title","img","price"},new int[]{R.id.title2,R.id.img2,R.id.price});

setListAdapter(adapter1);

//得到数据

public List<Map<String, Object>> getData2()

{

Map<String,Object>value =new HashMap<String,Object>();

List<Map<String,Object>>lm =new ArrayList<Map<String,Object>>();

value.put("title","苹果");

value.put("img",R.drawable.aphone);

value.put("price","价格:5800");

lm.add(value);

value =new HashMap<String,Object>();

value.put("title", "三星");

value.put("img",R.drawable.sphone);

value.put("price","价格:4800");

lm.add(value);

value =new HashMap<String,Object>();

value.put("title","华为");

value.put("img",R.drawable.hphone);

value.put("price","价格:6000");

lm.add(value);

return  lm;

}

//SimpleCursorAdapter适配Listview 用于数据库数据的显示

//关键代码

//获得一个指向系统通讯录数据库的Cursor对象获得数据来源

Cursor cursor = getContentResolver().query(Contacts.People.CONTENT_URI,null,null,null,null);

startManagingCursor(cursor);

//实例化列表适配器

ListAdapter la = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor,new String[]{Contacts.People.NAME,Contacts.People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});

setListAdapter(la);

//一定要在AndroidManifest.xml加读取联系人的权限。

<uses-permission android:name="android.permission.READ_CONTACTS"/>

//代码下载

http://down.51cto.com/data/2119105

时间: 2024-08-06 03:43:30

android studio   Listview简单实例的相关文章

Android Studio IDE 简单学习和介绍

1.下载安装请自行去Android 官放网站去下载,建议FQ. 2.eclipse 和 AS 的差异 (1)android studio是单工程的开发模式 (2)android studio中的application相当于eclipse里的workspace概念 (3)android studio中的module相当于eclipse里的project概念 3.新建工程后AS 的 project 目录简单介绍. (1).idea://AS生成的工程配置文件,类似Eclipse的project.pr

Android Studio一些简单设置

      简单设置   1.默认主题设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面. Settings --> Appearance --> Theme ,选择 Darcula 主题即可 2系统字体设置 如果你的Android Studio界面中,中文显示有问题,或者选择中文目录显示有问题,或者想修改菜单栏的字体,可以这么设置. Settings --> Appearance ,勾选 Override default fonts by (not re

android studio实现简单考试应用程序

一.问题 1.如图所示,设计一个包含四种题型的简单考试应用程序(具体考试题目可以选用以下设计,也可以自己另外确定),项目名称:zuoye06_666 :(666,改成自己的实际编号). 2.布局管理器任选(约束布局相对容易实现). 3.“提交”按钮的Text通过字符串资源赋值,不要直接输入“提交”两个字. 4.每题按25分计算,编写相应的程序,答题完成后单击“提交”按钮,在“总得分:”右边文本框中显示实际得分:同时,显示一个Toast消息框: 答对不足3题,显示:“还需努力啊!”: 答对3题,显

在Android Studio进行“简单配置”单元测试(Android Junit)

起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码.后来今天突然自己又抽风.又想去弄一下Android junit. 本文基于做过Eclipse开发使用过Android junit,如果Eclipse的Android Junit没有使用过,就我没有说过吧! 准备环境,配置 官网Demo地址:https://github.com/googlesamples/android-test

38.Android之ListView简单学习(一)

android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layou

Android Studio的简单设置

1.主题修改: 可能大家会觉得软件的界面不太好看,我们可以换一下主题.选择菜单栏"File--settings--apperance--theme",主题选择Darcula: 2.导入第三方主题: 系统提供的两种主题可能都不太好看,我们可以进入网站http://color-themes.com/来获取第三方主题. 上图中,下载下来之后,是一个jar包.那怎么导入到Android Studio呢? 别着急,回到Android Studio,选择菜单栏" File-Import

重大发现Android studio 如何简单快速修改package name

好多人都发现Android studio修改包名比较麻烦,只能一级一级的修改,今天偶尔发现了一个快捷方法. 废话不多说: 1 打开项目的AndroidManifest.xml文件 2 鼠标光笔定位到你想修改的包名,右键Refactor-->rename即可 比如包名是com.example.myapplication,我想把中间的example修改为hello,则把光标定位到example右键修改即可

android studio listview长按删除

activity_main.xml 的代码 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-

Android自定义Dialog简单实例

做Android应用中,最缺少不了的就是自定义Dialog,对于系统默认提供的Dialog样式,一般都不复合我们应用的样式. 自定义Dialog需要3步骤即可: 1.主要的重写Dialog的Java类 2.清除Dialog Theme,在style.xml文件中加一个即可 3.使用方法 一.创建CustomPopDialog2.java类 package com.lenovocw.music.app.widget; import com.lenovocw.zhuhaizxt.R; import