[英]Create an Array string for table layout
我想從我的 php 傳遞一個數據列表來填充我的表格布局,我目前正在使用 volley 連接到我的服務器端。 我如何設置這個列表
我正在使用 codecrafters 庫,他們有一個關於如何填充對我有用的表格的教程,但問題是他們使用的項目是手動輸入的,而不是從服務器動態獲取的,因此在實現相同的數據代碼時遇到了問題來自服務器
他們的字符串看起來像這樣
static String[][] spaceProbes={
{"1","Pioneer","Chemical","Jupiter"},
{"2","Voyager","Plasma","Andromeda"},
{"3","Casini","Solar","Saturn"},
{"4","Spitzer","Anti-Matter","Andromeda"},
{"5","Apollo","Chemical","Moon"},
{"6","Curiosity","Solar","Mars"},
};
並使用它們將其填充到布局中
tableView.setDataAdapter(new SimpleTableDataAdapter(MainActivity.this, spaceProbes));
我目前正在使用這個數組字符串,但它只返回數組中的最后一項
try {
//converign the strign to json assary object
JSONArray array = new JSONArray(response);
//traversing through all teh objects
for (int i = 0; i < array.length(); i++) {
//getting product object from json array
JSONObject allClass = array.getJSONObject(i);
takin = new String[]{allClass.getString("numb"),
allClass.getString("name"),
allClass.getString("time")};
}
tableView.setDataAdapter(new SimpleTableDataAdapter(AttendanceActivity.this, Collections.singletonList(takin)));
} catch (JSONException e) {
e.printStackTrace();
}
我如何重新創建與 spaceProbes 相同的結構,但這次是從服務器
每次執行時都會重置takin
的值
takin = new String[]{allClass.getString("numb"),
allClass.getString("name"),
allClass.getString("time")};
相反,試試這個,
String[][] takin = new String[array.length()][3]
for (int i = 0; i < array.length(); i++) {
JSONObject allClass = array.getJSONObject(i);
takin[i] = new String[]{allClass.getString("numb"),
allClass.getString("name"),
allClass.getString("time")};
}
tableView.setDataAdapter(new SimpleTableDataAdapter(AttendanceActivity.this, Collections.singletonList(takin)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.