android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview。

首先改下布局文件:

 <?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" > <ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>

SimpleAdapter对应布局文件:

 <?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
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp" > <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" > <TextView
android:id="@+id/appnametext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="weixin" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" > <TextView
android:id="@+id/sizetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="15.23" /> <View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="3dp"
android:layout_marginTop="5dp"
android:background="#000000" /> <TextView
android:id="@+id/amounttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:text="1234567" />
</LinearLayout>
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/dowmbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:text="load" />
</LinearLayout>
</LinearLayout>
</LinearLayout> </LinearLayout>

修改下MainActivity.java文件:

 package com.example.listviewdemo1;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.TextView; public class MainActivity extends Activity { private ListView listView;
private Intent intent;
private List<Map<String, Object>> mData; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listview);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(),
R.layout.simp_adap, new String[] { "nametext", "teitext",
"amounttext" }, new int[] { R.id.appnametext,
R.id.sizetext, R.id.amounttext });
listView.setAdapter(simpleAdapter);
mData = getData();
MyAdapter adapter = new MyAdapter(this);
listView.setAdapter(adapter); } public List<Map<String, Object>> getData() {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>(); map.put("appnametext", "微信");
map.put("sizetext", "15.2");
map.put("amounttext", "1234323");
map.put("appimg", R.drawable.weixin);
list.add(map); map = new HashMap<String, Object>();
map.put("appnametext", "手机QQ");
map.put("sizetext", "8.5");
map.put("amounttext", "122073323");
map.put("appimg", R.drawable.qq);
list.add(map); map = new HashMap<String, Object>();
map.put("appnametext", "手机QQ空间");
map.put("sizetext", "6.3");
map.put("amounttext", "122393");
map.put("appimg", R.drawable.qqko);
list.add(map); map = new HashMap<String, Object>();
map.put("appnametext", "微博");
map.put("sizetext", "7.7");
map.put("amounttext", "1278323");
map.put("appimg", R.drawable.weib);
list.add(map); map = new HashMap<String, Object>();
map.put("appnametext", "陌陌");
map.put("sizetext", "6.9");
map.put("amounttext", "1279073");
map.put("appimg", R.drawable.mom);
list.add(map); map = new HashMap<String, Object>();
map.put("appnametext", "飞信");
map.put("sizetext", "6.9");
map.put("amounttext", "1279073");
map.put("appimg", R.drawable.fei);
list.add(map);
return list;
} public final class ViewHolder {
public ImageView appimg;
public TextView appnametext;
public TextView sizetext;
public TextView amounttext;
public Button dowmbutton;
} public class MyAdapter extends BaseAdapter { private LayoutInflater mInflater; public MyAdapter(Context context) {
this.mInflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return mData.size();
} @Override
public Object getItem(int arg0) {
return null;
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder holder = null;
if (arg1 == null) {
holder = new ViewHolder();
arg1 = mInflater.inflate(R.layout.simp_adap,null);
holder.appimg = (ImageView) arg1.findViewById(R.id.img);
holder.appnametext = (TextView) arg1.findViewById(R.id.appnametext);
holder.sizetext = (TextView) arg1.findViewById(R.id.sizetext);
holder.amounttext = (TextView) arg1.findViewById(R.id.amounttext);
holder.dowmbutton = (Button) arg1.findViewById(R.id.dowmbutton);
arg1.setTag(holder);
} else {
holder = (ViewHolder) arg1.getTag();
} holder.appimg.setBackgroundResource((Integer) mData.get(arg0).get("appimg"));
holder.appnametext.setText((String) mData.get(arg0).get("appnametext"));
holder.sizetext.setText((String) mData.get(arg0).get("sizetext"));
holder.amounttext.setText((String) mData.get(arg0).get("amounttext")); return arg1;
} } }

运行效果:

38.Android之ListView简单学习(一)的更多相关文章

  1. (转)Android之ListView原理学习与优化总结

    转自: http://jishu.zol.com.cn/12893.html 在整理前几篇文章的时候有朋友提出写一下ListView的性能优化方面的东西,这个问题也是小马在面试过程中被别人问到的….. ...

  2. Android Studio IDE 简单学习和介绍

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. 【转】Android:Animation的简单学习--不错

    原文网址:http://blog.csdn.net/huangbiao86/article/details/6683665 Animation动画效果.提供了一系列的动画效果,可以应用大多数 的控件. ...

  4. Android greenDAO 数据库 简单学习之基本使用

    看网上对greenDAO介绍的不错,今天就动手来试一把,看看好不好使. greenDAO 官方网站:http://greendao-orm.com/ 代码托管地址:https://github.com ...

  5. Android 之 ListView的学习

    ListView 是一个控件,一个在垂直滚动的列表中显示条目的一个控件,这些条目的内容来自于一个ListAdapter .EditText Button TextView ImageView Chec ...

  6. 42.Android之ListView中ArrayAdapter简单学习

    今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...

  7. Android学习总结(十三) ———— ListView 简单用法

    一.ListView的基本概念 在Android所有常用的原生控件当中,用法最复杂的应该就是ListView了,它专门用于处理那种内容元素很多,手机屏幕无法展示出所有内容的情况.ListView可以使 ...

  8. android SearchView和ListView简单使用

    其实我写代码最担心遇到关于适配器的使用,在我的感觉中适配器是个难度很大的知识点,但是不能因为难而不去学习啊,毕竟现在时间很充裕,可以慢慢学,所以,不会也要写,真所谓,迎难而上啊.  下面是Search ...

  9. Android ListView简单实用

    layout创建: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...

随机推荐

  1. HDU 2669 Romantic【扩展欧几里德】

    裸的扩展欧几里德,求最小的X,X=((X0%b)+b)%b,每个X都对应一个Y,代入原式求解可得 #include<stdio.h> #include<string.h> ty ...

  2. 在WPF控件上添加Windows窗口式调整大小行为

    起因 项目上需要对Canvas中的控件添加调整大小功能,即能在控件的四个角和四条边上可进行相应的拖动,类似Windows窗口那种.于是在参考以前同事写的代码基础上,完成了该功能. 代码实现 Adorn ...

  3. android机型排行榜(201509)

    http://forum.techweb.com.cn/thread-1352272-1-1.html

  4. Python进阶-面向对象

    类的成员 类的成员可以分为三类:字段.方法.属性 一:字段: 普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同 普通字段属于对象 静态字段属于类 字段的定义和使用 ...

  5. scanf和cin的差异

    scanf和cin的差异 引例:http://www.cnblogs.com/shenben/p/5516996.html 大家都知道,在C++中有两种输入.输出方式—scanf和cin,但是,它们之 ...

  6. PKI公钥处理思路

    背景: 在使用任何基于RSA服务之前,一个实体要真实可靠的获取其他实体的公钥.   1,一个可以确认公钥身份的方案:[离线确认] 主:B做同样的事情得到A的公钥. 但是这种方法扩展性差,不可行.   ...

  7. [原]iBatis.Net(C#)系列一:简介及运行环境

    转载请注明http://www.cnblogs.com/13590/archive/2013/02/27/2934580.html 摘要:介绍iBatis.Net的基本情况和运行原理,运行环境中各参数 ...

  8. GEOS库在windows中的编译和测试(vs2012)

    版本:vs2012, geos3.5 一.下载和编译 这类的文章比较,不再具体细说,可以参考 http://blog.csdn.net/wangqinghao/article/details/8201 ...

  9. Android 动画之AlphaAnimation应用详解

    窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation.AlphaAnimation(0.01f, 1.0f); 从0.01f到1.0f渐变.学过 ...

  10. win7系统cmd命令切换到指定文件夹目录

    win7 系统下的cmd命令,直接cd命令切换盘符和以往有些不同,现在默认只能在当前盘符中改变目录,如果要改变盘符则需要多加一个/d命令.如下图所示:(对cd命令的帮助 大家可借助help cd命令进 ...