step - 1 create a listview in activity.xml.
create rootview as linerlayout
step - 2 create a customview for listview.
here we are doing this because we have created a listview but what will be in that list view is decided by the custom view
step - 3 create a custom class for custom layout.
step - 4 create custom array adapter.
public
class
NumbersViewAdapter
extends
ArrayAdapter<NumbersView> {
public
NumbersViewAdapter(
@NonNull
Context context, ArrayList<NumbersView> arrayList) {
super
(context,
0
, arrayList);
}
@NonNull
@Override
public
View getView(
int
position,
@Nullable
View convertView,
@NonNull
ViewGroup parent) {
View currentItemView = convertView;
if
(currentItemView ==
null
) {
currentItemView = LayoutInflater.from(getContext()).inflate(R.layout.custom_list_view, parent,
false
);
}
NumbersView currentNumberPosition = getItem(position);
ImageView numbersImage = currentItemView.findViewById(R.id.imageView);
assert
currentNumberPosition !=
null
;
numbersImage.setImageResource(currentNumberPosition.getNumbersImageId());
TextView textView1 = currentItemView.findViewById(R.id.textView1);
textView1.setText(currentNumberPosition.getNumberInDigit());
TextView textView2 = currentItemView.findViewById(R.id.textView2);
textView2.setText(currentNumberPosition.getNumbersInText());
return
currentItemView;
}
}
step -5 now we create declare or initiate the listview in the mainactivity.
// Now create the instance of the NumebrsViewAdapter and pass
NumbersViewAdapter numbersArrayAdapter =
new
NumbersViewAdapter(
this
, arrayList);
ListView numbersListView = findViewById(R.id.listView);
numbersListView.setAdapter(numbersArrayAdapter);
}
}
Comments
Post a Comment