适配器的Java类

  1. package com.app.adapter;
  2. import org.json.JSONArray;
  3. import org.json.JSONObject;
  4. import android.R.integer;
  5. import android.content.Context;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.BaseAdapter;
  10. import android.widget.ImageView;
  11. import android.widget.TextView;
  12. import com.app.test01.R;
  13. public class MyWeixinJSON extends BaseAdapter{
  14. private LayoutInflater mInflater;// 动态布局映射
  15. private JSONArray list;
  16. private Context context;
  17. private int i = 0;
  18. public MyWeixinJSON(JSONArray list,Context context){
  19. this.list = list;
  20. this.context = context;
  21. this.mInflater = LayoutInflater.from(context);
  22. }
  23. @Override
  24. public int getCount() {
  25. // TODO Auto-generated method stub
  26. return list.length();
  27. }
  28. @Override
  29. public Object getItem(int position) {
  30. // TODO Auto-generated method stub
  31. return null;
  32. }
  33. @Override
  34. public long getItemId(int position) {
  35. // TODO Auto-generated method stub
  36. return 0;
  37. }
  38. @Override
  39. public View getView(int position, View convertView, ViewGroup parent) {
  40. // TODO Auto-generated method stub
  41. convertView = mInflater.inflate(R.layout.item_weixin, null);//根据布局文件实例化view
  42. try {
  43. JSONObject jObject = list.getJSONObject(position);
  44. TextView title = (TextView) convertView.findViewById(R.id.title);//找某个控件
  45. title.setText(jObject.get("title").toString());//给该控件设置数据(数据从集合类中来)
  46. TextView time = (TextView) convertView.findViewById(R.id.time);
  47. time.setText(jObject.get("time").toString());
  48. TextView info = (TextView) convertView.findViewById(R.id.info);
  49. info.setText(jObject.get("info").toString());
  50. ImageView img = (ImageView) convertView.findViewById(R.id.img);
  51. img.setBackgroundResource((Integer)jObject.get("img"));
  52. } catch (Exception e) {
  53. // TODO: handle exception
  54. }
  55. return convertView;
  56. }
  57. }

Activity类

  1. package com.app.test01;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import org.json.JSONArray;
  6. import org.json.JSONObject;
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.widget.ListView;
  10. import com.app.adapter.MyWeixinJSON;
  11. import com.app.adapter.MyWeixinList;
  12. public class ListViewBase extends Activity{
  13. private ListView lv;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.weixin);
  19. lv = (ListView) findViewById(R.id.lv);
  20. MyWeixinJSON mJson = new MyWeixinJSON(getJSONArray(),this);
  21. lv.setAdapter(mJson);
  22. }
  23. private JSONArray getJSONArray(){
  24. JSONArray jsonArray = new JSONArray();
  25. try {
  26. for (int i = 1; i <= 30; i++) {
  27. JSONObject jsonObject = new JSONObject();
  28. jsonObject.put("title", "姓名"+i);
  29. jsonObject.put("time", "9月29日");
  30. jsonObject.put("info", "我通过了你的好友验证请求,现在我们可以开始对话啦");
  31. jsonObject.put("img", R.drawable.special_spring_head2);
  32. jsonArray.put(jsonObject);
  33. }
  34. } catch (Exception e) {
  35. // TODO: handle exception
  36. }
  37. return jsonArray;
  38. }
  39. }

ListView的性能优化

  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3. // TODO Auto-generated method stub
  4. System.out.println("正在渲染第"+position+"行  +++  "+ i++);
  5. OneView oneView;
  6. if (convertView == null) {
  7. convertView = mInflater.inflate(R.layout.item_weixin, null);//根据布局文件实例化view
  8. oneView = new OneView();
  9. oneView.title = (TextView) convertView.findViewById(R.id.title);//找某个控件
  10. oneView.time = (TextView) convertView.findViewById(R.id.time);
  11. oneView.info = (TextView) convertView.findViewById(R.id.info);
  12. oneView.img = (ImageView) convertView.findViewById(R.id.img);
  13. convertView.setTag(oneView);//把View和某个对象关联起来
  14. } else {
  15. oneView = (OneView) convertView.getTag();
  16. }
  17. JSONObject jObject = null;
  18. try {
  19. jObject = list.getJSONObject(position);//根据position获取集合类中某行数据
  20. oneView.title.setText(jObject.get("title").toString());//给该控件设置数据(数据从集合类中来)
  21. oneView.time.setText(jObject.get("time").toString());
  22. oneView.info.setText(jObject.get("info").toString());
  23. oneView.img.setBackgroundResource((Integer)jObject.get("img"));
  24. } catch (Exception e) {
  25. // TODO: handle exception
  26. }
  27. return convertView;
  28. }
  29. /** 把每行布局文件的各个控件包装成一个对象  */
  30. private class OneView{
  31. TextView title;
  32. TextView time;
  33. TextView info;
  34. ImageView img;
  35. }

【Android】以BaseAdapter做适配器的ListView及其性能优化的更多相关文章

  1. 转-ListView的性能优化之convertView和viewHolder

    ListView的性能优化之convertView和viewHolder 2014-05-14 参考:http://www.cnblogs.com/xiaowenji/archive/2010/12/ ...

  2. 安卓ListView的性能优化

    在安卓APP中LIstView这个控件可以说基本上是个APP就会用到,但是关于ListView除了需要了解其最基本的用法外,作为一个要做出高性能APP的程序员还需了解一些关于LIstView控件性能优 ...

  3. 【Android】以SimpleAdapter做适配器的ListView和GridView

    SimpleAdapter介绍 SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图. 构造函数 public SimpleAdapter (Context co ...

  4. Android 对BaseAdapter做优化处理

    对于BaseAdapter相信大家都不陌生,都知道该怎样用.怎样显示数据.怎样尽可能的把每个item做的令自己满意.但问题来了:有些朋友会说我界面做的非常的漂亮,数据也显示的非常完美,但是问什么我的L ...

  5. ListView之性能优化

    listview加载的核心是其adapter,本文通过减少adapter中创建.处理view的次数来提高listview加载的性能,总共分四个层次: 0.最原始的加载 1.利用convertView ...

  6. ListView的性能优化之convertView和viewHolder

    转载请注明出处 最近碰到的面试题中经常会碰到问"ListView的优化"问题.所以就拿自己之前写的微博客户端的程序做下优化. 自己查了些资料,看了别人写的博客,得出结论,ListV ...

  7. 安卓中listview中性能优化的处理

    1.在adapter中的getView方法中尽量少使用逻辑 不要在你的getView()中写过多的逻辑代码,我们能够将这些代码放在别的地方.比如: 优化前的getView(): @Override p ...

  8. ListView的性能优化

    @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHol ...

  9. Android开发进阶从小工到专家之性能优化

随机推荐

  1. HDU 1385 Minimum Transport Cost (最短路,并输出路径)

    题意:给你n个城市,一些城市之间会有一些道路,有边权.并且每个城市都会有一些费用. 然后你一些起点和终点,问你从起点到终点最少需要多少路途. 除了起点和终点,最短路的图中的每个城市的费用都要加上. 思 ...

  2. hdu1022 Train Problem I

    http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...

  3. Openflow的转发与传统的转发区别和优势

    来源:(SDN QQ群语录20130819) http://www.sdnap.com/sdnap-post/2411.html 山东同学-菜(Q群279796875) 21:40:21我是想问,op ...

  4. Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件

    原文:转:Java IDE 编辑器 --- IntelliJ IDEA 进阶篇 生成 hibernate 实体与映射文件 2011-04-30 12:50 很多人不知道怎么用 IntelliJ IDE ...

  5. iOS ARC下dealloc过程及.cxx_destruct的探究

    前言 这次探索源自于自己一直以来对ARC的一个疑问,在MRC时代,经常写下面的代码: 1 2 3 4 5 6 7 8 9 - (void)dealloc {     self.array = nil; ...

  6. Android Include标签

    编程的世界有的时候很微妙,有的时候就好像是在解决一个哲学问题,Android开发的时候,所有的布局,颜色,等(其实这些都可以称之为资源,Android中的资源是指非代码部分,如图片.音频.视频.字符等 ...

  7. Java API —— ArrayList类 & Vector类 & LinkList类

    1.ArrayList类     1)ArrayList类概述         · 底层数据结构是数组,查询快,增删慢         · 线程不安全,效率高     2)ArrayList案例   ...

  8. Hibernate框架简述

    Hibernate的核心组件在基 于MVC设计模式的JAVA WEB应用中,Hibernate可以作为模型层/数据访问层.它通过配置文件(hibernate.properties或 hibernate ...

  9. 注意:C++中double的表示是有误差的

    注意:C++中double的表示是有误差的,直接通过下面的例子看一下 #include<iostream> using namespace std; int main() { double ...

  10. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)

    题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...