ListView带有来自外部数据库的数据

时间:2021-10-16 09:52:37

I'm trying catch data from my JSON file and put them in ListView. My source code was compliated correctly, buy my ListView is empty and I don't have any idea why.

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

I created DisplayMarinasListView.class which gets a JSON_STRING data from URL: http://gypscom.000webhostapp.com/jsongetmarinadata.php. In this class layout, I have only ListView.

我创建了DisplayMarinasListView。类,从URL: http://gypscom.000webhostapp.com/jsongetmarinadata.php获取JSON_STRING数据。在这个类布局中,我只有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 catches data from String with JSON data

MarinaInfo。类通过JSON数据捕获字符串中的数据

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 should put data in my ListView based on row layout.

MarinaInfoAdapter。类应该根据行布局将数据放在我的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 个解决方案

#1


2  

Basing on the code you posted, you're simply not setting the adapter to the listview.

基于您发布的代码,您不需要将适配器设置为listview。

Do:

做的事:

listView.setAdapter(marinaInfoAdapter);

#2


0  

I think maybe You need to call functions below after your populate the list after while loop.

我认为你可能需要在你填充完列表之后调用下面的函数。

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

Hope it Helps.

希望它可以帮助。

#3


0  

Thank you, guys. Yours tips are very useful. I made a few tests and I found 2 mistakes. In MarinaInfoAdapter.class I tried to set an integer value in TextView object. There also was problem with

谢谢你,伙计。你的建议很有用。我做了一些测试,发现了两个错误。在MarinaInfoAdapter。类I尝试在TextView对象中设置一个整数值。还有一个问题

isOpen = JO.getBoolean("is_open").

isOpen = JO.getBoolean(“is_open”)。

In the JSON data is_open attribute has 0 or 1 value, so I cannot use Boolean as a type of isOpen parameter. In this place my try-block return exception. After removed this block of code, everything was fine.

在JSON数据is_open属性中,值为0或1,因此不能将布尔值用作isOpen参数的类型。在这里,我的try-block返回异常。删除这段代码之后,一切都很好。

#1


2  

Basing on the code you posted, you're simply not setting the adapter to the listview.

基于您发布的代码,您不需要将适配器设置为listview。

Do:

做的事:

listView.setAdapter(marinaInfoAdapter);

#2


0  

I think maybe You need to call functions below after your populate the list after while loop.

我认为你可能需要在你填充完列表之后调用下面的函数。

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

Hope it Helps.

希望它可以帮助。

#3


0  

Thank you, guys. Yours tips are very useful. I made a few tests and I found 2 mistakes. In MarinaInfoAdapter.class I tried to set an integer value in TextView object. There also was problem with

谢谢你,伙计。你的建议很有用。我做了一些测试,发现了两个错误。在MarinaInfoAdapter。类I尝试在TextView对象中设置一个整数值。还有一个问题

isOpen = JO.getBoolean("is_open").

isOpen = JO.getBoolean(“is_open”)。

In the JSON data is_open attribute has 0 or 1 value, so I cannot use Boolean as a type of isOpen parameter. In this place my try-block return exception. After removed this block of code, everything was fine.

在JSON数据is_open属性中,值为0或1,因此不能将布尔值用作isOpen参数的类型。在这里,我的try-block返回异常。删除这段代码之后,一切都很好。