JSONファイルからキャッチデータを取得してListViewに配置しようとしています。私のソースコードは正しく整理されていました。私のListViewは空であり、私は理由が分かりません。ListViewと外部データベースのデータ
URL:http://gypscom.000webhostapp.com/jsongetmarinadata.phpからJSON_STRINGデータを取得するDisplayMarinasListView.classを作成しました。このクラスのレイアウトでは、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は
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にデータを置くべきJSONデータを文字列からデータをキャッチします。
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;
}
}