3

I am trying to develop a Registration screen from Android XML. As in every Registration form, I would need to display the list of countries. I am able to do this using string-array in strings.xml file.

The greater part of the problem is, when a user selects a country, the phone number field just below it should be initially filled with its respective country code, which may or may not be editable. Here, my problem is how do I get the country code when the country is selected. Do I need to use a database or is it achievable using xml itself?

Besides that, when user submits the Register form, I would have to send the abbreviated code of the country, not the full country name. Now, again this would require either a database or xml?

My app doesn't use database till now, it would not be so good to use database for this purpose. Moreover, almost any application that uses a registration needs this thing to be done, but I cannot find any resources on how to do this in Android.

Also, I would like to tell you that my minimum sdk is version 7.

Please help.

Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
openrijal
  • 583
  • 6
  • 22

2 Answers2

3

I was finally able to do it without using database. I'm writing down the steps so that it may help anyone else who needs the same thing to be done.

First I downloaded the CSV available at: https://github.com/mledoze/countries/blob/master/countries.csv

I removed all other fields, except those I needed. It left me with 3 fields: name, abbreviation and calling code.

Next, I downloaded the CSVReader from: http://code.google.com/p/secrets-for-android/source/browse/trunk/src/au/com/bytecode/opencsv/CSVReader.java

Got the items from the CSV as mentioned in How to parse the CSV file in android application? by "Kopfgeldjaeger" as:

String next[] = {};
List<String[]> list = new ArrayList<String[]>();

 try {
      CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("countries.csv")));
            for(;;) {
                next = reader.readNext();
                if(next != null) {
                    list.add(next);
                } else {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

Next I added an ArrayList for each of the values like:

ArrayList<String> countryNames = new ArrayList<String>();
ArrayList<String> countryAbber = new ArrayList<String>();
ArrayList<String> countryCodes = new ArrayList<String>();

for(int i=0; i < list.size(); i++)
{
    countryNames.add(list.get(i)[0]); // gets name
    countryAbber.add(list.get(i)[1]); // gets abbreviation
    countryCodes.add(list.get(i)[2]); // gets calling code
}

Then added it to the spinner in the XML layout as:

ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countryNames);
        spinner.setAdapter(countryAdapter);

// adding event to display codes when country is selected


spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                // display the corresponding country code
                TextView tvCountryCode = (TextView) findViewById(R.id.country_code);
                tvCountryCode.setText("+"+list.get(pos)[2]);
                countryPosition = pos;
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

This way, I was able to display country code in the xml file, when a country was selected from the dropdown list.

Remember to add the setOnItemSelectedListener() to achive that.

I hope this helps somebody in future.

Community
  • 1
  • 1
openrijal
  • 583
  • 6
  • 22
  • Why would you not want to use a database for this? It just seems to me that the type of data you use is very well suited for a database and the database is easier to query then a set of arrays. – Aleks G Apr 23 '13 at 11:03
  • because if I use database, this will be the only data I store there, my app currently does not use database. it may be an added overhead. – openrijal Apr 23 '13 at 11:58
  • 1
    "It may be an added overhead" - or it may be a lot more efficient application. If the reason is that you are not comfortable working with sqlite, that's a valid reason, but don't be afraid to admit it to yourself. – Aleks G Apr 23 '13 at 12:11
  • you are not getting my point here. I am much more comfortable with sqlite that parsing CSVs, but using database for this small thing that only also be used in registration, is indeed an overhead. may be you should admit your extra love with sqlite now. – openrijal Apr 24 '13 at 08:43
  • 1
    I won't admit it, because I don't have any love for it whatsoever :) In fact, I prefer to avoid it if I don't really need it. However when it comes to performance, it does offer much better results than parsing CSV or XML. – Aleks G Apr 24 '13 at 08:53
0

I would advise you to use a database. API level 7 supports SQLite databases (I used them in android 2.1 myself). Create one table that has all the required info:

create table countries (
    _id integer primary key autoincrement,
    country_code    char(2),
    country_name    varchar,
    phone_code      char(4),
    editable        integer
);

Then store your country information into this table. When populating your list of countries, use this table instead of XML; display country names and associate country codes with each corresponding list item. Then on selection, use the country code to get the phone code and the 'editable' flag - and act upon this info.

Aleks G
  • 56,435
  • 29
  • 168
  • 265