[:en]Android: ListView with custom adapter[:]

[:en]MenuList

Step 1: activity_main.xml



    



Step 2: MainActivity.java

package com.example.cambridge.listviewdemo;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        ListView listView = (ListView)findViewById(R.id.listView);

        ArrayList subjects = new ArrayList();
        SchoolSubject math = new SchoolSubject("Scanner", false, R.drawable.p1);
        subjects.add(math);

        SchoolSubject science = new SchoolSubject("Chart", true, R.drawable.p2);
        subjects.add(science);

        SchoolSubject history = new SchoolSubject("Student", true, R.drawable.p3);
        subjects.add(history);


        SchoolSubject reading = new SchoolSubject("Calendar", false, R.drawable.p4);
        subjects.add(reading);

        //substantiate
        SubjectAdapter adapter = new SubjectAdapter(getApplicationContext(), R.layout.row_view, subjects);
        listView.setAdapter(adapter);
    }

    public class SchoolSubject {
        public  String name;
        public boolean isHomeworkDone;
        public int imageId;

        public  SchoolSubject(String name, boolean isHomeworkDone, int imageId){
            this.name = name;
            this.isHomeworkDone = isHomeworkDone;
            this.imageId = imageId;
        }
    }

    public class SubjectAdapter extends ArrayAdapter {
        Context context;
        int resource;
        ArrayList subjects = null;

        public SubjectAdapter(Context context, int resource, ArrayList subjects) {
            super(context, resource, subjects);
            this.context = context;
            this.resource = resource;
            this.subjects = subjects;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            SchoolSubject subject = subjects.get(position);
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.row_view,parent,false);
            }
            TextView subjectNameTextView = (TextView)convertView.findViewById(R.id.subjectNameTextField);
            CheckBox homeworkDoneCheckBox = (CheckBox)convertView.findViewById(R.id.homeworkDoneCheckBox);
            ImageView imageView = (ImageView)convertView.findViewById(R.id.imageView);

            subjectNameTextView.setText(subject.name);
            homeworkDoneCheckBox.setChecked(subject.isHomeworkDone);
            imageView.setImageResource(subject.imageId);

            return convertView;

        }
    }
}


Step 3: row_view.xml





    

    

    


[:]

Print Friendly, PDF & Email
Scroll to Top