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. 2014 UESTC 暑前集训队内赛(2) 部分解题报告

    B.Cuckoo for Hashing 模拟题. 代码: #include <iostream> #include <cstdio> #include <cstring ...

  2. Linux 删除文件中某一行的方法

    如果有一个abc.txt文件,内容是: aaa bbb ccc ddd eee fff 如果要删除aaa,那么脚本可以这样写: sed -i '/aaa/d' abc.txt 如果删除的是一个变量的值 ...

  3. android源码在线查看

    http://grepcode.com/project/repository.grepcode.com/java/ext/com.google.android/android/

  4. 005医疗项目-模块一:用户的查找:1.用户表查询的sql语句

    这是医疗项目的第一个模块:做一个用户的查询,可以根据用户的账号,用户的名称,单位的名称,用户的类型去查询.要求效果如下:

  5. GIT 专贴

    1.官网 git-scm.com github.com 代码库 2.源码

  6. use AP_VENDOR_PUB_PKG.Update_Vendor_Site_Public to u ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public

    ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public 发现此问题的经过: ...

  7. php基础10:字符串中插入变量

    <?php //插入字符串 //1.双引号可以解析字符串中的变量:但是前后不能跟中文符号 $username = "gaoxiong"; echo "my name ...

  8. 通过HttpClient来调用Web Api接口~续~实体参数的传递

    并且我们知道了Post,Put方法只能有一个FromBody参数,再有多个参数时,上讲提到,需要将它封装成一个对象进行传递,而这讲主要围绕这个话题来说,接口层添加一个新类User_Info,用来进行数 ...

  9. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  10. 实验一报告 20135238&20135207

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础              班级:1352 姓名:(按贡献大小排名)龚睿  王国伊 学号:(按贡献大小排 ...