make-money-468x60

Custom Listview on Android Example

Custom Listview on Android Example

I'll introduce you to perform custom listview in android with the following sequence of steps, you should comply with such order.

Steps 1 :  Create project new with demo CustomListview.
    1. in Android studio create new project by navigating: File -> new-> New Project -> import name project and select forder save project.
    2. Import libraries need to my project.
    3. I am creating required packages first just to keep the project organised. This step is optional but it is recommended. Create four packages named adapter, app, model and util. So after creating packages my project contains following.
   4. Create file Color.xml on foder value.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="color_red">#FF0000</color>
    <color name="color_white">#FFFFFF</color>
    <color name="color_blue">#444444</color>
    <color name="color_black">#000000</color>
    <color name="color_green">#00FF00</color>
</resources>


5. Create File Dimen.xml  on forder Values.
<resources>
    <!-- Default screen margins, per the Android Design guidelines. --> 
 <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

6. Create file String.xml on folder values.
<resources>
    <string name="app_name">DemoExample</string>
</resources>

7. Create class Student on object .
public class Student {
    private String image;
    private String name;

    public Student(String image, String name) {
        this.image = image;
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public String getName() {
        return name;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public void setName(String name) {
        this.name = name;
    }
}
8. Create Layout custom listview  by xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="horizontal"    android:layout_height="wrap_content">
    <ImageView        android:id="@+id/imgAvatar"        android:layout_width="0dp"        android:layout_weight="1"        android:src="@drawable/ic_rankings"        android:contentDescription="@null"        android:layout_height="100dp" />
    <TextView        android:id="@+id/tvName"        android:layout_gravity="center"        android:gravity="center"        android:text="@string/app_name"        android:layout_width="0dp"        android:layout_weight="2"        android:layout_height="wrap_content" />
</LinearLayout>
9.  Create class Custom Adapter listview
public class CustomStudent extends BaseAdapter {
    private Context mContext;
    private List<Student> mStudent = new ArrayList<>();
    public CustomStudent(Context context,List<Student> students){
        super();
        this.mContext = context;
        this.mStudent = students;
    }

    @Override    public int getCount() {
        return mStudent.size();
    }

    @Override    public Object getItem(int postion) {
        return mStudent.get(postion);
    }

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

    @Override    public View getView(int postion, View convertView, ViewGroup viewGroup) {
        View view = convertView;
        ViewHolder mViewHolder;
        if(view == null){
            LayoutInflater mInflater = (LayoutInflater) mContext                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view = mInflater.inflate(R.layout.item_student_custom, null);
            mViewHolder = new ViewHolder();
            mViewHolder.mImgaVatar = (ImageView) view.findViewById(R.id.imgAvatar);
            mViewHolder.mTvNameStudent = (TextView) view.findViewById(R.id.tvName);
            view.setTag(mViewHolder);
        }else{
            mViewHolder = (ViewHolder)convertView.getTag();
        }
        Student student = mStudent.get(postion);
       // mViewHolder.mImgaVatar.setBackground(mContext.getDrawable(R.drawable.ic_rankings));        mViewHolder.mTvNameStudent.setText(student.getName());
        return view;
    }
    class ViewHolder{
        private ImageView mImgaVatar;
        private TextView mTvNameStudent;
    }
}
 10 . Create class mainActivity this listview on android
public class MainListview extends AppCompatActivity {
    private ListView mLvStudent;
    private List<Student> mStudents = new ArrayList<>();
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setContentView(R.layout.main_listview);
        init();
        addData();
        updateData();
    }
    private void init(){
        mLvStudent = (ListView) findViewById(R.id.lvStudent);
    }
    private  void addData(){
        Student student;
        student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
         student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
         student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
         student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
        student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
        student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
        student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
        student = new Student("anhh","Tran Tien Anh");
        mStudents.add(student);
        Log.d("anhtt", " mstudent : " + mStudents.size());
    }
    private  void updateData(){
        CustomStudent mAdapter = new CustomStudent(this,mStudents);
        mLvStudent.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
        Log.d("anhtt" ," mstudent : " +mStudents.size());
    }

}