【Android】以BaseAdapter做适配器的ListView及其性能优化
适配器的Java类
- package com.app.adapter;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import android.R.integer;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.app.test01.R;
- public class MyWeixinJSON extends BaseAdapter{
- private LayoutInflater mInflater;// 动态布局映射
- private JSONArray list;
- private Context context;
- private int i = 0;
- public MyWeixinJSON(JSONArray list,Context context){
- this.list = list;
- this.context = context;
- this.mInflater = LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return list.length();
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- convertView = mInflater.inflate(R.layout.item_weixin, null);//根据布局文件实例化view
- try {
- JSONObject jObject = list.getJSONObject(position);
- TextView title = (TextView) convertView.findViewById(R.id.title);//找某个控件
- title.setText(jObject.get("title").toString());//给该控件设置数据(数据从集合类中来)
- TextView time = (TextView) convertView.findViewById(R.id.time);
- time.setText(jObject.get("time").toString());
- TextView info = (TextView) convertView.findViewById(R.id.info);
- info.setText(jObject.get("info").toString());
- ImageView img = (ImageView) convertView.findViewById(R.id.img);
- img.setBackgroundResource((Integer)jObject.get("img"));
- } catch (Exception e) {
- // TODO: handle exception
- }
- return convertView;
- }
- }
Activity类
- package com.app.test01;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- import com.app.adapter.MyWeixinJSON;
- import com.app.adapter.MyWeixinList;
- public class ListViewBase extends Activity{
- private ListView lv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.weixin);
- lv = (ListView) findViewById(R.id.lv);
- MyWeixinJSON mJson = new MyWeixinJSON(getJSONArray(),this);
- lv.setAdapter(mJson);
- }
- private JSONArray getJSONArray(){
- JSONArray jsonArray = new JSONArray();
- try {
- for (int i = 1; i <= 30; i++) {
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("title", "姓名"+i);
- jsonObject.put("time", "9月29日");
- jsonObject.put("info", "我通过了你的好友验证请求,现在我们可以开始对话啦");
- jsonObject.put("img", R.drawable.special_spring_head2);
- jsonArray.put(jsonObject);
- }
- } catch (Exception e) {
- // TODO: handle exception
- }
- return jsonArray;
- }
- }
ListView的性能优化
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- System.out.println("正在渲染第"+position+"行 +++ "+ i++);
- OneView oneView;
- if (convertView == null) {
- convertView = mInflater.inflate(R.layout.item_weixin, null);//根据布局文件实例化view
- oneView = new OneView();
- oneView.title = (TextView) convertView.findViewById(R.id.title);//找某个控件
- oneView.time = (TextView) convertView.findViewById(R.id.time);
- oneView.info = (TextView) convertView.findViewById(R.id.info);
- oneView.img = (ImageView) convertView.findViewById(R.id.img);
- convertView.setTag(oneView);//把View和某个对象关联起来
- } else {
- oneView = (OneView) convertView.getTag();
- }
- JSONObject jObject = null;
- try {
- jObject = list.getJSONObject(position);//根据position获取集合类中某行数据
- oneView.title.setText(jObject.get("title").toString());//给该控件设置数据(数据从集合类中来)
- oneView.time.setText(jObject.get("time").toString());
- oneView.info.setText(jObject.get("info").toString());
- oneView.img.setBackgroundResource((Integer)jObject.get("img"));
- } catch (Exception e) {
- // TODO: handle exception
- }
- return convertView;
- }
- /** 把每行布局文件的各个控件包装成一个对象 */
- private class OneView{
- TextView title;
- TextView time;
- TextView info;
- ImageView img;
- }
【Android】以BaseAdapter做适配器的ListView及其性能优化的更多相关文章
- 转-ListView的性能优化之convertView和viewHolder
ListView的性能优化之convertView和viewHolder 2014-05-14 参考:http://www.cnblogs.com/xiaowenji/archive/2010/12/ ...
- 安卓ListView的性能优化
在安卓APP中LIstView这个控件可以说基本上是个APP就会用到,但是关于ListView除了需要了解其最基本的用法外,作为一个要做出高性能APP的程序员还需了解一些关于LIstView控件性能优 ...
- 【Android】以SimpleAdapter做适配器的ListView和GridView
SimpleAdapter介绍 SimpleAdapter是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图. 构造函数 public SimpleAdapter (Context co ...
- Android 对BaseAdapter做优化处理
对于BaseAdapter相信大家都不陌生,都知道该怎样用.怎样显示数据.怎样尽可能的把每个item做的令自己满意.但问题来了:有些朋友会说我界面做的非常的漂亮,数据也显示的非常完美,但是问什么我的L ...
- ListView之性能优化
listview加载的核心是其adapter,本文通过减少adapter中创建.处理view的次数来提高listview加载的性能,总共分四个层次: 0.最原始的加载 1.利用convertView ...
- ListView的性能优化之convertView和viewHolder
转载请注明出处 最近碰到的面试题中经常会碰到问"ListView的优化"问题.所以就拿自己之前写的微博客户端的程序做下优化. 自己查了些资料,看了别人写的博客,得出结论,ListV ...
- 安卓中listview中性能优化的处理
1.在adapter中的getView方法中尽量少使用逻辑 不要在你的getView()中写过多的逻辑代码,我们能够将这些代码放在别的地方.比如: 优化前的getView(): @Override p ...
- ListView的性能优化
@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHol ...
- Android开发进阶从小工到专家之性能优化
随机推荐
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- POJ 2253 Frogger(floyd)
http://poj.org/problem?id=2253 题意 : 题目是说,有这样一只青蛙Freddy,他在一块石头上,他呢注意到青蛙Fiona在另一块石头上,想去拜访,但是两块石头太远了,所以 ...
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- js的数据处理记录
mongoDB的mapReduce返回的数据有可能会非常之多,所以单独拎出来先在浏览器里面玩一玩; // 数据源 var arr = [ {"address": "四川汶 ...
- CreateTwoArray
public class CreateTwoArray{ public static void main(String []args){ int[][]arr=new int [2][3]; Syst ...
- 格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。
格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数. (* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentT ...
- [iOS]如何给Label或者TextView赋HTML数据
// // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights ...
- Spring AOP: Spring之面向方面编程
Spring AOP: Spring之面向方面编程 面向方面编程 (AOP) 提供从另一个角度来考虑程序结构以完善面向对象编程(OOP). 面向对象将应用程序分解成 各个层次的对象,而AOP将程序分解 ...
- HDU5090——Game with Pearls(匈牙利算法|贪心)(2014上海邀请赛重现)
Game with Pearls Problem DescriptionTom and Jerry are playing a game with tubes and pearls. The rule ...
- SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文章之一)
SQL索引一步到位(此文章为“数据库性能优化二:数据库表优化”附属文章之一) SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭 ...