This question already has answers here:
Populating a ListView using an ArrayList?
(6 answers)
Closed 9 years ago.
I'm programming an Android application and one feauture is a ListView generated and populated by a MySQL Database.
This is the outputted ArrayList:
DatabaseHandler db = new DatabaseHandler(this);
List<Suspect> list = db.getAllContacts();
Do I use a cursor to input it in my ListView?
Any code would be appreciated..
I recommend you to use CursorAdapter in case you don't need any data manipulations after the query.
If you want to use List so try ArrayAdapter.
If you need to modify the data after it is captured from the database and have the view updated I'd suggest you create an arrayList of , and then you can use a custom Array Adapter like shown here. If you then change the underlying data, you can use adapter.notifyDataSetChanged()
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.
This question already has answers here:
RecyclerView stackFromBottom
(2 answers)
Closed 6 years ago.
How can I set stackFromBottom(boolean) from ListView in RecyclerView? I need the first element in the list to be last element in the view.
For example, I have a list with 10 elements. On top of the view, I have an element with index 9, and at the bottom of the view I have an element with index 0. After I call notifyDataSetChanged() new elements are added to the bottom of the view.
In ListView, I use the setStackFromBottom method. Does RecyclerView have alternatives for this?
setReverseLayout(Boolean) method from RecyclerView.LinearLayoutManager - it's my choice.
setStackFromEnd(boolean) method was added for support with ListView
I have never used it, but I think this could be a method you are looking for: setStackFromEnd(boolean) from RecyclerView.LinearLayoutManager.
I'm using the navigation drawer example in Android. What's the easiest way to change the string items in the adapter dynamically? Do I just create a new adapter and set it with my new values?
The problem is I'm altering the string items based on user state logged in and logged out. How do I access it from a non static context and just say update list adapter? It doesn't seem to be re-drawing itself and running my adapter code which is dynamic based on user state, so I guess it runs/inits once and I have to create and load a new adapter if I want to change it later?
Thanks.
You haven't given us much to go on (a little code would be nice), but you can update the data in the ListView by calling notifyDatasetChanged() on the Adapter. If you are not using a custom Adapter e.g. an ArrayAdapter then you can just create a new Adapter instance with different String values and set it to the ListView.
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
I am new to Java and Android and come from PHP background. I am trying to display a list view based on arbitrary data received from a previous activity. This the code that I am using:
String[] chapter_array = getResources().getStringArray(R.array.view_chapter);
setListAdapter(new ArrayAdapter<String>(this, R.layout.view_surah, chapter_array));
How do I change the "view_chapter" in R.array.view_chapter to make it populate the list from the data received from the last activity.
You probably want to write a custom ListAdapter that pulls the data out of the Intent passed to the new activity (i.e. getIntent().getExtras()) and feed that data into your custom ListAdapter.
The Android developer docs have a good sample on how to write a ListAdapter.
Similar to Erich's answer you could simply have the previous activity pass a string in CSV form and do String[] chapter_array = getIntent().getExtras().split(','); or something similar to that