Получить номер записи в массиве списков из другого класса. Но это не работает?

#java #android

#java #Android

Вопрос:

Я пытаюсь получить номер записи из arraylist в классе в mainactivity. Но это неправильно. Вот мой код.

 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 amp;amp; 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

Комментарии:

1. В вашем вопросе недостаточно информации, чтобы дать правильный ответ.

Ответ №1:

Попробуйте создать экземпляр вашего класса

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
  

Комментарии:

1. В классе bindNotifyData . Я не могу установить значения записей number в переменную countNotiffy . любой орган может мне помочь?