Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Context;
import android.content.SharedPreferences;

public class Main {
    public static final String DEF_PREF_NAME = "Default";

    public static ArrayList<String> getArrayListValue(Context context, String name) {
        ArrayList<String> list = new ArrayList<String>();
        SharedPreferences prefs = getPref(context, DEF_PREF_NAME);
        String json = prefs.getString(name, null);
        if (json != null) {
            try {
                JSONArray array = new JSONArray(json);
                for (int i = 0; i < array.length(); i++) {
                    String url = array.optString(i);
                    list.add(url);
                }
            } catch (JSONException e) {
            }
        }
        return list;
    }

    public static SharedPreferences getPref(Context context, String name) {
        return context.getSharedPreferences(name, Context.MODE_PRIVATE);
    }
}