Java 如何使小部件的JSONArray保持活动状态?

Java 如何使小部件的JSONArray保持活动状态?,java,android,widget,Java,Android,Widget,我的应用程序有一个带有两个按钮的小部件,可以来回移动,并显示存储在JSONArray中的信息。它工作得很好,但问题是当我关闭应用程序时,小部件停止工作,因为JSONArray变为null。即使在应用程序关闭的情况下,我如何保持其工作 它太长了,无法在这里发布,但这是我单击“下一步”按钮时的部分 if(intent.getAction().equals(NEXT_LISTING)){ try{ if (listingNumber !=15)

我的应用程序有一个带有两个按钮的小部件,可以来回移动,并显示存储在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();
        }
    }
if(intent.getAction().equals(下一个清单)){
试一试{
如果(列表编号!=15)
{
listingNumber++;
}
JSONObject latestListing=jsonListings.getJSONObject(listingNumber);
String titleString=latestListing.getString(“title”);
String descriptionString=latestListing.getString(“描述”);
String categoryString=latestListing.getString(“类别”);
Date postDate=new SimpleDateFormat(“yyyy-MM-dd'T'HH:MM:ss”).parse(latestListing.getString(“created”);
Date newDate=新日期(postDate.getTime()+Utils.getCurrentTimezoneOffset()*(3600*100));
字符串createdDate=Utils.getFormattedDate(newDate);
RemoteView视图=新的RemoteView(context.getPackageName(),R.layout.njoftime\u小部件);
views.setTextViewText(R.id.title,titleString);
views.setTextViewText(R.id.description,descriptionString);
views.setTextViewText(R.id.textView_category,categoryString);
views.setTextViewText(R.id.textView\u日期,createdDate);
对于(int i=0;i
尝试使用共享首选项存储JSON数组:

创建共享引用:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit();
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
将数据存储为密钥/值对:

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
检索SharedReferences数据:

//如果键的值不存在,则返回第二个参数值-在本例中为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
正在从SharedReferences中删除键值:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit();
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
/********清除共享数据引用中的所有数据***********/

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

下面是一个教程

分享您的widget代码您在哪里保存jsonListings以及何时初始化或删除该字段?当小部件第一次启动时,我向我的web服务发出Http请求,获取json并将其存储为JSONArray。然后我在本地来回移动,不做更多的请求。你确定jsonListings为空吗?我怀疑传入listingNumber的最新列表(JSONObject)可能为空。如果您共享您的logcat输出,这将很有帮助。不,我确定,这就是为什么我对listingNumber有If条件的原因。我想这是因为这些是运行时变量,这就是为什么我在发布之前关闭应用程序时这些变量为null,我只是尝试了SharedReferences,但在这行上获取它时仍然为null
String listings=listingPreferences.getString(“com.rtsoftwaregroup.njoftime.widgetlings”,null)当你的应用程序运行时,你在哪里将json数组插入到共享首选项中?当您试图检索用于将数据存储到共享首选项中的json数组时,
com.rtsoftwaregroup.njoftime.widgetListings
只是一个
,例如
listingPreferences.edit().putString(“com.rtsoftwaregroup.njoftime.widgetListings”,jsonListings.toString()).commit()
在您的小部件中,您确定
listingPreferences
对象不为空吗?当你试图检索数据时,它被初始化了吗?该死的,就是这样,我必须初始化每个按钮,谢谢你,伙计