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. Mango Weekly Training Round #3 解题报告

    A. Codeforces 92A Chips 签到题.. #include <iostream> #include <cstdio> #include <cstring ...

  2. EZ GUI Button和Checkbox创建

    第一次接触EZ GUI,记录学习过程 准备工作 导入资源 导入 EZ GUI 1.0795.unitypackage 和 SpriteManager2 v1.92.unitypackage EZGUI ...

  3. HASHKILL

    6ac66ed89ef9654cf25eb88c21f4ecd0是flag的MD5码,(格式为ctf{XXX_XXXXXXXXXXX_XXXXX})由一个0-1000的数字,下划线,纽约的一个区,下划 ...

  4. ArcGis 获取地理、平面坐标系

                                         ESRI.ArcGIS.Geometry.ISpatialReference spatialReference = spati ...

  5. Linux下检测IP地址冲突及解决方法

    问题说明:在公司办公网内的一台物理机A上安装了linux系统(ip:192.168.9.120),在上面部署了jenkins,redmine,svn程序.由于是在办公网内,这台机器和同事电脑都是在同一 ...

  6. 后台跳转到登录页嵌套在iframe的问题(MVC例)

    //首页 public ActionResult Index() { if (!Request.IsAuthenticated) //判断权限,没有登录就跳回登录页 {string url = Url ...

  7. WP 8.1 中挂起时页面数据保存方式

    1.保存到Applicaion Data配置信息中: 保存: private void testTB_TextChanged(object sender, TextChangedEventArgs e ...

  8. SpringMVC中的@PathVariable

    @PathVariable是用来动态获得url中的参数的,代码示例如下: 可以在代码中获得lev_1.lev_2和target参数的值看一下 // 支持跳转到WEB-INF/目录下二层目录 @Requ ...

  9. MVC4项目中验证用户登录一个特性就搞定

    在开发过程中,需要用户登陆才能访问指定的页面这种功能,微软已经提供了这个特性. // 摘要: // 表示一个特性,该特性用于限制调用方对操作方法的访问. [AttributeUsage(Attribu ...

  10. python数字图像处理(3):图像像素的访问与裁剪

    图片读入程序中后,是以numpy数组存在的.因此对numpy数组的一切功能,对图片也适用.对数组元素的访问,实际上就是对图片像素点的访问. 彩色图片访问方式为: img[i,j,c] i表示图片的行数 ...