【Android】以BaseAdapter做适配器的ListView及其性能优化
16lz
2022-05-27
适配器的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);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn list.length();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubconvertView = mInflater.inflate(R.layout.item_weixin, null);//根据布局文件实例化viewtry {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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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的性能优化
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubSystem.out.println("正在渲染第"+position+"行 +++ "+ i++);OneView oneView;if (convertView == null) {convertView = mInflater.inflate(R.layout.item_weixin, null);//根据布局文件实例化viewoneView = 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在java中动态改变控件大小
- Android(安卓)自定义时钟控件
- Android(安卓)Mediacodec H.265文件播放
- Android学习 数据存储之_文件存储
- Android(安卓)BaseAdapter基本使用及数据刷新
- Android电池电量更新 - BatteryService(转)
- android之从SD卡读取数据
- Android(安卓)Studio学习:中级控件:使用Intent进行页面之间数据传
- mybatisplus的坑 insert标签insert into select无参数问题的解决