在 android 中,我的 pojo class 实例正在获取空值

In android my pojo class instance are getting null value

我创建了一个 pojo class.. 我在其中存储我从 api..

获得的数据

在 MainActivity.java 中,我使用 loopj 来解析 json 数据

storeList = new ArrayList<AllStore>();
                try {
                    JSONArray jsonArray = new JSONArray(new String(responseBody));
                    for (int i = 0; i < jsonArray.length(); i++) {
                        store = new AllStore();

                        store.setLocation(jsonArray.getJSONObject(i).getString("storeName"));
                        store.setAddress(jsonArray.getJSONObject(i).getString("address"));

我的 pojo class

AllStore.java

public class AllStore{

String storeName;
String address;

public AllStore(String storeName, String address){
this.storeName= storeName;
this.location = location;
}

public AllStore{

}

public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

public String getStoreName() {
        return storeName;
    }

    public void setStoreName(String storeName) {
        this.storeName = storeName;
    }

}

在其他 java classes.. 而不是适配器 class 当我做

AllStore allStore = new Allstore();

mText.setText(allStore.getStoreName);

此处 allStore.getStoreName 值为 null 显示..如何解决此问题..

请帮忙

AllStore allStore = new Allstore();

mText.setText(allStore.getStoreName());

在这里您使用默认构造函数创建一个新对象 Allstore

public AllStore{

}

如果您调用 .getStoreName 它将 return 为空,因为您还没有设置任何内容。

在另一种方法中,您必须在数组列表中添加存储对象

store.setLocation(jsonArray.getJSONObject(i).getString("storeName"));        
store.setAddress(jsonArray.getJSONObject(i).getString("address"));
storeList.add(store);

稍后如果您想使用列表中项目的名称,您可以这样做:

mText.setText(storelist.get(index).getStoreName());

其中 index 是项目在列表中的位置。