You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Volley从MySQL取数并通过数组填充表格?

Fixing Volley Data Population to TableView

Hey there! I see the issue with your current code—right now, you're overwriting the data array every time you loop through the JSON response, so only the last row of data ends up showing in your table. Let's fix that so all your MySQL data gets loaded properly.

The Problem in Your Current Code

In each iteration of the loop, you're creating a new String[][] array with just the current row:

data = new String[][]{{j, name, email, phone}};

This replaces the entire array each time, so by the end of the loop, data only holds the last entry from your JSON response. Plus, setting the adapter inside the loop (implied by your snippet) is inefficient and can cause unexpected UI glitches.

Corrected Code Example

Here's how to adjust your code to store all rows and set the adapter only once:

// Get the JSON result array from the response
JSONArray result = response.getJSONArray("result");
// Initialize a 2D array with the exact length of your data
String[][] data = new String[result.length()][4]; // 4 columns: index, name, email, phone

for (int i = 0; i < result.length(); i++) {
    JSONObject jsonObject = result.getJSONObject(i);
    // Extract values from the JSONObject
    String name = jsonObject.getString("name");
    String phone = jsonObject.getString("phone");
    String email = jsonObject.getString("email");
    
    // Assign values to the current row in the data array
    data[i][0] = String.valueOf(i + 1); // Start index at 1 for better readability
    data[i][1] = name;
    data[i][2] = email;
    data[i][3] = phone;
}

// Set the adapter AFTER populating all data (only once!)
tableView.setDataAdapter(new TableDataAdapter(getContext(), data));

Alternative: Using ArrayList for Flexibility

If you prefer a more flexible approach (perfect if your data length might vary unexpectedly), use an ArrayList to collect rows first, then convert it to a 2D array:

ArrayList<String[]> dataList = new ArrayList<>();
JSONArray result = response.getJSONArray("result");

for (int i = 0; i < result.length(); i++) {
    JSONObject jsonObject = result.getJSONObject(i);
    String name = jsonObject.getString("name");
    String phone = jsonObject.getString("phone");
    String email = jsonObject.getString("email");
    
    // Add each row to the ArrayList
    dataList.add(new String[]{String.valueOf(i + 1), name, email, phone});
}

// Convert ArrayList to 2D array
String[][] data = dataList.toArray(new String[dataList.size()][]);
// Set the adapter
tableView.setDataAdapter(new TableDataAdapter(getContext(), data));

Key Tips to Avoid Issues

  • Handle Null Values: Use jsonObject.optString("name") instead of getString() if your MySQL fields might be null—this prevents JSONException crashes.
  • Match Column Counts: Make sure the number of columns in your data array matches the number of columns defined in your TableView.
  • UI Thread Safety: Volley's response callback runs on the main thread, so you don't need to use runOnUiThread() to update the TableView—this is already safe.

内容的提问来源于stack exchange,提问作者Rahul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:30:27