Listview: Arraylist
Listview: Arraylist
xml
<ListView
android:id = “@+id/listView”
android:width = “match_parent”
MainActivity.java
ListView listView;
ArrayList<String> arrayList;
ArrayAdapter arrayAdapter;
@Override
setContentView(R.layout.activity_main); Android
//step 1 iOS
arrayList.Add(“Android”);
arrayList.Add(“iOS”);
arrayList.Add(“Microsoft”);
arrayList.Add(“RIM”);
//step 3
//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”
<GridView
android:id = “@+id/listView”
android:width = “match_parent”
android:height = “match_parent”
android:numColumns=”???dp”/>
layout_item.xml (res/layout)
android:width = “match_parent”
android:height = “wrap_content”
<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
// Atl+Insert
int image;
String name
this.image = image;
this.name = name;
}
StudentAdapter.java
//Alt Enter
Context context;
int layout;
ArrayList<Student> arrayList;
this.context = context;
this.layout = resource;
this.arrayList = arrayList;
@Override
converView = LayoutInflater.from(context).inflate(layout,parent,false);
textView.setText(student.getName());
imageView.setImageResource(student.getImage())
return convertView;
}
class MainActivity extends AppCompactActivity{
ListView listView;
ArrayList<Student> arrayList;
StudentAdapter arrayAdapter;
@Override
setContentView(R.layout.activity_main);
//step 1
listView = findViewById(R.id.listView);
//step 2
// hinh1.jpg getView()
arrayList = new ArrayList<>();
//step 3
//step 4
ListView
listView.setAdapter(arrayAdapter);
btnThem.setOnClickListener(new View.OnClicklListener(){
@Override
Student s =
arrayList.add(s);
arrayAdapter.notifydatasetchanged();
Them
}
});