适配器的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. UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)

    题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...

  2. C#和Javascript中 正则表达式使用的总结

    说明:本文并非原创,而是从网站上搜集了一些资料整理的!如有雷同,纯属巧合 1.js中正则表达式的使用 在js中定义正则表达式很简单,有两种方式,一种是通过构造函数,一种是通过//,也就是两个斜杠.例如 ...

  3. **【ci框架】精通CodeIgniter框架

    http://blog.csdn.net/yanhui_wei/article/details/25803945 一.大纲 1.codeigniter框架的授课内容安排 2.codeigniter框架 ...

  4. MyBatis Mapper 文件例子

    转载:http://blog.csdn.net/ppby2002/article/details/20611737 <?xml version="1.0" encoding= ...

  5. Qt 添加外部库文件(四种方法)

    Qt添加外部库文件, 一种就是直接加库文件的绝对路劲,这种方法简单,但是遇到多个库文件的时候,会很麻烦,而且,如果工程移动位置以后还需要重新配置 另一种就是相对路径了,不过Qt 编译的文件会在一个单独 ...

  6. Ajax省市联动

    以JQuery为JS,写的Ajax省市联动. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  7. php库Faker

    Faker License : MIT Source Code Allo点评:Faker是一个很神奇的项目,会自动生成拟真的数据,包括用户资料.长文本.IP.日期等等,在网站上线前测试时非常好用. g ...

  8. 用maven进行测试

    maven的重要职责之一就是自动运行单元测试,它通过maven-surefire-plugin与主流的单元测试框架junit和testng集成,并且能够自动生成丰富的结果报表. maven并不是一个单 ...

  9. HBase的Shell操作

    1.进入命令行 bin/hbase shell 2.输入help 查看各种命令组. 命令是分组的,可以执行help 'general'查看general组的命令. 3.常用命令 --显示有哪些表 li ...

  10. Android手机拍照

    参考的这个视频教程:http://v.youku.com/v_show/id_XNjI5MzkzMjQ4.html和官方API档file:///D:/Android/androidstudio/sdk ...