1

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.

2
  • When using android:entries="@array/buildings" are you then changing the Adapter for the ListView in your source code? Commented Feb 16, 2013 at 4:20
  • Using the android:entries, I have no adapter at all...the text for each list item is defined in the layout, so no adapter is used to populate the list. I have this working now, but I can't figure out how to handle the selection of an item without using the adapter now. Commented Feb 16, 2013 at 19:29

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.