I managed to retrive data from the database and store it in ArrayList:
ArrayList<Category> selectCategory = select.all().from(Category.class).execute();
for (Category category: selectCategory) {
builder.append(category.name).append("\n");
}
Now I want to add that data to child element of ExpandableListView. The following code is hardcoded, and I don't want it that way. I want it to get data from database. How do I do that?
List<String> class1= new ArrayList<String>();
class1.add("English");
class1.add("Maths");
class1.add("Geo");
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
I used this tutorial to create my own ExpandableListAdapter.
Basically you need a list of all parent items and an additional Hashmap with the parent as key and the child as List<>.
List<Parent> parentList = new ArrayList<>();
HashMap<Parent, List<Child>> childHashMap = new HashMap<Parent, List<Child>>
Related
I am trying to put one piece of value in an ArrayList to my listView, below are the details.
Here is the sample of the ArrayList info:
private static final ArrayList<User> users = new ArrayList<User>(){{
add(new User("Nevin Hobden",38,"Male","Friend","New Mexico","NM"));
add(new User("Stillman Macken",32,"Male","Friend","Arizona","AZ"));
add(new User("Stevy Ranscomb",36,"Male","Friend","Arizona","AZ"));
add(new User("Lynelle Garstang",22,"Female","Family","California","NE"));
I want to grab the state data out from users ArrayList, eg. I only wish to get "New Mexico", "Arizona", "Arizona", and "California" data out to show it on my listView
if that is possible I also want to remove the duplicate and sort in ascending order
Arizona
California
New Mexico
Below is the code I had
ListView stateListView;
ArrayList<DataServices.User> stateList = DataServices.getAllUsers();
ArrayAdapter<DataServices> arrayListAdapter;
stateListView = view.findViewById(R.id.stateListView);
arrayListAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,android.R.id.text1, stateList);
stateListView.setAdapter(arrayListAdapter);
I know I should not use stateList in the arrayAdapter however I have no idea how should I change for that part, any help is appreciated. Thanks!
If you want to get a list of the states, you can just loop over the original list and put the state into an ArrayList<String>, like this
ArrayList<String> states = new ArrayList<>();
for(DataServices.User user : users) {
states.add(user.state);
}
If you only want unique states, you could just use a HashSet there instead. Sets eliminate duplicates.
HashSet<String> states = new HashSet<>();
for(User user : users) {
states.add(user.state);
}
// then convert it back to a list
ArrayList<String> unique_states = new ArrayList<>(states);
And if you want it to be sorted, you can use Collections.sort to sort the list
Collections.sort(unique_states);
If your version of Java supports it, you can also do this in fewer lines with streams, but it accomplishes the same thing
List<String> statesList = users.stream()
.map(u -> u.state)
.collect(Collectors.toList());
Set<String> statesSet = users.stream()
.map(u -> u.state)
.collect(Collectors.toSet());
// Or in one-line
List<String> unique_states = users.stream()
.map(u -> u.state)
.collect(Collectors.toSet())
.stream()
.sorted()
.collect(Collectors.toList());
Please don't use ListView. ListView is outdated and not so efficient as RecyclerView. Here is a good doc how to use RecyclerView
I am using JPA and I have an entity/class named Order. I have a rest GET endpoint to fetch an order by an id. It works perfectly fine. The order entity looks like below:
#Entity
public class Order {
#Id
private Long id;
#Column
private List<String> transactionRefs;
}
Now, in one particular scenario, I need to fetch the order from the database and add another item to the transactionRefs and save it. So I do as below:
Order order = orderRepository.findById(1).get();
List<String> transactionList = order.getTransactionRefs();
transactionList.add("transaction-ref");
I get the below error when I do that:
java.lang.UnsupportedOperationException: null\n\tat java.util.AbstractList.add(AbstractList.java:148)
If I do as below, that fixes the problem:
Order order = orderRepository.findById(1).get();
List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);
transactionList.add("transaction-ref");
So, I need to know if I am in the right direction here and is this an expected error scenario.
Update:
Whenever we are adding an item to the list, we have the below condition :
if (transactionRefs == null) {
transactionRefs = new ArrayList<>();
}
So, whenever the transactionref is saved for the first time, we cast it to a ArrayList.
Update 2 :
Below is the getter for the transactionRef:
public List<String> getTransactionRefs(){
if (this.transactionRefs != null) {
return Arrays.asList(this.transactionRefs.split(","));
}
return null;
}
This is the cause of your exception
return Arrays.asList(this.transactionRefs.split(","));
Arrays.asList returns a collection backed by the array and it can't be modified with add or addAll. You need to create the List just like you are doing in the question:
List<String> transactionList = order.getTransactionRefs();
transactionList = new ArrayList<>(transactionList);
For more examples:
How to add elements in List when used Arrays.asList()
Regarding immutable List (created by Arrays.asList())
I have a database setup which looks like this.
I want to overwrite the array students with my new array.
I tried doing it by the set method but I keep getting an error " Invalid data. Unsupported type: Models.students"
Here's my code.
private void update(){
DocumentReference document = db.collection("Records").document(rid);
Map students = new HashMap();
students.put("students", studentsList);
document.set(students);
}
"studentslist" is an arraylist of students (List< students > studentslist)
Update
I solved the issue by implementing what Doug Stevenson suggested
Here's my updated method.
private void update(){
DocumentReference document = db.collection("Records").document(rid);
List<Map> mapList = new ArrayList<>();
for(int i=0; i< studentsList.size(); i++){
Map student = new HashMap();
student.put("name", studentsList.get(i).getName());
student.put("id", studentsList.get(i).getId());
student.put("attended", studentsList.get(i).isAttended());
mapList.add(student);
}
document.update("students", mapList);
}
The Firestore SDK doesn't support writing a field that's an array of POJO style objects. That's the reason for the error.
Your alternative is to instead write an ArrayList of HashMap objects that each contains the fields and values contained in the POJO.
so I'm stuck on something probably pretty basic. I've got data stored in an SQLite database table and want to display that in an expandable list view. The data basically forms as headings and subheadings. There's many sub-headings to a single heading.
Retrieving the data is pretty straightforward. The part I'm stuck at is taking that data and giving it to the expandable list view.
I want the expandable list view to display the headings as the item that can be expanded, with the subheadings as the child items. How can I do this?
Thanks in advance.
You must provide an adapter to ExpandableListView. There is a base class adapter that you can extend it for your needs.
For creating the data lists. Assume Column1 is Heading, Column2 is SubHeading, try this in activity/fragment:
ArrayList<String> headings = new ArrayList<>();
HashMap<String, ArrayList<String>> subheadings = new HashMap<>();
String heading, subheading;
do{
heading = cursor.getString(1);
subheading = cursor.getString(2);
ArrayList<String> tmpChild;
if(headings.contains(heading)){
tmpChild = subheadings.get(heading);
}else{
headings.add(heading);
tmpChild = new ArrayList<>();
}
tmpChild.add(subheading);
subheadings.put(heading, tmpChild);
}while (cursor.moveToNext());
For the adapter, try my answer here: Tree with checkBox
Hope that helps!
So I am working on a news application which has a Arraylist TopNewsList with all the news from all categories. Now I have a category specific list of news, so I want to use the same arraylist in the adapter for different Listviews, I want the adapter to filter the news based on category id I provide (I have it in the arraylist), the Arraylist is actually - ArrayList<HashMap<String, String>> with id, news title, imageurl etc etc Note that I cannot use a copy , because there are fields like read/unread which should reflect across categories.
This is my ArrayList FYI
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_CATEGORYID, id);
map.put(TAG_TITLE, title);
map.put(TAG_DESCRIPTON, description);
map.put(TAG_CREATED, created);
map.put(TAG_IMAGE, image);
map.put(TAG_IMAGE2, image2);
// adding HashList to ArrayList
TopNewsList.add(map);
From design point of view you should make class/type News with this fields.
So you will get
class News
{
String id;
String title;
// and so on
}
Then you will have ArrayList<News> allNews list of newses.
If you want to filter and get only some newses from the list just make another list
List<News> newsOfSportsCategory = ArrayList<News>();
for(int i=0;i< allNews.size();i++)
{
if(allnews.get(i).id.equals("idOfSportsCat")
{
newsOfSportsCategory.add(allnews.get(i));
}
}
You will get list with sport categories, and this is not copy because you are only holding references to objects that are also on allNews list.