【Android】以BaseAdapter做適配器的ListView及其效能最佳化

來源:互聯網
上載者:User

適配器的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;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.