How to implement android ListView using custom adapter | Android | Java
In this article, I will demonstrate how we can implement an android listview with custom apdater.
Step 1. Open Android Studio, click on a new project and select an empty layout then enter your project name then hit finish.
Step 2. Now take list view in your main activity layout as bellow
<ListView android:layout_below="@+id/tv_title" android:id="@+id/lv_items" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView>
Or simply replace with your activity.xml file with below code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Android List View" android:textAlignment="center" android:layout_marginBottom="10dp"/> <ListView android:layout_below="@+id/tv_title" android:id="@+id/lv_items" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </RelativeLayout>
Step 3. Now create a model class to add multi-dimension array list as below and name it model.java .
model.java
package com.sahebul.simplelistview; /** * Created by Sahebul on 25/5/19. */ public class model { private String ItemName; private String ItemQty; public String getItemName() { return ItemName; } public void setItemName(String itemName) { ItemName = itemName; } public String getItemQty() { return ItemQty; } public void setItemQty(String itemQty) { ItemQty = itemQty; } public model(String itemName, String itemQty) { ItemName = itemName; ItemQty = itemQty; } public model() { } }
Step 4. Now create your adapter class as shown below
Adapter.java
package com.sahebul.simplelistview; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by Sahebul on 25/5/19. */ public class Adapter extends BaseAdapter { List<model> data; Activity activity; private LayoutInflater inflater; public Adapter(Activity activity, List<model> data) { inflater = activity.getLayoutInflater(); this.activity = activity; this.data = data; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final Adapter.MyViewHolder mViewHolder; if (convertView == null) { convertView = inflater.inflate(R.layout.single_item, parent, false); mViewHolder = new Adapter.MyViewHolder(convertView); convertView.setTag(mViewHolder); } else { mViewHolder = (Adapter.MyViewHolder) convertView.getTag(); } mViewHolder.tv_name.setText(data.get(position).getItemName()); mViewHolder.tv_qty.setText(data.get(position).getItemQty()); return convertView; } public class MyViewHolder { private TextView tv_name, tv_qty; public MyViewHolder(View item) { tv_name = (TextView) item.findViewById(R.id.tv_name); tv_qty = (TextView) item.findViewById(R.id.tv_qty); } } }
Step 5. Now open your MainActivity.java file and initialize an array list of an item which we will show on list view as below.
ArrayList<model> list=new ArrayList<>();
step 6. Create load_items() function in MainActivity.java to load the array lits as below
public void load_items(){ for (int i=0;i<20;i++){ Integer num=i+1; list.add(new model("Item "+num,num+"")); } }
Now just call the load_items() function on your onCreate() method.
Step 6. Now set your adapter as below in onCreate method
Adapter adapter = new Adapter(this, list ); itemListView.setAdapter(adapter);
MainActivity.java
package com.sahebul.simplelistview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ArrayList<model> list=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView itemListView=findViewById(R.id.lv_items); //load the item list to display load_items(); //set a string adapter Adapter adapter = new Adapter(this, list ); itemListView.setAdapter(adapter); } public void load_items(){ for (int i=0;i<20;i++){ Integer num=i+1; list.add(new model("Item "+num,num+"")); } } }
0 Comments
Leave a reply
Your email address will be private. Required fields are marked with *