[Android] uses BaseAdapter as the adapter's ListView and Its Performance Optimization

Source: Internet
Author: User

Java class of the adapter

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; // Dynamic Layout ING private JSONArray list; private Context 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); // instantiate viewtry {JSONObject jObject = list based on the layout file. getJSONObject (position); TextView title = (TextView) convertView. findViewById (R. id. title); // find the title of a control. setText (jObject. get ("title "). toString (); // set data for the control (data from the Collection class) 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 Type
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", "name" + I); jsonObject. put ("time", "September 29"); jsonObject. put ("info", "I have passed your friend verification request, now we can start to talk"); jsonObject. put ("img", R. drawable. special_spring_head2); jsonArray. put (jsonObject) ;}} catch (Exception e) {// TODO: handle exception} return jsonArray ;}}
ListView Performance Optimization
@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubSystem. out. println ("rendering the nth" + position + "Row ++" + I ++); OneView oneView; if (convertView = null) {convertView = mInflater. inflate (R. layout. item_weixin, null); // viewoneView = new OneView (); oneView is instantiated Based on the layout file. title = (TextView) convertView. findViewById (R. id. title); // find a widget 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); // associate View with an object} else {oneView = (OneView) convertView. getTag ();} JSONObject jObject = null; try {jObject = list. getJSONObject (position); // obtain the oneView of a row in the Collection class based on the position. title. setText (jObject. get ("title "). toString (); // set data for the control (data comes from the Collection class) 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 ;} /** wrap each control of each layout file in each row into an object */private class OneView {TextView title; TextView time; TextView info; ImageView img ;}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.