I have a recyclerview which displays data from an SQLite database.
I have a list of icons that I save in an ArrayList and apply color to them inside the ArrayList too.
Example: mMoodIcons.add(new MoodIcons(R.drawable.excited_icon, R.mipmap.background_clouds_excited, ContextCompat.getColor(mContext, R.color.excited), mContext.getString(R.string.excited)));
In the recycerview I have this code:
if(currentLog.getMoodBefore() == 3){
holder.moodIcon.setImageResource(R.drawable.confident_icon);
holder.moodIcon.setColorFilter(ContextCompat.getColor(mContext, R.color.confident));
}
I need to make it dynamic so, for example, each mood has a number 1 = happy, 2 = fine, etc
So when the recycler view is displaying the information from the log it can display the correct mood icon with the color applied.
Could someone help me with the logic to write this code? I'm hitting a wall.
EDIT: The way I have it written above works in the if statement. But that would require 18 if statements. Is there a slicker way of writing this, maybe using an arraylist and a for loop?
I solved this myself. I created an arraylist of the mood icons and colors. Then I used this code:
for (MoodIconsWorkoutLog moodBefore : moodIcons) {
if(moodBefore.getMoodBefore() == currentLog.getMoodBefore() ){
holder.moodIcon.setImageResource(moodBefore.getMoodImage());
holder.moodIcon.setColorFilter(moodBefore.getColour());
}
}
Worked perfectly
Related
picture from a reddit news feed
(https://i.stack.imgur.com/6YXMK.jpg)
I am creating an app with a list view that is populated from a sqlite database. Each of the data base items can have a status of either “resolved” or “unresolved”.
I want the listview to have 3 “tabs” with the labels “all items”, “resolved items”, and “unresolved items” with correspoding sqlite queries to populate each.
It should behave similarly to the one pictured.
I assumed this would be a tabbed listview and have been watching tutorials for a week based on those search words and it’s taking taking me down a dark rabbit hole of fragments and changing gradles and so on. I’m not sure tabs are what i really want.
Could I do this with three buttons instead where each button would run a different query and populate my listviewcontainer?
Ideally, when the page is opened, the first “tab” would be highlighted and the listview populated with all records. As the other tabs are pressed, they would highlight and a new query would run.
Would another approach work better?
I’m not asking for code, I just want some conceptual direction on where to focus my research.
If I get you right you need to filter your query results in different lists. Making a lot of queries into database is not the thing that is preferable specially if it's going to be a long process and doing it a lot of times is time and memory consuming.
So to make it work you could simple store your full query result in one variable and change the RecyclerView data using custom method setList() and later using notifyDataSetChanged() to apply the changes.
To make it work you need to get understanding of "how RecyclerView works" and then you will be fine.
So after providing the right logic you would be able to simple split your whole query result as it's needed (by element values for example) as it's showed above:
About the code below:
list - is your query result
leftFilterList or rightFilterList - are lists that contain sorted items
adapter.setList(rightFilterList) - sets the RecyclerView data (filtered items in our case)
adapter.notifyDataSetChanged() - is used to notify RecyclerView that list was changed, and he need to rebuild it.
So we have two Buttons and logic that fillter items in differend ways.
public void left(View view) {
ArrayList<ExampleItem> leftFilterList = new ArrayList<>();
for (ExampleItem item : list) {
if (item.getTitle().length() % 2 == 0) {
leftFilterList.add(item);
}
}
adapter.setList(leftFilterList);
adapter.notifyDataSetChanged();
}
public void right(View view) {
ArrayList<ExampleItem> rightFilterList = new ArrayList<>();
for (ExampleItem item : list) {
if (item.getTitle().length() % 2 == 1) {
rightFilterList.add(item);
}
}
adapter.setList(rightFilterList);
adapter.notifyDataSetChanged();
}
And the result of filtering*:
sorry for wrong toast text. It shows the whole list size.
I am using FirebaseListAdapter to populate ListView. It is working fine for the data populated directly from Firebase. Here, I am trying to display/hide a tick image based on the status field. On initial load it is working fine, but when I scroll up and down the tick mark is getting set for some of the other items incorrectly. Need help! Here is my code inside populateView.
nameTextView.setText(appointment.getName());
timeTextView.setText(appointment.getTime());
if (appointment.getAppointmentStatus() == APPOINTMENT_COMPLETED) {
appStatusImage.setImageResource(R.drawable.ic_done_black_24dp);
} else {
appStatusImage.setVisibility(View.INVISIBLE);
}
This happens probably because you are using a RecyclerView, that meaning that the Views are reused instead of each object having it's own View. In other words, when you write:
if (appointment.getAppointmentStatus() == APPOINTMENT_COMPLETED) {
appStatusImage.setImageResource(R.drawable.ic_done_black_24dp);
}
Initially, you will set your View with the given image if it meets your if statement. But if the View itself is reused for another object that does not meet the if statement, the image resource will remain from the previous object property. What you should do is:
if (appointment.getAppointmentStatus() == APPOINTMENT_COMPLETED) {
appStatusImage.setImageResource(R.drawable.ic_done_black_24dp);
appStatusImage.setVisibility(View.VISIBLE);
} else {
appStatusImage.setVisibility(View.INVISIBLE);
}
And everything should work as you expect
Alright... No one laugh... I am working on an app in MIT/Google Appinventor. Here's a rundown:
Scans a barcode then runs the pictured function.
If the item is in the list then
Find that items position index in the inventory list.
Use that items index, to increment the value in the quantity list by 1.
If the item is not in the list then
Add that item to the inventory, and add a "1" to the quantity.
I can't see why it won't work so I was just checking to see if there are any obvious flaws in my logic. If the logic looks solid, then I should be able to figure out the Appinventor problem to make it work.
Here's your function translated (accurately?) into pseudo-code to help with my own understanding (and hopefully others'):
function addItem:
if inventoryList.contains(scannerResult):
inventoryPosition = inventoryList.positionOf(scannerResult)
quantityPosition = quantityList.positionOf(scannerResult)
quantityItem = quantityList.selectListItemAt(quantityPosition)
quantityList.insert(quantityItem at inventoryPosition)
else
inventoryList.add(scannerResult)
quantityList.add(1)
The problem appears to be in the logic when the scanner result is already in the list. I don't know the relevant app-inventor functions, but I think that you want something more like:
if inventoryList.contains(scannerResult):
inventoryPosition = inventoryList.positionOf(scannerResult)
quantity = quantityList.selectListItemAt(inventoryPosition)
quantityList.setListItemAt(quantityPosition to quantity + 1)
That last line is the bit I don't know how to translate into app-inventor language, but hopefully it's enough to point you in the right direction.
#blahdiblah made a good analysis of the problem.
The solution with App Inventor looks like this: instead of the insert list item block you have to use the replace list item block
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've got what I thought was a simple android UI design problem but I've been going around in circles for a couple of days. I have a REST service that I'm downloading XML from and displaying the XML in a form in an android app. I have a web page built and am mimicking this with android, same options, same URLs being sent to the REST service whether from android or the web pages. With HTML I can easily create checkbox groups and radiobutton/dropdowns for various id/display items, so for instance, I can display a planet option as:
<select name="planet"><option value="0">Mercury</option></select>
I wanted to do something similar in android where I had a pair of values, one an id and the other the user-friendly text to display. So I decided to create an adapter using android.util.Pair:
public class PairView extends Pair<String, String> {
public PairView(String first, String second) {
super(first, second);
}
public String toString() {
return second;
}
}
public class PairAdapter extends ArrayAdapter<PairView> {
}
So now I can put my id in pair.first and what to display to the user in pair.second.
My problem comes in that some of these options will be single-selects and some will be multi-selects. In html, that's not an issue, just use a checkbox group for multi, and radio buttons/dropdowns for single selects. In android however, it seems it's not so straight forward. I tried using Spinners for the adapters, but Spinner seems to only allow single selection. AlertDialog.Builder allows for single and multi-selections, but curiously I don't see an option for using an adapter for the multi-selection, just for single selections.
I guess what I really want is a consistent look for all my options, with radio buttons displayed for single selections and checkboxes displayed for multi selections, via an adapter so I can get the id's from the Pair for the items selected.
What approach should I use? A custom spinner with code added for multi-selections? AlertDialog.Builder and somehow make it use an adapter for multi-selections? Just create a plain Alert and wrap a ListView in it? Another option that is (hopefully) simpler?
I feel like I'm missing something very basic here.
I had a similar situation in an app I was making so would share what I opted for. I had different type of questions and depending on that I removed and added things in my activity. For radio buttons I used with elements in it. For multiple choice questions I wanted a checkbox based view so I added an empty within my layout and in code added CheckBox(s) to it.
As for the caption and value, for radio buttons and checkboxes you can set display text by setText and add any object/value as a tag. So what I used to do was something like this:
CheckBox option = new CheckBox(MyActivity.this);
option.setText("Option 1");
option.setTag(10);
Later on when you get the selected option, you can simply get its tag and use its value.
This is just one way of doing it which I found simple. Hope this helps