Hello Friends, today we are going see an example of ViewHolder Pattern with BaseAdapter in Android. I am sharing this code example because few weeks ago I was working on a project and after completing code, I have analyzed my code using “Inspect Code…” feature of Android Studio which is available under Analyze menu. And I found performance issue in my code (see screenshot below). Then I googled and found this solution which I am sharing now.
Let’s Code Now
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.zatackcoder.viewholderpatternwithbaseadapter; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; import java.util.List; import java.util.Random; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); List<Item> items = new ArrayList<>(); Random random = new Random(); for(int i = 1; i <= 2000; i++) { items.add(new Item("Item " + i, random.nextInt(1000))); } ListView listView = findViewById(R.id.list_view); listView.setAdapter(new MyBaseAdapter(this, items)); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </androidx.constraintlayout.widget.ConstraintLayout> |
MyBaseAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.zatackcoder.viewholderpatternwithbaseadapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import android.widget.Toast; import java.util.List; class MyBaseAdapter extends BaseAdapter { private final Context context; private final List<Item> items; public MyBaseAdapter(Context context, List<Item> items) { this.context = context; this.items = items; } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); viewHolder = new ViewHolder(); viewHolder.nameTV = convertView.findViewById(R.id.name); viewHolder.priceTV = convertView.findViewById(R.id.price); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } /* convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); ViewHolder viewHolder = new ViewHolder(); viewHolder.nameTV = convertView.findViewById(R.id.name); viewHolder.priceTV = convertView.findViewById(R.id.price); */ final Item item = items.get(position); viewHolder.nameTV.setText(item.getName()); viewHolder.priceTV.setText(String.valueOf(item.getPrice())); convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context, "You Just Clicked " + item.getName(), Toast.LENGTH_LONG).show(); } }); return convertView; } static class ViewHolder { TextView nameTV; TextView priceTV; } } |
list_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="#ffffff" android:clickable="true" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:attr/selectableItemBackground" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Name" android:textFontWeight="bold" android:textSize="15dp" /> <TextView android:id="@+id/price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Price: 100" android:textSize="12dp" /> </LinearLayout> </LinearLayout> |
Item.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.zatackcoder.viewholderpatternwithbaseadapter; class Item { private String name; private Integer price; public Item(String name, Integer price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } } |
Video
AndroidStudio Project Download
ViewHolderPatternWithBaseAdapter AndroidStudio Project
1 file(s) 8.13 MB
References
https://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html