Java 从另一个类获取列表数组中的帖子数。但它没有';不行?
我试图将类中arraylist中的帖子数输入mainactivity。 但这是错误的。 这是我的密码Java 从另一个类获取列表数组中的帖子数。但它没有';不行?,java,android,Java,Android,我试图将类中arraylist中的帖子数输入mainactivity。 但这是错误的。 这是我的密码 public static int countNotify; public static List<Notification> bindNotifyData(JsonElement list) { List<Notification> results= new ArrayList<>(); JsonObject
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;
}
但是count的值始终为0,请尝试创建类的实例 BindFetchDataHelper BindFetchDataHelper=新的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
在bindNotifyData类中。我不能将number的posts值设置为variable countnotify。有人能帮我吗?你的问题中没有足够的信息给出正确的答案。
BindFetchDataHelper bindFetchDataHelper = new BindFetchDataHelper ()
bindFetchDataHelper.setcountNotify(YOURVALUE); //set
int whatyourwant = bindFetchDataHelper.getcountNotify(); //get