Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the implementation of BaseAdapter by Android

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how Android customizes the implementation of BaseAdapter. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The principle of implementation is that we have to look up the control in setData every time, and then setXXX () or something. We can put this write implementation into ViewHolder and write a chained method in ViewHolder to help us implement the function. The chained method is as follows:

Public ViewHolder setText (int viewId, String data) {TextView tv = getView (viewId); tv.setText (data); return this;}

Through this method, we can set the corresponding content very well. Just pass an id that needs to set the control, and then pass the corresponding data to achieve the effect of setting the text. Let's take a look at the code in our MyAdapter.java.

MyAdapter.java

/ * before MyAdapter*/public class MyAdapter extends MyBaseAdapter {public MyAdapter (List data) {super (data);} @ Override public void setData (ViewHolder holder, Student t) {TextView tvName = holder.getView (R.id.mTv1); tvName.setText (t.getName ()); TextView tvSex = holder.getView (R.id.mTv2); tvSex.setText (t.getSex ());}} / * optimized MyAdapter*/public class MyAdapter extends MyBaseAdapter {public MyAdapter (List data) {super (data) @ Override public void setData (ViewHolder holder, Student t) {holder.setText (R.id.mTv1, t.getName ()) .setText (R.id.mTv2, t.getSex ());}}

Ok, compared with the implementation in setData, we can replace the above implementation with only one piece of code here, isn't it more convenient? in this way, our extension is more convenient, if we not only set the text content, but also set the picture through the picture control? Quite simply, we just need to add the method we want to implement in ViewHolder, such as setting the image, we can add the following code:

Public ViewHolder setImageResource (int viewId,int resId) {ImageView img = getView (viewId); img.setImageResource (resId); return this;} public ViewHolder setImageBitmap (int viewId, Bitmap bm) {ImageView img = getView (viewId); img.setImageBitmap (bm); return this;}

After adding these two methods, it is easy to set up the picture, just pass the corresponding parameters in setData

@ Override public void setData (ViewHolder holder, Student t) {holder.setText (R.id.mTv1, t.getName ()) .setText (R.id.mTv2, t.getSex ()); holder.setImageResource (R.id.img1, resource id) .setImageBitmap (R.id.img2jobm);}

All right, a general adapter has been completed, and the overall framework and code have been implemented. I will synthesize all the code and write it below for your convenience. They are MainActivity.java (main interface class, which is responsible for passing parameters to set ListView data), MyAdapter.java (custom adapter), MyBaseAdapter.java (general adapter class), ViewHolder.java (general holding class object), and entity class Student.java.

MainActivity.java

Public class MainActivity extends AppCompatActivity {private List data; private ListView mList; MyAdapter adapter; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); getData (); mList = (ListView) findViewById (R.id.mList); adapter = new MyAdapter (data); mList.setAdapter (adapter);} private void getData () {data = new ArrayList (); Student stu = null; for (int I = 0; I < 20; iTunes +) {stu = new Student () Stu.setName ("name" + I); stu.setSex (I% 2 = = 0? "male": "female"); data.add (stu);}

MyAdapter.java

Public class MyAdapter extends MyBaseAdapter {public MyAdapter (List data) {super (data);} @ Override public void setData (ViewHolder holder, Student t) {holder.setText (R.id.mTv1, t.getName ()) .setText (R.id.mTv2, t.getSex ());}}

MyBaseAdapter.java

Public abstract class MyBaseAdapter extends BaseAdapter {protected List data; public MyBaseAdapter (List data) {this.data = data;} @ Override public int getCount () {return data = = null? 0: data.size ();} @ Override public Object getItem (int position) {return data.get (position);} @ Override public long getItemId (int position) {return position;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = ViewHolder.getHolder (convertView,parent,position, R.layout.list_item) SetData (holder,data.get (position)); return holder.getConvertView ();} public abstract void setData (ViewHolder holder,T t);}

ViewHolder.java

Public class ViewHolder {private int position; private SparseArray array; private View convertView; private Context context; private ViewHolder (ViewGroup parent, int position, int layout) {this.position = position; this.context = parent.getContext (); convertView = LayoutInflater.from (parent.getContext ()) .propagate (layout, null); convertView.setTag (this); array = new SparseArray ();} public static ViewHolder getHolder (View convertView, ViewGroup parent, int position, int layout) {if (convertView = = null) {return new ViewHolder (parent, position, layout) } else {ViewHolder holder = (ViewHolder) convertView.getTag (); holder.position = position; return holder;}} public T getView (int viewId) {View view = array.get (viewId); if (view = = null) {view = convertView.findViewById (viewId); array.put (viewId, view);} return (T) view;} public View getConvertView () {return convertView;} public ViewHolder setText (int viewId, String data) {TextView tv = getView (viewId); tv.setText (data); return this } public ViewHolder setImageResource (int viewId, int resId) {ImageView img = getView (viewId); img.setImageResource (resId); return this;} public ViewHolder setImageBitmap (int viewId, Bitmap bm) {ImageView img = getView (viewId); img.setImageBitmap (bm); return this;}}

Student.java

Public class Student {private String name; private String sex; public String getName () {return name;} public void setName (String name) {this.name = name;} public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;}}

Thank you for reading! This is the end of this article on "how to customize Android to implement BaseAdapter". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report