I am writing an application which contains a few simple views; first is just two buttons. each button leads to a map, and from this map is a button which opens a list of buildings on the site. From here the user will be able to select a building and view detailed information on it (much like a contacts list actually).
So far, I am able to populate the listview by creating the string array to populate it it directly within the activity, like so:
public class BuildingsActivity extends ListActivity {
static final String[] buildings=new String[]{
"Building 1",
"Building 2",
"Building 3",
"Building 4",
"Building 5"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buildings);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,buildings));
}
This works all well and good for a 5 item list, but in it's final form, the list may contain up to a hundred buildings, and I have 2 concerns:
1) A one-hundred item long string array is going to look really gross defined in code like this
2) I am worried about how long it will take the application to open the activity if it has to generate such a large list of string values beforehand
To avoid this, I had the idea to define this string array in the strings.xml file and assign the strings to the list layout in the corresponding layout XML for the list activity. However, this so far doesn't seem to work; no errors but the android:entries command does not see to affect the layout at all. This is the XML. If anybody has knowledge of populating a list this way, I would appreciate your answers.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BuildingsActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/buildings">
</ListView>
</RelativeLayout>
My other alternative is to retrieve it from a text file, which I will have to do for building details (as the volume of text is too large to bother saving as strings) but wanted to avoid for the list. Admittedly, I have not heavily researched this route (both because I had not really wanted to use it yet and based on what I have seen on other forums it looks difficult, but if there is a lot of support for this method then I will certainly appreciate any advice in using it.
Forgive me if any of this has already been covered in other threads. I DID search for similar topics, but nobody seemed to want to populate a list this way and a lot of the language from their questions went over my head (I am very new...this is my first real application, besides having worked through some of the tutorials on the developer website.
Java's ArrayList implements the Serializable interface, which allows you to save and load an array to and from a pure binary format using the functions writeObject and readObject. This page should be enough to get you started.
If you want to populate the listView with from a resource file you just have to use this in your code:
String[] values = getResources().getStringArray(R.array.<input_file>);
Once you have the values loaded you just have to assign it to the ArrayAdapter like this:
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
The second parameter is a predefined layout of android SDKs, you could use your own layout.
If your String[]'s ceiling is 100, I don't see it as a problem to just generate the array and pass it to the adapter. As for your concern about the code looking gross, have you considered storing this information in a database and then requesting it? For instance, you could have a Building Entity, with all of its information and persist it to MySQL using OrmLite. http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_1.html#SEC1 You could then write a getter than returns an array representation to avoid the ugly code.
Related
I'm new to Android programming. I can get simple ListViews to work in which I use an ArrayList of strings. I want to take a step upward in complexity and have my ListView be composed of simple Java objects like this:
class myItem {
public String name;
public Integer price;
public Integer weight;
}
The ListView only needs to display the name in the above object. It doesn't have to have multiple icons, or multiple clickable actions for each item.
But I don't know where to start. All of the examples I see on the WWW are much more complicated and require me to learn things that have nothing to do with this (like a database). Or each ListView items is displayed with multiple views like text and images and icons etc. and each is clickable for a different action. I don't need any of that, and I'm getting bogged down reading about unneeded features in order to get those examples to work.
Does anyone have an example of a simple ListView that contains simple Java objects (like the one above)?
But I don't know where to start.
Implement a public String toString() method on myItem. Then create an ArrayList<myItem> and use that instead of ArrayList<String>, populate that list with suitable myItem instances, and put the list into an ArrayAdapter<myItem> instead of an ArrayAdapter<String>. No other changes should be required -- whatever layout you are using for an ArrayAdapter<String> will work with your ArrayAdapter<myItem>.
In case you are new to Java, toString() is the standard Java method for returning a String representation of an object. The default behavior of ArrayAdapter is to call toString() on the object for the given list position and use that to fill in the row.
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
Following on from an earlier question I'm trying to get multiple (widgets or in this case WebViews) inside a GridView.
So I wasn't really sure how to go about it whether to make my own adapter (seemed scary at the time) or create an ArrayAdapter<WebView>.
I wasn't really sure if ArrayAdapter could accept it like this or if it only supported primitive types.
GridView contentGrid;
LinearLayout contentLayout;
WebWidget[] webWidgets;
WebWidget web1;
WebWidget web2;
WebWidget web3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web1 = new WebWidget(this, "http://www.google.ie");
web2 = new WebWidget(this, "http://www.facebook.com");
web3 = new WebWidget(this, "http://www.youtube.ie");
webWidgets = new WebWidget[]{web1,web2,web3};
contentGrid = (GridView) findViewById(R.id.contentGrid);
//Heres my attempt at an adapter for webview
ArrayAdapter<WebView> adapter = new ArrayAdapter<WebView>(this,
android.R.layout.simple_list_item_1, webWidgets);
contentGrid.setAdapter(adapter);
Unfortunately what comes out is the string version of the WebView.
So question is can I do it this way or am I better of making my own CustomAdapter?
NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1
No matter how you implement your Adapter, you cannot reliably put scrollable widgets in other scrollable widgets, at least where they scroll in the same direction. Since both WebView and GridView scroll vertically, you will get unreliable results trying to combine them this way.
With that, on to some of your statements and questions:
I wasn't really sure if ArrayAdapter could accept it like this or if it only supported primitive types.
An ArrayAdapter can adapt a Java array or an ArrayList of whatever data type you like.
Unfortunately what comes out is the string version of the webview...NOTE: Could someone maybe point some information as to what the second constructor variable is? I.E android.R.layout.simple_list_item_1
The second constructor variable is what you want the cells in your GridView to look like. In your case, you are specifying a built-in layout resource that is a TextView. Hence, you are telling Android you want all your grid cells to be TextView widgets. Android will call toString() on the objects in your array, pour that result into the TextView, and the TextViews will go in your grid cells.
If you want your ArrayAdapter to be returning things other than a TextView, you will need to override getView() and handle more of that yourself, possibly using a layout file of your own creation.
So question is can I do it this way or am i better of making my own CustomAdapter?
As noted at the outset of my answer, what you want simply will not work reliably.
Pretending for the moment that having WebViews in a GridView would work, the simplest solution would be for you to override getView() in your own subclass of ArrayAdapter, as I mentioned previously. Here is a free excerpt from one of my books that goes over this process.
I've searched far and wide for this answer and can't seem to find it.
I'm looking to populate a very simple 3 line listview, no more then 5-6 words per line at the most inside of my android app.
I'm currently using a base adapter and a string array to enable the actual text to show up on the screen.
I want to have the ability to update the information inside of my listview remotely using
some sort of means whether that's xml, SQLite, plain text, etc and then have that hosted file populate my listview.
Can anyone here help me to figure out how to do this? I'm still pretty new to android development so please go easy on me. Hopefully this question wont be too hard answer and also not too difficult to enable for a newbie like myself.
If the most you're going to ever have in there is just 3 lines of text, I think a SQLite DB may be a bit much for your situation. I'd look into using a Typed Array.
Here's a link to the Android Dev Guide on this subject:
http://developer.android.com/guide/topics/resources/more-resources.html#TypedArray
Here's a code sample:
public class YourListActivity extends ListActivity {
String[] mTestArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an ArrayAdapter to contain your items
ArrayAdapter<String> adapter;
mTestArray = getResources().getStringArray(R.array.yourArray);
// Assign your array to an adapter with your layout file
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mTestArray);
// Assign the adapter to this ListActivity
setListAdapter(adapter);
}
}
EDIT
Just realized that your data will be on a remote server, so this approach may not work for you, but it can still give you an idea of how to take your data once received from your remote server and place it into a ListView.
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.