Android custom listview with image and text

Описание к видео Android custom listview with image and text

In this tutorial you will learn how to create Android custom listview with image and text. The list will show the country names and country flags, and you can click on any item to move to another screen/page.

Here is how to do it:
- Add a listView to the xml layout file and then initialize that inside the java file

- Initialize a string array variable that will hold the country names and initialize another int array variable that will hold flag images

- Create a new class called MyAdapter.java and make sure it extends ArrayAdapter

- Add the required constructor and make sure to override getCount and getView methods

- Create a new xml layout file and call it "listview_item.xml", this file will represent each row inside the list.

- Add an imageView and a TextView inside "listview_item.xml" file

-Inflate the layout like this:
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_item, parent, false);

- Initialize the views that were created inside "listview_item.xml" inside getView method like this:
ImageView mFlag = (ImageView) convertView.findViewById(R.id.imageView);
TextView mName = (TextView) convertView.findViewById(R.id.textView);

- Don't forget to set it's appropriate values through the use of the position that is provided along with getView method like this:
mFlag.setImageResource(flags[position]);
mViewHolder.mName.setText(names[position]);

- Open MainActivity.java and initialize MyAdapter class like this:
MyAdapter myAdapter = new MyAdapter(MainActivity.this, countryNames, countryFlags);

- Set adapter to the listview like this:
mListView.setAdapter(myAdapter);

- Android listview tutorial:    • Android Listview Onitemclick Example  

- Android ViewHolder documentation: https://developer.android.com/trainin...

- Project source code: https://github.com/codingdemos/Custom...

Social media:
▶Twitter:   / codingdemos  

Комментарии

Информация по комментариям в разработке