How can a RecyclerView adapter display multiple different GraphView graphs? - android

I want to display live graphs of incoming bluetooth sensor data in a list of cards.
I have tried using the AppendData() function for each series that holds data for the the various sensors. The series are stored in a list.
I first initialize the list of series with some series containing dummy data:
private List<PointsGraphSeries<DataPoint>> dataSeriesList = Arrays.asList(initSeries, initSeries, initSeries, initSeries, initSeries, initSeries);
Then, in onBindViewHolder() I update each of the series with the new reading:
public void onBindViewHolder(#NonNull ProductCardViewHolder holder, int position) {
....
dataSeriesList.get(position).appendData(newReading, true, 40);
....
//I set the series to the GraphView just once, like so:
if (holder.graph.getSeries().size() == 0)
holder.graph.addSeries(dataSeriesList.get(position));
...
}
I would expect that I would have 6 different graphs that each display the values for one sensor, since I am appending to a different series in the list for each position.
Instead, what I get are 6 identical graphs that display all 6 of the different sensor values for each point on the x-axis.
I have confirmed using logcat that each graph is only displaying one series, this means that each series somehow contains all of the data. I don't understand why, since I am clearly appending my readings to 6 seperate series.

Related

Android ListView with “tabs”

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.

Dynamically loading more data in line chart using MPAndroidChart library

I'm using the MPAndroidChart library in my app to render a line chart, and am facing an issue while adding more data dynamically to the chart. I tried going through the existing StackOverflow questions and also the GitHub issues for this library, but couldn't find a fix for my issue.
I'm using the LineChart API. In my use case i need to first show 15 data points on the chart, and once the user scrolls to the boundary, I'm fetching 15 more data points from the backend and resetting the data object with the set of 30 data points.
My logic for checking if the user reached the boundary is:
#Override
onChartTranslate {
.
.
if (!isLoading) {
if (lineChart.getHighestVisibleX() == lineChart.getXChartMax()) {
isLoading = true;
fetchMoreData();
}
}
.
.
}
Below method fetches data from the backend, and then updates the chart
fetchMoreData {
dataObservable.subscribe(newData -> updateUI(newData))
}
the update chart method with additional data
updateUI(newData) {
//from the data received, create new dataset,
//create a new lineData Object, and add this dataSet
//set the lineData object on the chart using
lineChart.setData(newLineDataSet)
//view port updates which consist of below
lineChart.setVisibleXRangeMinimum(4);
lineChart.setVisibleXRangeMaximum(4);
lineChart.setAutoScaleMinMaxEnabled(true);
lineChart.moveViewToX(previousBoundary)
lineChart.highlightValue(previouslyHighlightedValue)
isLoading = false
}
Things work fine for me, when i scroll slowly beyond the boundary, i.e., backend call to fetch additional data is fired and I get the new 15 data points, and I replace the existing lineData object with the new lineData object which has (old + new) data points.
I'm facing an issue if I scroll too quick, that the fetchMoreData is getting called multiple times (2/3 times), and the chart draws more data.
On debugging this I noticed that the lineChart.getHighestVisibleX() method is returning an incorrect value sometimes causing this to happen.
For e.g., the first call is triggered when the highestVisibleX equals the maxX value of 14. This updates the chart with new data points (now 30), and during redrawing of the chart, the onTranslate method gets called and the highestVisibleX somehow returns 29 (same value as maxX for the new data) even though the highest visible x-value is 14.
Could someone please help me with this issue?

populate a recycler adapter from databases

I'm trying to add data (objects) to an adapter, (for a recycler view) from many databases. Theres two recycler views (both use view pagers) A and B, the data in A can change and anything in A should be movable to B, what i did was create a class that makes objects and then made an array list that holds the objects so in A id have my array list
List<CardWriter> cardMakerList = new ArrayList<>();
and add it to my adapter
cardAdapter = new CardAdapter(cardMakerList);
then add items to it and update the adapter
CardWriter cardWriter = new
CardWriter(getResources().getDrawable(R.drawable.happy),"HAPPY"," happy");
cardMakerList.add(cardWriter);
cardAdapter.notifyDataSetChanged();
if i wanted to change the data i could
CardWriter cardWriter = new
CardWriter(getResources().getDrawable(R.drawable.sad),"SAD","sad ");
cardMakerList.add(cardWriter);
cardAdapter.notifyDataSetChanged();
and for adding items to B i could get the position of the item from the onClick method. The adapter ive used for B has almost identical variables and methods so i could use this
SpeakRecyclerGrid.cardMakerList.add(SpeakRecyclerGrid.cardMakerList.size(),
cardWriter);
and it works fine but ive now started using databases so what i had:
List<CardWriter> cardMakerList = new ArrayList<>();
becomes this:
List<addNewCard> cardMakerList = new ArrayList<>();
to change the data its a new database so it changes to this
List<simpleNewCard> cardMakerList = new ArrayList<>();
but trying to add this new data to my (A) adapter doesnt work as its expecting addNewCard (even though the methods and properties are identical) it gives me an error like
CardAdapter(java.util.List<greendao.addNewCard>) in CardAdapter cannot be
applied to (java.util.List<greendao.addSimpleCard>)
so i don't want to make lots of different adapters i just want one for recycler view adapter A and one for the recycler view adapter B.
I've posed this question lately on here and had someone reply that i could make them implement a common interface or abstract Card class and make a List of that, I'm a novice programmer and have spent the last few days studying this and trialling different things, but I'm using a project called GreenDao and i don't know how to implement this can anyone give me some pointers

How to display differents images, not created when the activity is create [ANDROID]

I'm developing a game, and in the model of the application I have an 'endless' mode. A number of monsters are created, and when they die new ones are created. The monsters move and they are not always at the same place.
I would like to know how to display them: in the activity, there will be five monsters, but after that there are 4, 3, 2, 1 and 0. I would like to display them dynamically.
Welcome to Stackoverflow!
It is good practice to start with already developed game engines if you want to develop games on Android. But its not essential. You can still develop simple games with raw Android.
To do so, you need to have a clear concept on how view and related APIs works.
I will recommend you to explore http://developer.android.com/training/custom-views/custom-drawing.html
In your case,
Game game = new Game(Game.Settings);
game.createHero();
game.createMonsters();
while(game.playing()){
game.render();
if(game.needNewMonster()){
game.createMonster(); // at random position in view
}
}
Inside Game.java,
List<Monster> list = new ArrayList<Monster>();
void createMonster(){
list.add(new Monster("monsterA", x, y, R.drawable.monster)); // x,y are position and monster is drawable for monster. write the model code yourself
}

Android- print out values of an array

I am new in programming Android. I am developing an app that you want to capture sound and analyzes it. The amplitude of the sound for each time is stored in an array. These are the data needed to analyze . My question is: How I show the values of the array in wich the sound is saved?
for (int i = 0; i < array.length ; i++ )
Log.i("value" , Integer.toString(array[i]))
This depends on how you want to output the values. You could choose to create a RecyclerView instance in your GUI and connect the array with an adapter (read here) or you could choose to format that information into a String and insert the text into a TextView using the setText(String) method.
Edit: Based on your comment it sounds like you should look at the Recylcer View. You will need the following things.
A layout file containing a Recycler View.
A ViewHolder class that extends RecyclerView.ViewHolder - this is used to display your content in individual items of the list
An adapter class that extends RecyclerView.Adapter - this connects your array to the ViewHolder
Some links to help you get on the way with RecyclerView:
A first glance at RecyclerView
TwoWay View
They do a much better job explaining and showing how the code will work then I will writing it again here.

Categories

Resources