I want a custom list view with a custom array adapter.I found this blog post but customadapter.java is unclear. I can do the about with default simple_list_1.
Here is the blog post http://sogacity.com/how-to-make-a-custom-arrayadapter-for-listview/
How is constructor getting called.How is static class called.Totally,I'm not able to under stand the flow of the program.
Thank you.
getview() method will be called automatocally for every list you will add.
Take a look here for more explanation on array adapter.
Hope it will help.
Related
I'm starting learning Android and I want to know if there is some option in Android that let you modify each item or view (I don't know how it's named exactly, I mean each of the items from an ArrayList that you show in a ListView).
Well, I made a ListView that is going to show some books that were located in an ArrayList named "books".
I made a custom adapter that I associate to the ListView to show each item with the corresponding layout in the application. I also have a class "book" for each item that is going to be shown in the ListView.
Further, I made an Intent that I call from MainActivity with startForActivityResult(), that I process in Book class and that I return to MainActivity with all data of a book with the method setResult and got the information with onActivityResult() and the requestCode.
So I don't have any problem to add items to the ListView, just I have the problems if I want to modify some of the items (or views) that are located in the ListView (for example if I have title and author of a book, if I put some wrong information, I want have the option to change it).
I have that, in the same moment that you click on some of the items of the ListView, a new layout will be show to modify the information that it's wrong so I use the method setOnItemClickListener with onItemClick event on the custom adapter that I created before. Here it's where I call the new Intent to modify the wrong information with the method startActivityForResult().
I made the same as before to add a new item but, instead of add a new item with custom_adapter.addBook(title,author) I want to know if there is some option to made something like this: custom_adapter.modify(title,author) or custom_adapter.update(title,author), I mean, when you have modify all the items that were wrong of a book (for example an EditText that were "title") and you have all the information in the MainActivity class (because you returned it with setResult), how to put it again in the same item updating it in the custom_adapter and also in the ArrayList.
I searched it on the Internet but I didn't find anything.
I'm sorry if I have a poor English, but I expect that it can be understand.
Thank you very much!
If I am understanding your problem correctly - you could simply modify the ArrayList of type Book that is backing your ArrayAdapter.
So if you know what Book object you want to modify then you can simply make your changes to the Book object itself. As long as this Book is a reference to the same object that you originally added to the ArrayList you instantiated your ArrayAdapter with then you can then call custom_adapter.notifyDataSetChanged() to tell the adapter to redraw its childviews with the new data.
There are some good code samples on the Internet but you have to understand the code for your purposes. So...here is a start, look at Using an ArrayAdapter with ListView. The code shows the use of ArrayAdapter with getView() method. And I hope it shows how to define the listeners, which you need.
How about that for a start? Have fun...
Thank you very much for all help you gave to me. I'm very pleased with you. :)
Finally, I just send the info with a Bundle when I started the Intent, also with the position. And after, I just used this position to set the new info to my items (in my case, books).
Again, thank you very much ;)
I am wanting to change my listView from the normal ArrayAdapter (simple_list_item_1) to something more like this:
Name..... Score(center right).
Date (under name)
So There's 3 Views...
name
date
score
I've looked up how to make custom adapters and layouts but they are all very confusing. I just want a simple fix that I can add to an existing project.
Here's my code for my list right now:
//update listView
listAdapter = new ArrayAdapter<String>(GradesActivity.this, android.R.layout.simple_list_item_1, names);
mListView.setAdapter(listAdapter);
Help is appreciated!
I just had the SAME problem last week. My solution was a tutorial on the internet: http://androidtuts4u.blogspot.com.br/2013/02/android-list-view-using-custom-adapter.html
Just copying and pasting the code onto a temporary project (it will take less than 15 minutes) will help you understand how simple it is and also make it very clear so you can implement it into your original project. I hope it helps!
Peace!
ArrayAdapter can't auto binding three view
You can try SimpleAdapter or extend BaseAdapter implements it youself
This is Google's training
https://developer.android.com/training/material/lists-cards.html
https://developer.android.com/training/wearables/ui/lists.html
And here is a sample of SimpleAdapter http://www.java2s.com/Code/Android/UI/UsingSimpleAdaptertofilldatatoListView.htm
There are many fixes you need to do
1. Create a class that extends BaseAdapter.
Override all the methods create a constructor of that class that will initialize listitems
You are just passing names
listView listAdapter = new ArrayAdapter(GradesActivity.this, android.R.layout.simple_list_item_1, names);
Instead of just names create a class that contains three elements you need I.e name date and score and make there get and set methods
And the object of the class will be saved in a list.
4. In getview method you have to set these variables
I am looking at this ListView Tutorial:
ListView Tutorial
and I was wondering how much better is to create my own ArrayAdapter, rather than just using and ArrayAdapter.
In the Tutorial it defines a "StableArrayAdapter", what exactly does this means? If I use a regular ArrayAdapter, could it be dangerous for some reason?
The two previous answers are absolutely right, but just to address more directly your question and in case someone else has the same doubt than you; a regular ArrayAdapter is not dangerous at all, the only "problem" is that it might not fulfill your needs, in which case you will have to create your own adapter, as the author of the tutorial did by creating what he called StableArrayAdapter in the end of the ListViewExampleActivity class.
Don't get lost by the name, which I guess comes from the fact that the overwritten method "hasStableIds" always returns true, it doesn't mean that the regular ArrayAdapter creates problems.
ArrayAdapter: It is merely a way to provide data to a ListView. It is also a BaseAdapter that is backed by an array of objects.
CustomAdapter: If if your ListView is a normal and simple ListView (wherein you are having one TextView per item in the list), then the use of ArrayAdapter would be apt.
But it is recommended you to create your own CustomAdapter which extends an ArrayAdapter that you can use for providing data to your ListView. This way you can easily extend your ListView to include more that one TextView or even ImageView (to show images).
CursorAdapter: Cursor Adapter is used when you have Data in a Cursor (typically when you are retrieving data from a database. The Cursor must include a column named "_id" or this class will not work.
If you are using a simple ListView, like merely a TextView per item, then just use the standard ArrayAdapter, on the other hand, if you want a custom item in the list, as in a combinations of views within each item in the ListView, then extend the ArrayAdapter and implement it to your needs.
StableArrayAdapter is merely an extended version of ArrayAdapter, but in StableArrayAdapter they have overridden the method hasStableIds() of BaseAdapter to return true.
You can check this in the following links:
StableArrayAdapter -
Override hasStableIds to return true
ArrayAdapter -
Has not Override hasStableIds but extended BaseAdapter
BaseAdapter -
Has hasStableIds but returning false
Now Question is What is the use of StableIds
This Indicates whether the item ids are stable across changes to the underlying data. If True then same id always refers to the same object. for more info
In my application i have customListView ,i need to get the first value of list view(i.e. Zero position value) when very first time activity is loaded without using OnItemClickLiseners and it is focusable.How Can i do?,please can any one help me.
Thanking in Advance.
You can either use
listView.getChildAt(0)
or
listView.getChildAt(listView.getFirstVisiblePosition());
This is based on the assumption that you need to pull out values of the item that is at the first (0th) position in the ListView.
Since you are using a Custom ListView, you should also have a custom Adapter (possibly an ArrayAdapter or a BaseAdapter) that you use to show the data etc.
In any case, just check for this in the getView():
if (position == 0) {
// GET WHAT EVER DATA YOU NEED OUT OF THE ITEM.
}
If you can add more info to the OP about how you deal with the data, perhaps this answer can be made a little bit more useful. For example, do you use a POJO class to bind the data to the ListView?
I'm having a class (ExpandableListDataClass) which extends BaseExpandableListAdapter
I am calling to ExpandableListDataClass from an Activity like below;
ExpandableListDataClass expandableListDataClass = new ExpandableListDataClass(this, categories);
categoryExpandableListView.setAdapter(expandableListDataClass);
categories is a String array. That means groupCount of the ExpandableListDataClass is depend on the categories.
The worst thing is, I am loading a GridView for expanding of a group item in ExpandableListView. Those GridView have different data to show according to the category.
If I use following code inside getChildView(), it is perfectly working
if(getGroup(groupPosition).toString().equals("ebooks")){
expandableListInsideGridView.setAdapter(eBooksImageAdapterForExpandableList);
}else if(getGroup(groupPosition).toString().equals("ebrochures")){
expandableListInsideGridView.setAdapter(eBrochuresImageAdapterForExpandableList);
}
But what I want to remove from that is eBooksImageAdapterForExpandableList and eBrochuresImageAdapterForExpandableList. Because now what I am doing is creating ArrayList according to the existing categories.
But when I don't know how many categories/groupCount there are, I can't do that.
Please give me a solution
Finally I was able to find a solution :-)
I will explain it in backwards.
In the getChildView() I have to give following line of code
expandableListInsideGridView.setAdapter(new ImageAdapterForExpandableList(expandableListDataClassContext, eItemThumbCachePathArrayListContainer.get(groupPosition), eItemNamesArrayListContainer.get(groupPosition), eItemUrlArrayListContainer.get(groupPosition)));
getChildView() is in the ExpandableListDataClass which extends the BaseExpandableListAdapter.
What I had to do is declare an ArrayList which contains some other ArraLists of same type. If I explain it more with my code eItemThumbCachePathArrayListContainer is an ArrayList which contains ArrayLists of type String that I want to pass. That is the simple logic I have used and it works for me.