Today we’ll play lists. First of all, many thanks to articles from Android Developers, Proyectosimio and SGOliver that have served me invaluable help.
You can followprevious articles in this section here
Well, the next step to improve the application is to have the list of parkings also a list format to quickly access & parking. Ideally will split the screen into two, for these side parking for list format and the other format map .
This will touch the layout . In res -> layout -> activity_main.xml going to change what we had with
.
If you look, first create a LinearLayout with an identifier @ + id / top_half that will fill it. Inside this first LinearLayout we add a ListView, where we will insert our list. Then insert the following LinearLayout map as we had before.
get the screen is divided exactly in half use weights (or weights). By putting the property android: layout_weight = “1″ in both LinearLayout we’re indicating that both are equally important in the app, and therefore, given half the floor space each. If we wanted to map occupies more, we would indicate a higher weight, eg “2″. The rule for distributing the space is as follows:
Space that’ll
= Weight / Sum of all weights
For example, a LinearLayout with weight 3 and another with 1, occupy respectively: 3 / (3 +1) = 75% and 1 / (3 +1) = 25%. If you want more information on the subject I recommend this article and this one.
Design
The first thing we do is correctly define the container on this list in the layouts. But that had not already done? No. Just so you understand, before we split into two screen come up saying that a list. Now we are defining the style of each row : height, spaces, colors, text, etc. ..
Here
threw me several hours to find the style I liked. You will notice that it is a hell and it is advisable to review the properties wrap_content and the rest of the layout_width (here, here and here) and the weights of which we have spoken. Basically, this design of mine creates a list in which each row consists of the name to id parking lblTitle and smaller text indicating if it is free or not with the id lblStatus . These ids will use them later and can have any name you please ..
Adapter
Then we need a
adapter (adapter). Why? Well, we have a list, yes. But, how? Well, I have to tell Android, and that what we do through this adapter. Therefore, in our class we create a new class that extends ArrayAdapter
First of all, we define the default constructor which is composed of an Activity context and a list of items we receive, in my case the ParkingMarker. And then the class that interests us: getView
.
getView will define the types of content you want to appear on our list and where we want them. So the first thing we do is define a item on this list and assign the design we’ve done before with the first two lines.
We then define the elements of our item:
- associate with TextView lblTit (invented name) of the item layouts.list_details.xml lblTitle we have defined in the design stage.
- associate with TextView lblStat in the same way the item lblStatus.
- well, look if the parking is free or not obtained, and we will feature green or red.
- finally we return this item we have created.
To implement the list, do the following.
1. We will create three variables: lstOptions, adapter and parkings_data ??strong> private class level so that they can access from any subclass. Variable parkings already existed in the previous release.
private ListView lstOptions;
private List parkings;
List parkings_data;
ParkingAdapter adapter;
2. In the onCreate
/ / Create the list of
& parking parkings = new ArrayList ();
/ / Add our car parks to the list
parkings_data = new ArrayList ();
parkings_data = parkings;
adapter = new ParkingAdapter (MainActivity.this, parkings_data);
lstOptions = (ListView) findViewById (R.id.LstParkings);
lstOptions.setAdapter ( adapter)
Waiting time
And now comes a question we have to consider. It is important to note that the parkings_data ??em> collects information from varying parkings . This variable is what we have shared with maps parse XML files generated .
So, if we want to assign it to make sure the variable is filled parkings. But as it is filled asynchronously, you must be careful: we have to ensure that the task of the asynchronous class is finished before calling the adapter. That is done using the get () method of our asynchronous class. After the
task.execute (URL)
introduce the following code
try {
task.get(2500, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block e.printStackTrace
();
} catch (ExecutionException e) {
/ / TODO Auto-generated catch block e.printStackTrace
();
} catch (TimeoutException e) {
/ / TODO Auto-generated catch block e.printStackTrace
();
}
And already, we have our list of car parks in the upper half of our charging properly.
In the next installment we will see how to add the top bar we see in the capture and adding listeners
I remind you that the project source code is found on GitHub
Any questions, suggestions or questions you can find me on twitter!
No comments:
Post a Comment