0% found this document useful (0 votes)
58 views7 pages

Listview: Arraylist

The document describes how to create a custom ListView in Android. It includes: 1. Defining the layout files activity_main.xml and layout_item.xml for the ListView and its items. 2. Creating a Student class to store item data like image and name. 3. Creating a StudentAdapter class that extends ArrayAdapter to populate the ListView from an ArrayList of Student objects. 4. Implementing the adapter and populating the ListView in the MainActivity class.

Uploaded by

Tom Vu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views7 pages

Listview: Arraylist

The document describes how to create a custom ListView in Android. It includes: 1. Defining the layout files activity_main.xml and layout_item.xml for the ListView and its items. 2. Creating a Student class to store item data like image and name. 3. Creating a StudentAdapter class that extends ArrayAdapter to populate the ListView from an ArrayList of Student objects. 4. Implementing the adapter and populating the ListView in the MainActivity class.

Uploaded by

Tom Vu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Activity_main.

xml

<ListView

android:id = “@+id/listView”

android:width = “match_parent”

android:height = “match_parent” />

MainActivity.java

class MainActivity extends AppCompactActivity{

ListView listView;

ArrayList<String> arrayList;

ArrayAdapter arrayAdapter;

@Override

protected void onCreate(Bundle saveInstanceState){


ListView
super.onCreate(saveInstanceState);

setContentView(R.layout.activity_main); Android

//step 1 iOS

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


//step 2

arrayList = new ArrayList<>();

arrayList.Add(“Android”);

arrayList.Add(“iOS”);

arrayList.Add(“Microsoft”);

arrayList.Add(“RIM”);

//step 3

arrayAdapter = new ArrayAdapter(MainActivity.this,android.R.lay.simple_list_item_1,arrayList);

//step 4

listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener(int pos)

listView.setOnItemLongClickListener(int pos)
}

activity_main.xml (res/layout)

<ListView

android:id = “@+id/listView”

android:width = “match_parent”

android:height = “match_parent” />

<GridView

android:id = “@+id/listView”

android:width = “match_parent”

android:height = “match_parent”

android:horizontalSpacing = “???dp” (2-4dp)

android:verticalSpacing = “???dp” (2-4dp)

android:columnWidth=”???dp” (120 – 160dp)

android:numColumns=”???dp”/>

layout_item.xml (res/layout)

Right Click res  new  Layout resource file

<LinearLayout (//RelativeLayout // FrameLayout)

android:width = “match_parent”

android:height = “wrap_content”

android: orientation =”horizontal”>

<ImageView

android:id = “@+id/imageView”

android:width = “100dp”
android:height = “100dp”

android:src=”@color/primaryDark”/>

<TextView

android:id = “@+id/textView”

android:width = “match_parent”

android:height = “100dp”

android:text = “Name”

android:gravity=”center|left” />

</LinearLayout>
Student.java

Right Click java  com.example.StudentList  class

// Atl+Insert

public class Student {

int image;

String name

public Student (int image, String name){

this.image = image;

this.name = name;

}
StudentAdapter.java

Right Click java  com.example.StudentList  class

//Alt Enter

public StudentAdapter extends ArrayAdapter<Student>{

Context context;

int layout;

ArrayList<Student> arrayList;

public StudentAdapter(Context context,int resource, ArrayList<Student> arrayList){

this.context = context;

this.layout = resource;

this.arrayList = arrayList;

@Override

public View getView(int position, View convertView, ViewGroup parent){

Student student = arrayList.get(position);

converView = LayoutInflater.from(context).inflate(layout,parent,false);

TextView textView = convertView.findViewById(R.id.textView)

ImageView imageView = convertView.findViewById(R.id. imageView)

textView.setText(student.getName());

imageView.setImageResource(student.getImage())

return convertView;

}
class MainActivity extends AppCompactActivity{

ListView listView;

ArrayList<Student> arrayList;

StudentAdapter arrayAdapter;

@Override

protected void onCreate(Bundle saveInstanceState){


ListView
super.onCreate(saveInstanceState);

setContentView(R.layout.activity_main);

//step 1

listView = findViewById(R.id.listView);

//step 2

// hinh1.jpg getView()
arrayList = new ArrayList<>();

arrayList.Add(new Student(R.drawable.hinh1, “Tom”));

arrayList.Add(new Student(R.drawable.hinh2, “John”));


Them
arrayList.Add(new Student(R.drawable.hinh2, “John”));

//step 3

arrayAdapter = new StudentAdapter(MainActivity.this,R.layout.layout_item,arrayList);

//step 4
ListView
listView.setAdapter(arrayAdapter);

btnThem.setOnClickListener(new View.OnClicklListener(){

@Override

public void onClick(View v){

Student s =

new Student(R.drawable.hinh2, “John”);

arrayList.add(s);

arrayAdapter.notifydatasetchanged();
Them
}

});

You might also like