ListView包含来自外部数据库的数据

时间:2017-12-19 14:38:04

标签: java android json database external

我正在尝试从我的JSON文件中捕获数据并将它们放在ListView中。我的源代码正确合并,买我的ListView是空的,我不知道为什么。

我创建了DisplayMarinasListView.class,它从URL获取JSON_STRING数据:http://gypscom.000webhostapp.com/jsongetmarinadata.php。在这个类布局中,我只有ListView。

public class DisplayMarinasListView extends AppCompatActivity {
String JSON_STRING;
JSONObject jsonObject;
JSONArray jsonArray;
MarinaInfoAdapter marinaInfoAdapter;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_marinas_listview_layout);
    marinaInfoAdapter = new MarinaInfoAdapter(this, R.layout.row_layout);
    listView = (ListView)findViewById(R.id.marinasListView);

    JSON_STRING= getIntent().getExtras().getString("json_data");
    String marinaId, description;
    double BCor, LCor;
    int size, availablePlace;
    boolean isOpen;

    try {
        jsonObject = new JSONObject(JSON_STRING);
        int count = 0;
        jsonArray = jsonObject.getJSONArray("server_response");
        while (count<jsonArray.length()) {
            JSONObject JO = jsonArray.getJSONObject(count);
            marinaId = JO.getString("marina_id");
            BCor = JO.getDouble("b_cor");
            LCor = JO.getDouble("l_cor");
            size = JO.getInt("size");
            availablePlace = JO.getInt("available_place");
            isOpen = JO.getBoolean("is_open");
            description = JO.getString("description");

            MarinaInfo marinaInfo = new MarinaInfo(marinaId, BCor, LCor, size,
                    availablePlace, isOpen, description);
            marinaInfoAdapter.add(marinaInfo);
            count++;
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

}

MarinaInfo.class从带有JSON数据的String中捕获数据

public class MarinaInfo {
private String marinaId, description;
private double BCor, LCor;
private int size, availablePlace;
private boolean isOpen;

public MarinaInfo (String marinaId, double BCor, double LCor, int size, int availablePlace,
                   boolean isOpen, String description) {
    this.setMarinaId(marinaId);
    this.setBCor((float) BCor);
    this.setLCor((float) LCor);
    this.setSize(size);
    this.setAvailablePlace(availablePlace);
    this.setIsOpen(isOpen);
    this.setDescription(description);
}


public String getMarinaID() {
    return marinaId;
}
public String getDescription() {
    return description;
}
public float getBCor() {
    return (float) BCor;
}
public float getLCor() {
    return (float) LCor;
}
public int getSize() {
    return size;
}
public int getAvailablePlace() {
    return availablePlace;
}
public boolean getIsOpen() {
    return isOpen;
}
public void setAvailablePlace(int availablePlace) {
    this.availablePlace = availablePlace;
}

public void setMarinaId(String marinaId) {
    this.marinaId = marinaId;
}

public void setDescription(String description) {
    this.description = description;
}

public void setBCor(float BCor) {
    this.BCor = BCor;
}

public void setLCor(float LCor) {
    this.LCor = LCor;
}

public void setSize(int size) {
    this.size = size;
}

public void setIsOpen(boolean open) {
    isOpen = open;
}
}

MarinaInfoAdapter.class应该根据行布局将数据放入我的ListView中。

public class MarinaInfoAdapter extends ArrayAdapter {
List list = new ArrayList();
public MarinaInfoAdapter(Context context, int resource) {
    super(context, resource);
}

@Override
public void add(Object object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();

}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row;
    row = convertView;
    MarinaInfoHolder marinaInfoHolder;

    if (row==null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout, parent, false);
        marinaInfoHolder = new MarinaInfoHolder();

        marinaInfoHolder.tx_availablePlace = (TextView) row.findViewById(R.id
                .tx_availablePlace);
        marinaInfoHolder.tx_description = (TextView) row.findViewById(R.id.tx_description);
        row.setTag(marinaInfoHolder); }
    else {
        marinaInfoHolder = (MarinaInfoHolder) row.getTag();

    }

    MarinaInfo marinaInfo = (MarinaInfo) this.getItem(position);
    marinaInfoHolder.tx_availablePlace.setText(marinaInfo.getAvailablePlace());
    marinaInfoHolder.tx_description.setText(marinaInfo.getDescription());
    return row;
}

static class MarinaInfoHolder {
    TextView tx_description, tx_availablePlace;
}
}

3 个答案:

答案 0 :(得分:2)

根据您发布的代码,您只是不将适配器设置为列表视图。

做:

listView.setAdapter(marinaInfoAdapter);

答案 1 :(得分:0)

我想也许你需要在while循环后填充列表后调用下面的函数。

 listView.invalidateViews();
 listView.refreshDrawableState();

希望它有所帮助。

答案 2 :(得分:0)

谢谢你们,伙计们。你的提示非常有用。我做了一些测试,发现了2个错误。在MarinaInfoAdapter.class中,我尝试在TextView对象中设置一个整数值。

也有问题
  

isOpen = JO.getBoolean(&#34; is_open&#34;)。

在JSON数据中,is_open属性有0或1个值,所以我不能使用Boolean作为isOpen参数的类型。在这个地方我的try-block返回异常。删除这段代码后,一切都很好。