Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import android.content.Context;
import android.content.SharedPreferences;

import android.text.TextUtils;

public class Main {
    public static final String PREF_NAME = "weather";
    public static final String PREF_KEPT_CITY_NUM_KEY = "kept_city_num";
    public static final String PREF_KEPT_CITY_ITEM_BASE = "kept_city_item_";

    public static ArrayList<String> loadSavedCityList(Context c) {
        ArrayList<String> list = new ArrayList<String>();

        SharedPreferences preferences = c.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        int cityNum = preferences.getInt(PREF_KEPT_CITY_NUM_KEY, 0);

        for (int i = 0; i < cityNum; i++) {
            String keptCity = preferences.getString(PREF_KEPT_CITY_ITEM_BASE + (i + 1), "");

            if (!TextUtils.isEmpty(keptCity)) {
                list.add(keptCity);
            }
        }
        return list;
    }
}