I am trying to populate a custom Android ListView from a List. I am using sugar ORM to save and get data from a database. In my Activity I use the following 2 lines to get the data, and put it in a simple listview.
List<Fish> fishList = Select.from(Fish.class).orderBy("species").list();
setListAdapter(new ArrayAdapter<Fish>(this,android.R.layout.simple_list_item_1, fishList));
This works as expected and displays the getString() method from the Fish class.
Every method I have found for populating a custom listview uses ArrayList but fishList is just a List. I have tried probably 4 or 5 tutorials and they all come up short. Most just describe populating a list manually, but not how to do it if I already have the data in a database.
I have the ArrayAdapter classes set up and at one point had it working where it would just load the most recent object into the list. I suspected some kind of loop was needed but couldn't get that working after hours of trying.
Is there a way to populate a custom listview using my List, or do I need to convert it to an ArrayList somehow, and if so, how do I do that for all items in the List?
Can I replace the first line so it gets the data from the database and puts it in an ArrayList right away?
I apologize if this is super simple (I'm sure it is) but I have spent 4 days trying to find a solution before resorting to asking a question here. Thank you so much for any help you can provide.
Found Exactly what I needed here:ListView Populating using Custom Class
In my My onCreate()
adapter = new ListAdapter(this);
listView = getListView();
List<Fish> fishList = Select.from(Fish.class).orderBy("species").list(); //get data from database (SugarORM)
adapter.setData(fishList);
listView.setAdapter(adapter);
Use a BaseAdapter:
http://developer.android.com/reference/android/widget/BaseAdapter.html
Refer to this tutorial:
http://www.vogella.com/tutorials/AndroidListView/article.html
Related
I've figured out how to get the sorted data with a query and pass that to the Recycler view, but it only adds to the RecyclerView instead of replacing the already existing items. Uploaded the code to Pastebin since code irrelevant to the problem is in the class.
MyDatabaseHelper class = https://pastebin.com/fXHw5ccb
CustomAdapter class = https://pastebin.com/4epLqWzJ
MainActivity class = https://pastebin.com/h5mDQunv
Including
customAdapter.notifyDataSetChanged();
recyclerView.setAdapter(customAdapter);
or removing it doesn't solve the problem.
How it looks: https://i.imgur.com/C60ur8b.png
How I want it to look after clicking the menu sort item: https://i.imgur.com/wjZP9yX.png
Solved: Had to pass Custom Adapter to a custom object and read the ArrayList from the object, which let me sort based on SQL queries.
It looks like you call storeFoodDataInArrayList() multiple times which populates the food_ID array, but you never clear that array.
Clear the food_ID array before populating it.
Ive got some gridView and some function which is called when a message arrives from server. In this function I get the values from server and I want to write this value in specific item in the gridView.
The problem is I dont have any idea how can I rewrite text of som Item in the gridView and I couldn find any answer on the internet :/
First you need to to add the received data into arraylist which you used to populate your grid view . You can add the received data into any position of arraylist as you need.
Then update the Arrayadapter using the following command
adapter.notifyDataSetChanged();
I understand up to some point. In my expatiation i think your set by using adapter right. So the simple solution is once you get the new items for a grid. call the setadapter again. Then that will set the new items. Try it once.
Actually i have to comment out but i don't have that much points.
Getting straight to the point, I'm new to this and trying to use this https://github.com/matshofman/Android-RSS-Reader-Library library to parse an rss feed and put all that into a listview. (I don't mind if you give me an alternate solution to this though. I picked this simply because it looked like it would be easier to understand and less work....)
MY problem is that I'm not sure how to do it, and when I read up on it on google it's either darn confusing or Eclipse says there's something wrong with it and gives me an error.
So :: can someone please explain to me, how I should go about doing this. I understand how to put a URL into this feed, and generally how listviews work,
but I'm getting stumped on passing the data that the library extracts from the feed and passing it into my listview. I don't have a clue how to write out java code that tells it to take data that this library passed and say, put it into this string.
I'm also confused about how a listadapter / adapter works. I think I understand how to write it, but all my past attempts have given me errors and I'm not sure what's the problem there.
It would also be nice if someone could explain how the list_layout thing works out. By that, I mean when you create a new xml file and define how one row of the listview looks, but I don't see how it gets linked up with the main xml file with a single listview.
Thanks for helping me out - it's a school project....
(btw, please be simple with the explanations. I think the main problem i'm having is that a lot of the tutorials like using very technical language, and it ends up not getting the point across to me. Even if you give me a chunk of code, and tell me all that parses my url, that's all i need.)
A ListView needs an Adapter to know what the data is and how much of the data there is. The ListView asks the adapter for a view to display a single item using the adapter's method getView(....). In this method you should inflate your view (the item) using the getLayoutInflater().inflate(..). You then get a view for which you can get the specific sub-views by using the findViewById(...) method on that view. Of each sub-view you set the value of a part of your item.
In order to avoid having to inflate a view for each and every item the ListView recycles item views whenever possible, therefore the method getView(...) receives a view, if that view is not null you can use that view instead of inflating a new view.
While reading your data, or when you have read all your data, you need to tell the adapter that the data has changed, which then tells your ListView that it needs to redisplay data. You tell your adapter by calling notifyDataSetChanged().
There is a Google I/O session on the ListView, maybe that might be interesting to watch: http://www.google.com/intl/nl/events/io/2010/sessions/world-of-listview-android.html
In you android-sdk under /samples/android-xx/ there is a sample called "XmlAdapter" which contains a RSS-feed activity. Could be a helpful alternative.
I am trying to reload the list view when the data has changed. But its not working.Here is the code I am using
localPits.clear();//
this.adapter.clear();
//Fetch new data and update the list
this.localPits.addAll(pl);
this.adapter.notifyDataSetChanged();
this.lst_Pits.invalidate();
Each time I call this portion of code existing values are successfully removed. But new values are not loaded in the list view.
How can I do this in android?
Thanks
It's not terribly clear, but I suspect the new values aren't being loaded because you're clearing the adapter without updating it.
Your 2nd line: this.adapter.clear();
You need to update and add the adapter before you call invalidate(). Or, at least, that's as far as I can gather.
this.adapter.clear(); should not be needed. It must reset the links between adapter and localPits.
If you need to keep this, update list adapter localPits.setAdapter(adapter);. It should fix your problem.
I currently use a bunch of subclassed CursorAdapters with custom layouts throughout my app, populating them with a Cursor returned by an SQLite query, then allowing them to populate my ListView, after setting them with
setListAdapter(new SearchAdapter(this, searchCursor));
Is there anyway I can get the same behavior but instead of passing in a Cursor pass in an
ArrayList<String[]>
Would there be a different class to subclass? (Obviously) Or, should I convert the ArrayList to a Cursor somehow? Or, is there a different method I am missing?
Thanks!
you can do it with an array Adapter (also you can subclass Base adapter) and implement your own way of displaying objects in a list. there are plenty of tutorials on google on how to do that.
Just search ArrayAdapter, Base adapter. :D
Hope this helps,
Take care.