簡體   English   中英

從另一個類獲取列表數組中的帖子數。 但它不起作用?

[英]Get number of post in list array from another class. But It doesn't work?

我嘗試從類中的數組列表中獲取帖子數到主要活動中。 但這是錯誤的。 這是我的代碼。

public static  int countNotify;
    public static List<Notification> bindNotifyData(JsonElement list)
    {
        List<Notification> results= new ArrayList<>();
        JsonObject dataJsonObj = list.getAsJsonObject();
        // get data api from Json array "updates"
        JsonArray notifyJsonArray = dataJsonObj.get("updates").getAsJsonArray();
        ArrayList<Notification> notifyList = new ArrayList<>();
        countNotify=notifyJsonArray.size();
        if(notifyJsonArray != null && notifyJsonArray.size() > 0) {
            for(int i = 0; i < notifyJsonArray.size(); i++) {
                JsonObject notifyJson = (JsonObject) notifyJsonArray.get(i);
                Notification notification = new Notification();
                notification.setContent(notifyJson.get("content").getAsString());
                // Convert timestamp to Datetime
                String timestamp= notifyJson.get("time").getAsString();
                notification.setTime(ConvertTimestamp(timestamp));
                results.add(notification);
                // count numbers of the post in the list json array.
            }
        }
        return results;
    }

在 MainActivity.class 中

 final int count=BindFetchDataHelper.countNotify;

但是count的值總是0

嘗試創建您的類的實例

BindFetchDataHelper bindFetchDataHelper = new BindFetchDataHelper()

然后調用 final int count=bindFetchDataHelper.countNotify;

我有同樣的問題,它現在應該可以工作了。

編輯

像這樣嘗試:

public class BindFetchDataHelper {

private static int sTest;



static {
public static  int countNotify=0;

}




public static int getcountNotify() {
    return countNotify;
}

public static void setcountNotify(int setcountNotify) {
    this.countNotify = countNotify;
}

//your others functions
}

現在訪問變量或設置它:

BindFetchDataHelper bindFetchDataHelper = new BindFetchDataHelper ()

bindFetchDataHelper.setcountNotify(YOURVALUE); //set


int whatyourwant = bindFetchDataHelper.getcountNotify(); //get

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM