Android- print out values of an array - android

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.

Related

How to save a ListView to database in one go

I am new to Android programming and have a basic question.
I have created a ListActivity where each row in its ListView has a TextView and a RatingBar. I am able to display values in the TextView and RatingBar by reading them from a pre-populated database; I am doing this via a custom CursorAdapter and it's bindView() method, i.e.
public class MyCursorAdapter extends CursorAdapter {
...
public void bindView(View view, Context context, final Cursor cursor) {
TextView name = (TextView)view.findViewById(R.id.name);
name.setText(cursor.getString(cursor.getColumnIndex(MyDbAdapter.KEY_NAME)));
RatingBar rating = (RatingBar)view.findViewById(R.id.life_bar_rating);
rating.setRating(cursor.getFloat(cursor.getColumnIndex(MyDbAdapter.KEY_RATING_VALUE)));
}
...
}
Now, my problem is that in my ListActivity I want the user to be able to click a "Save" button so that all changes are saved to the database in one go?. How would I iterate through the adapter to get the row ID and rating values so they can be saved (or is there another better way of doing this?).
The reason why I want to do a save in one go is because I thought it would be bad practice to update the database every time the user makes a change to a single rating (even though this is easier) as this causes more wear and tear on flash memory. I may be wrong about this, but I thought that there are a limited number of reads and writes with flash memory.
Remember the Adapter's backing array? The one you used to initialize it? save that ;)
or if that doesn't work...
for(int i = 0; i < listView.getAdapter().getCount(); i++)
SaveThing(listView.getAdapter().getItem(i));
if you need SQLite examples of saving... tomorrow.
edit for same of keeping comments clean:
as far as that object thing goes - you're free to cast it to anything that's in the listview. the data type, not view type.
so for example, an arrayAdapter that works with strings still returns Objects. but you will most certainly cast them to Strings because you know that's what you put in. it's awkward but thats how it is.

Flex ActionScript - ArrayList Issues

I am banging my head for the last couple days in order to get this done but Im unable to. Someone please help me out!
Let me not tell u the whole thing and will try to explain it simply n clearly.
Im having 1 ArrayList. I am trying to replicate that into another one and trying to delete an item at a particular index. But this not only deletes the item in the replicated ArrayList but also the original ArrayList.
For ex:
var DuplicateList:ArrayList = new ArrayList();
DuplicateList = OriginalList;
DuplicateList.removeItemAt(2);
The above not only deletes the "Item 3" at Index-2 in the DuplicateList but also in the OriginalList.
I just need some workaround with this approach as this is the only way by which whatever I typed inside the controls present in an ItemRenderer of a FLEX List control that uses the OriginalList as a dataProvider is RETAINED, when I change the dataProvider of the List Control from OriginalList to DuplicateList. The following approach does not retain all the data.
var DuplicateList:ArrayList = new ArrayList();
DuplicateList.addAll(OriginalList);
DuplicateList.removeItemAt(2);
ListCntrl.dataProvider = DuplicateList;
Thanks for your help in advance...
A very, very important thing to understand:
ActionScript3 uses references to objects. Because of that, the two variables in this line of code refer to the exact same instance of an ArrayList:
DuplicateList = OriginalList;
So, when you remove an item from one reference, it is gone from the next. If you want two separate instances of ArrayList, then you need to clone it like you are suggesting later in your code.
So far, so good... but why is your ListCntrl retaining the data from the OriginalList? That doesn't make any sense at all. If you remove an item from DuplicateList and then use it as the data provider, then that item shouldn't be there. I think there is more to this story...

Rowdatabound on Android?

I wrote a class similar to .NET GridView for android but my mind went blank for one action! when I loop through rows (or data whatever) I want to fire an event which includes selected row on every row. You can think as if you are reaching each row separately with helping by rowdatabound on .NET.
for (int m = 0; m < this.columns.length; m++) {
do some stuff
fire()
}
Do I need to create a new thread or listener? If yes how do I do these and how do I catch it on the otherside? Please give me an idea.
with this codes which I wrote below, I can create a gridview and display it without any problem. I need to get every rows one by one but how?
DataGrid dgSiparis = new DataGrid(this,lnSiparis,dt); //CREATING THE DATAGRID, argumants: context, linearlayout, a class named datatable which runs similar to DataTable on .NET, dt object has rows)
dgSiparis.setDataBind();
The answer:
create an interface for listener
implement your class with it (for this example DataGrid class will be implement it)
set it where you loop through rows
catch the listener you create on the instance class (for this ex dgSiparis)
There are no events in Java, instead it uses Observer - Listener pattern. Have a look at this article for details:
http://www.vogella.de/articles/DesignPatternObserver/article.html

Passing information between views

I am new to android programming, but I am trying to learn. I have written some code that takes in some parameters through a "normal" view with checkboxes and textviews. Then I use this information to generate a lot of numbers that I want to display in a listview. I have managed to create a listview when I press a run button, but how do I pass the information from the main view to the listview. Is it best to pass the information one number at the time or a large array with all the numbers. The list of numbers can be really large.
What you probably what to do is create an adapter with the numbers as the data source. If the numbers are in an array you can create a new ArrayAdapter and set the ListView adapter as that adapter:
ArrayAdapter adapter = new ArrayAdapter<Double>(getApplicationContext(), R.id.id_of_textbox, arrayOfDoubles);
listView.setAdapter(adapter);
In this code I've assumed the numbers are doubles, however ArrayAdapter is a generic class so it can be any object contained in the array. The array can also be presented as a List (like an ArrayList).
Hope that helps you out. Here are some bit of documentation to read and some good video sessions to watch:
ArrayAdapter
ListView.setAdapter()
The World of ListView Google I/O 2010 Session
How big is the array can get?
Most likely that displaying the list as another activity and passing the data as intent's extra will be the solution.

Problem with list activity

I have implmented pagination and it display 5 records per page. Now suppose I am on page 3 and click 3'rd element then 3'rd element of page-1 is selected.
I am not able to figure out problem as I always create new list object while setting data.
I used below code
temp = new ArrayList();
this.someListAdapter = new SomeListAdapter(this, R.layout.row_facet,temp);
setListAdapter(this.someListAdapter );
Below is signature of SomeListAdapter class.
public class SomeListAdapter extends ArrayAdapter<VoNeighborhood> {
}
Please help....
There really aren't enough details here about how you do pagination with your ListView.
So I might guess you're overriding onListItemClick and using the position variable it sends you, but you then don't take into account the page you're on?
Alternatively, just don't use pagination as you have an infinite canvas to scroll your list within — I don't think I've recall seeing an Android app so far that uses pagination!

Categories

Resources