There are so many apps utilizing ListView in Android. Your feed in a social network, your tasks in a to-do app, even your mail in your favorite email app stored in a ListView.
Basically, ListView is a container for a collection of items that can be represented as an array. You should be able to get any item from a collection by its index and get the size of an underlying collection. Also, it would be nice to be able to add new elements to your collection unless you want it to be constant.
In my opinion, ArrayList fits this definition perfectly. In ArrayAdapter, they use List as an underlying collection, though, but in some implementations of it (LinkedList for example) get operation is linear of index you pass to it, so it can be bad for performance. But it's not a reason to reject ArrayAdapter, just be careful with what implementation you pass to it.
Basically, ListView is a container for a collection of items that can be represented as an array. You should be able to get any item from a collection by its index and get the size of an underlying collection. Also, it would be nice to be able to add new elements to your collection unless you want it to be constant.
In my opinion, ArrayList fits this definition perfectly. In ArrayAdapter, they use List as an underlying collection, though, but in some implementations of it (LinkedList for example) get operation is linear of index you pass to it, so it can be bad for performance. But it's not a reason to reject ArrayAdapter, just be careful with what implementation you pass to it.
Custom List Adapter
ArrayAdapter by default adapts only strings to TextViews, but we want something more interesting.
Let's say we are developing a messaging app and we want to show active conversations. We will represent our chat with this class.
Let's say we are developing a messaging app and we want to show active conversations. We will represent our chat with this class.
public class Chat { private int mProfilePic; private String mName; private String mMessage; Chat(int profilePic, String name, String message) { mProfilePic = profilePic; mName = name; mMessage = message; } public int getProfilePic() { return mProfilePic; } public String getName() { return mName; } public String getMessage() { return mMessage; } }
So we want our ListView to show some information besides strings. How do we do that?
Turns out it's not so complex if you know the basics!
There are several ways to achieve this goal. One of these is to use ArrayAdapter and override getView method (the simplest) and the other is to build your WhateverYouWantAdapter that will extend BaseAdapter class (the funniest).
Let's consider both approaches.
ArrayAdapter
There is a method getView in all adapters that returns the view of your list item.
By default ArrayAdapter just finds TextView passed as a parameter to its constructor and sets the text that is stored in underlying list (by the way if your objects are not Strings it just calls toString and makes them so). You can find this in source code (line 368 see createViewFromResource method).
So to implement custom ListView we just need to override this behavior. But wait a minute you also need some place to put your data in right. Let's build a simple layout that will represent our conversation.
So here we have our friend's profile pic, his name and last message received from him.
In our getView method, we need to get access to them and set values.
After that just make some test data and set your adapter to the list, everything else will be handled by Android.
P.S. You can look at my code on my GitHub repository
By default ArrayAdapter just finds TextView passed as a parameter to its constructor and sets the text that is stored in underlying list (by the way if your objects are not Strings it just calls toString and makes them so). You can find this in source code (line 368 see createViewFromResource method).
So to implement custom ListView we just need to override this behavior. But wait a minute you also need some place to put your data in right. Let's build a simple layout that will represent our conversation.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <!--I'm using hdodenhof's library to make image circle--> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/profile_pic_imageview" android:layout_width="80dp" android:layout_height="80dp" android:scaleType="centerCrop" android:layout_marginEnd="8dp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/black" android:textSize="20sp" android:layout_marginBottom="12dp"/> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp"/> </LinearLayout> </LinearLayout>
Where is Harry? |
So here we have our friend's profile pic, his name and last message received from him.
In our getView method, we need to get access to them and set values.
public class ChatAdapter extends ArrayAdapter<Chat> { public ChatAdapter(Context context, int resource, ArrayList<Chat> data) { super(context, resource, data); } public View getView(int position, View view, ViewGroup parent) { //Get the instance of our chat Chat chat = getItem(position); LayoutInflater inflater = (LayoutInflater) getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE ); //Create new list item View rowView = inflater.inflate(R.layout.list_item, null, true); //Get UI objects ImageView profilePic = (ImageView) rowView.findViewById(R.id.profile_pic_imageview); TextView nameView = (TextView) rowView.findViewById(R.id.name); TextView messageView = (TextView) rowView.findViewById(R.id.message); //Set image profile picture profilePic.setImageDrawable(getContext().getResources().getDrawable(chat.getProfilePic())); //Set text into TextViews nameView.setText(chat.getName()); messageView.setText(chat.getMessage()); return rowView; } }
After that just make some test data and set your adapter to the list, everything else will be handled by Android.
ArrayList<Chat> chats = new ArrayList<>(); chats.add(new Chat(R.drawable.hermione, "Hermione Granger", "Harry! Where are you? " + "Dambledore is looking for...")); chats.add(new Chat(R.drawable.ginger, "Ron Weasley", "Wingardium leviosaaaaaaaaaaaaaaaa")); chats.add(new Chat(R.drawable.snape, "Severus Snape", "Well, it may have escaped your " + "notice, but life is not ...")); chats.add(new Chat(R.drawable.iman, "Tony Stark", "I don't know why I'm even chatting with you")); ChatAdapter adapter = new ChatAdapter(this, R.id.listview_chats, chats); ListView chatList = (ListView) findViewById(R.id.listview_chats); chatList.setAdapter(adapter);
BaseAdapter
This approach is manual but it's also fun because you will build your very own adapter and will understand deeply how adapters work.
The main idea is the same to override some methods in BaseAdapter they are not even implemented, so, we will do it manually. Also in the BaseList, there is no any underlying collection to store data, it means we need to make out own.
Let's use the same layout for list items. For our adapter to work we need to implement getView, getItem, getItemId, getCount methods. But before diving in the code let's consider what these should do.
- getView(int position, View view, ViewGroup parent): for the getView idea is the same as for ArrayAdapter it should return our list item layout.
- getItem(int pos): this method should return an instance of the object we store in list item in pos position.
- getItemId(int pos): you need this when for example when you use a database to retrieve items for your list, it just makes it easier to get item's id in this database. We need to implement to make our adapter work, let's just return 0 from it.
- getCount(): this method just returns number of elements in your list
public class ChatAdapter extends BaseAdapter { private final Activity mContext; private final ArrayList<Chat> mChats; private final Resources mRes; private final LayoutInflater mInflater; public ChatAdapter(Activity context, ArrayList<Chat> chats) { mContext = context; mChats = chats; mRes = mContext.getResources(); mInflater = mContext.getLayoutInflater(); } @Override public int getCount() { return mChats.size(); } @Override public Chat getItem(int pos) { return mChats.get(pos); } @Override public long getItemId(int position) { return 0; } public View getView(int position, View view, ViewGroup parent) { //Get the instance of our chat Chat chat = mChats.get(position); //Create new list item View rowView = mInflater.inflate(R.layout.list_item, null, true); //Get UI objects ImageView profilePic = (ImageView) rowView.findViewById(R.id.profile_pic_imageview); TextView nameView = (TextView) rowView.findViewById(R.id.name); TextView messageView = (TextView) rowView.findViewById(R.id.message); //Set image profile picture profilePic.setImageDrawable(mRes.getDrawable(chat.getProfilePic())); //Set text into TextViews nameView.setText(chat.getName()); messageView.setText(chat.getMessage()); return rowView; } }
And now you can use you newborn adapter just as ArrayAdapter
There is a little bit different constructor so you should change one line of code just write
ChatAdapter adapter = new ChatAdapter(this, chats);
Instead of
ChatAdapter adapter = new ChatAdapter(this, R.id.listview_chats, chats);
Result
Both approaches led me to this view. It's pretty much it. I think we've got nice cosy layout and our users will be satisfied :)
Summary
Of course, it's better to use an ArrayAdapter but I didn't include BaseAdapter part "just because". I think it will deepen your understanding of how does ListAdapter works and will encourage you to learn how things work internally in Android.
Now I'm looking forward to writing more tutorials, so stay tuned. See you next week, peace!
P.S. You can look at my code on my GitHub repository
It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving... very specific nice content.Android Training in chennai |Android Training in Velachery
ReplyDeleteIt's really interesting I'll be read that many of the bloggers relevant android development that's time I read that's your tips helped me and clarify the new thing.We share this tutorial for android ListView Custom method is excellent.I really appreciate to tutorial.
ReplyDeleteexample:
Selenium Training in Chennai | Selenium Testing Training in Chennai | Best Hadoop Training in Chennai | Android Training in Chennai with placement
It's really interesting I'll be read that many of the bloggers relevant android development that's time I read that's your tips helped me and clarify the new thing.We share this tutorial for android ListView Custom method is excellent.I really appreciate to tutorial.
ReplyDeleteexample:
Selenium Training in Chennai | Selenium Testing Training in Chennai | Best Hadoop Training in Chennai | Android Training in Chennai with placement
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
Your very own commitment to getting the message throughout came to be rather powerful and have consistently enabled employees just like me to arrive at their desired goals.
ReplyDeleterpa Training in tambaram
blueprism Training in tambaram
automation anywhere training in tambaram
iot Training in tambaram
rpa training in sholinganallur
blue prism training in sholinganallur
automation anywhere training in sholinganallur
iot training in sholinganallur
Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
ReplyDeleterpa Training in annanagar
blue prism Training in annanagar
automation anywhere Training in annanagar
iot Training in annanagar
rpa Training in marathahalli
blue prism Training in marathahalli
automation anywhere Training in marathahalli
blue prism training in jayanagar
automation anywhere training in jayanagar
This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
ReplyDeleteData Science training in marathahalli
Data Science training in btm
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in kalyan nagar
Data Science training in electronic city
Data Science training in USA
Data science training in pune
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
ReplyDeleteData Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore
After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
ReplyDeletejava training in tambaram | java training in velachery
java training in omr | oracle training in chennai
java training in annanagar | java training in chennai
I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
ReplyDeletepython training in tambaram
python training in annanagar
python training in OMR
python training in chennai
Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
ReplyDeleteBlueprism training in annanagar
Blueprism training in velachery
Blueprism training in marathahalli
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteBest Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies
Selenium Training in Bangalore | Best Selenium Training in Bangalore
Best AWS Training in Marathahalli | AWS Training in Marathahalli
Amazon Web Services Training in Anna Nagar, Chennai |Best AWS Training in Anna Nagar, Chennai
AWS Training in Velachery | Best AWS Course in Velachery,Chennai
Best AWS Training in Chennai | AWS Training Institutes |Chennai,Velachery
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training
https://smithstories2nblog.blogspot.com/2015/05/potty-training-for-graham.html
All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteArticle submission sites
Technology
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteLinux Training in Chennai
Python Training in Chennai
Data Science Training in Chennai
RPA Training in Chennai
Devops Training in Chennai
This comment has been removed by the author.
ReplyDeleteGreat Blog post very useful info thanks for this post ....
ReplyDeletepower bi training chennai |Power bi training class chennai
I'm happy to see the considerable subtle element here!.
ReplyDeleteAsus Mobile Price in Bangladesh
it is very impressive article to show some of the skills.
ReplyDeleteBig Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery
Really very nice blog information for this one and more technical skills are improving, I like that kind of post.keep update lotguys.
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
It's interesting that many of the bloggers to helped clarify a few things for me as well as giving. Most of ideas can be nice content.
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
ReplyDeletepython training in chennai
python online training in chennai
python training in bangalore
python training in hyderabad
python online training
python flask training
python flask online training
python training in coimbatore
Really Thanks For Posting Such a Useful and informative article. oracle training in chennai
ReplyDeleteMake sense, I had not think of it before, well anyways, thanks for sharing mate! keep posted! هکر
ReplyDeleteWorth reading! Our experts also have given detailed inputs about these trainings & courses! Presenting here for your reference. Do checkout
ReplyDeleteoracle sql training in chennai & enjoy learning more about it.
Thank you for sharing nice article
ReplyDeleteKindly check out some Blue Prism Training
Dream your career towards Big Data? Then come to Infycle Technologies, the best software training center in Chennai, which gives the combined and best Big Data Hadoop Training in Chennai, in a 100% hands-on training guided by professional teachers in the field. In addition to this, the interviews for the placement will be guided to the candidates, so that, they can face the interviews without struggles. Apart from all, the candidates will be placed in the top MNC's with a bet salary package. Call 7502633633 and make this happen for your happy life.
ReplyDeleteInfycle Technologies is the best software training institute in Chennai, providing an amazing Python course in Chennai that is 200% realistic and taught by industry experts. Aside from the preparation, mock interviews will be conducted for the students so that they can confidently face the interviews. All of this will result in full placement assurance in top MNC’s Company. For queries just call 7502633633 and have a free demo with us.
ReplyDeletePython training in Chennai
Reach to the <a href='https://infycletechnologies.com/data-science-training-in-chennai">
ReplyDeletebest Data Science Training in Chennai</a>, Infycle Technologies, to enter the IT industry with well-defined skills. Infycle Technologies is the rapidly developing software training cum placement center in Chennai and is generally known for its significance in providing quality hands-on practical training with 200% guaranteed outcomes! Call 7502633633 to book a free demo and to avail the best offers.
If salary is your only income means. Simply think to make it HIGH by getting into AWS training institute in Chennai, Infycle. It will be so easier for you because, past 15 years software industry Infycle leads a 1 place for giving a best students in IT industry.
ReplyDeleteHide ALL IP Crack This encryption standard is right for both authorities and privates associations for high security. If you prefer to change the country, press .Hide All Ip Patch
ReplyDeleteIt has the appearance of a sturdy dinosaur dream synthesizer, and it is called Native Instruments Massive Crack. Through twelve thorough and .Native Instruments Synth
ReplyDelete