Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3445072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:59:45+00:00 2026-05-18T08:59:45+00:00

I have this .xml file with countries and their countrycodes in them. This is

  • 0

I have this .xml file with countries and their countrycodes in them. This is how it looks like:

<?xml version="1.0" encoding="UTF-8"?>
<landen>
<land>
   <naam>Afghanistan</naam>
   <code>AF</code>
</land>
<land>
   <naam>Albani�</naam>
   <code>AL</code>
</land>
<land>
   <naam>Algerije</naam>
   <code>DZ</code>
</land>
<land>
</landen>

Now I want people to choose one country out of an list. I though an AlertDialog would be nice to display everything.

The way i get the values out of my xml-file is like this:

protected ArrayList<Land> getLanden() {
    ArrayList<Land> lijst = new ArrayList<Land>();
    try {
        DocumentBuilder builder =DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(getAssets().open("landenlijst.xml"));
        NodeList nl = doc.getElementsByTagName("land");
        for (int i=0;i<nl.getLength();i++) {
            Node node = nl.item(i);

            Land land = new Land();

            land.land = Xml.innerHtml(Xml.getChildByTagName(node, "naam"));
            land.landcode = Xml.innerHtml(Xml.getChildByTagName(node, "code"));
            lijst.add(land);
        }
        Log.d("Gabug","Klaar met parsen");
        Log.d("Gabug","Landen: " + lijst);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return lijst;
}

And I use this to make my AlertDialog:

public void KiesLandMenu(){
        ArrayList<Land> alleLanden = getLanden();
        final CharSequence[] items = alleLanden.toArray(new CharSequence[alleLanden.size()]);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Kies land");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                switch (item){
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                }
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

I don’t know if this works as DDMS returns some bytecode or something when i Log it. And after that it Force Closes because of ArrayStoreException..

Now my question is; is this the best way to do this? if yes, how can I fix the ArrayStoreException? If no, what are better ways to let my user choose a country (a whole new view maybe)?
Furthermore, how can I register what country someone tapped?

EDIT:

I slightly changed the sample code below and I get an NullPointerException now..

public void KiesLandMenu(){
    ArrayAdapter<Land> arrAdapter;
    ArrayList<Land> alleLanden = getLanden();
    arrAdapter = new ArrayAdapter<Land>(this, android.R.layout.simple_list_item_single_choice, alleLanden);

    ListView list = (ListView)findViewById(R.layout.lijstview);
    list.setAdapter(arrAdapter);
    list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> list, View view, int position,
                long id) {
            Log.e("item clicked", String.valueOf(position));
        }
    });
}

The NullPointerException is at list.setAdapter(arrAdapter);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:59:45+00:00Added an answer on May 18, 2026 at 8:59 am

    Make a layout with a ListView, then set that layout in your onCreate. To make the list, you can do something like:

    public class RunTestProject extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);  //whatever you want your layout to be
    }
    
    // getLanden() implementation goes here
    
    
    public void KiesLandMenu(){
        ArrayList<Land> alleLanden = getLanden();
        arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, alleLanden);
    
        Dialog dialog = new Dialog(this);
        dialog.setTitle("Kies land");
        dialog.setContentView(R.layout.withList); // The dialog layout
        ListView list = (ListView) dialog.findViewById(android.R.id.list); //note that it's not simply findViewById
        list.setAdapter(arrAdapter);
        list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    
        list.setOnItemClickListener(new OnItemClickListener() {
    
            public void onItemClick(AdapterView<?> list, View view, int position,
                    long id) {
                Log.e("item clicked", String.valueOf(position));
            }
        });
    
        dialog.show();      
    }
    

    }

    When the user chooses on an item, you can see in the log that the item’s position in the array is shown.

    Your layout file can be something like:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <ListView android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1" 
             /> 
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this xml file <?xml version=1.0 encoding=utf-8?> <CRM> <Unit ID=1 Name=آذربایجان شرقی> <city
Lets assume we have this xml: <?xml version=1.0 encoding=UTF-8?> <tns:RegistryResponse status=urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Failure xmlns:tns=urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0 xmlns:rim=urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0> <tns:RegistryErrorList
I have only this in my mxml source code: <?xml version=1.0 encoding=utf-8?> <mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml
To get this XML file: <?xml version=1.0 encoding=UTF-8?> <root> <command action=retrieve id=1234 code =
I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
I have normally hand written xml like this: <tag><?= $value ?></tag> Having found tools
I have this xml file target.xml: <World> <Nkvavn> <Rcltwkb> <Pjwrgsik /> <Nemscmll /> <Fnauarnbvw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.