如何保持小部件的JSONArray?

时间:2014-09-24 10:06:29

标签: java android widget

我的应用程序有一个小部件,其中有两个按钮来回移动并显示存储在JSONArray中的信息。它工作得很好,但问题是当我关闭应用程序时,小部件停止工作,因为JSONArray变为null。即使应用程序关闭,我怎样才能保持工作?

它太长了,无法在此发布,但这是我点击下一个按钮时的部分

if(intent.getAction().equals(NEXT_LISTING)){
        try{
            if (listingNumber !=15)
            {
                listingNumber++;
            }

            JSONObject latestListing = jsonListings.getJSONObject(listingNumber);
            String titleString = latestListing.getString("title");
            String descriptionString = latestListing.getString("description");
            String categoryString = latestListing.getString("category");
            Date postDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(latestListing.getString("created"));
            Date newDate = new Date(postDate.getTime() + Utils.getCurrentTimezoneOffset() * (3600 * 100));
            String createdDate = Utils.getFormattedDate(newDate);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.njoftime_widget);
            views.setTextViewText(R.id.title, titleString);
            views.setTextViewText(R.id.description, descriptionString);
            views.setTextViewText(R.id.textView_category, categoryString);
            views.setTextViewText(R.id.textView_date, createdDate);

            for(int i=0; i < categoriesList.size();i++)
            {
                if(categoriesList.get(i).get(0).equals(latestListing.getString("category")))
                {
                    views.setImageViewResource(R.id.category_icon_widget, iconList.get(i));
                }
            }

            if(listingThumbnails.get(listingNumber) != null)
            {
                views.setViewVisibility(R.id.listing_photo, View.VISIBLE);
                views.setImageViewUri(R.id.listing_photo, Uri.parse(""));
                Bitmap roundedBitmap = getRoundedLeftCornerBitmap(listingThumbnails.get(listingNumber), 10);
                views.setImageViewBitmap(R.id.listing_photo,roundedBitmap);
            }
            else
            {
                views.setViewVisibility(R.id.listing_photo, View.GONE);
            }
            // Instruct the widget manager to update the widget

            ComponentName componentName = new ComponentName(context, NjoftimeWidget.class);
            AppWidgetManager.getInstance(context).updateAppWidget(componentName, views);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试使用Shared Prefernce存储您的JSON数组:

创建SharedPreferences:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit();

将数据存储为KEY / VALUE对:

editor.putBoolean("key_name1", true);           // Saving boolean - true/false
editor.putInt("key_name2", "int value");        // Saving integer
editor.putFloat("key_name3", "float value");    // Saving float
editor.putLong("key_name4", "long value");      // Saving long
editor.putString("key_name5", "string value");  // Saving string

// Save the changes in SharedPreferences
editor.commit(); // commit changes

Retrive SharedPreferences数据:

//如果key的值不存在则返回第二个参数值 - 在这种情况下为null

pref.getBoolean("key_name1", null);         // getting boolean
pref.getInt("key_name2", null);             // getting Integer
pref.getFloat("key_name3", null);           // getting Float
pref.getLong("key_name4", null);            // getting Long
pref.getString("key_name5", null);          // getting String

从SharedPreferences中删除键值:

editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4

// Save the changes in SharedPreferences
editor.commit(); // commit changes

/ ******** 清除SharedPreferences的所有数据 ************* /

 editor.clear();
 editor.commit(); // commit changes

这是一个教程Link