How to make a custom adapter in android?

Android adapter is basically used to manage the data. The adapter takes the data (i.e data may be in the form of the array) and creates the view. Then it gives the data to the adapter view. This adapter view displays the data just the way you want. The adapter view is responsible for controlling how the data should be displayed. The data can be displayed vertically or in the form of rows and columns.

Custom adapter extends Base adapter and it is very flexible. It allows you to do whatever you want. In order to make a custom adapter create a class Customadapter that extends Baseadapter and that implements abstract methods. Now you need to create a model class for saving your data. Save the model data in the array list for each row. At last, you need to create a custom adapter class and pass the array list as a parameter in the custom adapter constructor.

Steps to create CustomAdapter


1. Create a package named view under the package create class named CustomAdapter


 


Paste following code to the custom adapter



import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.app_using_base_adpt.R;

import org.w3c.dom.Text;

public class CustomAdapter extends BaseAdapter {

private Context context;
private String[] name;
private String[] address;
private int[] roll_no;
public CustomAdapter(Context c, String[] names, String[] addr, int[] roll) {
this.context = c;
this.name = names;
this.address = addr;
this.roll_no = roll;
}


@Override
public int getCount() {
return name.length;
}

@Override
public Object getItem(int i) {
return i;
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(context).inflate(R.layout.my_item,viewGroup,false);

setUpData(view,i);
return view;
}


private void setUpData(View view, int i) {
LinearLayout linearLayout = view.findViewById(R.id.my_layout);
TextView tv = view.findViewById(R.id.item_name);
TextView tv_address = view.findViewById(R.id.item_address);
TextView tv_roll = view.findViewById(R.id.item_roll);

tv.setText(name[i]);
tv_address.setText(address[i]);
tv_roll.setText(address[i]);
tv_roll.setText(String.valueOf(roll_no[i]));
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "Name : "+name[i], Toast.LENGTH_SHORT).show();
}
});

}
}

 2. Create a package named model under the package create a class named DataModel


 

Paste the following code to the DataModel


public class DataModel {


private String[] names = {
"Pushpa","Ram","Hari","Gopal",
"Pushpa","Ram","Hari","Gopal",
"Pushpa","Ram","Hari","Gopal",
"Pushpa","Ram","Hari","Gopal",
"Pushpa","Ram","Hari","Gopal"
};

private String[] address = {
"Kathmandu","Nepal","India","Pakistan",
"Dang","Rolpa","Hari","Gopal",
"Kathmandu","Ram","Hari","Gopal",
"Kathmandu","Ram","Hari","Gopal",
"Pushpa","Ram","Hari","Gopal"
};

private int[] roll = {
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,19,19,191,19,2
};


public String[] getNames() {
return names;
}

public String[] getAddress() {
return address;
}

public int[] getRoll() {
return roll;
}




}

 3. Paste the following code into the activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/home"
xmlns:android="http://schemas.android.com/apk/res/android">

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"/>
</LinearLayout>

4. Paste the following code to the MainActivity

public class MainActivity extends AppCompatActivity{
private ListView list;
@override

protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
list = findViewById(R.id.list);
DataModel data = new DataModel();
String[] names = data.getNames();
String[] address = data.getAddress();
int[] roll = data.getRoll();

list.setAdapter(new CustomAdapter(this,names,address,roll));
}
}


You will see the following result.





Reactions

Post a Comment

0 Comments