私はアンドロイドプロジェクトに取り組んでおり、問題に直面しています。問題は次のとおりです。
Arraylist を返すと空になります。
ここに私のJavaコードがあります:
ArrayList<ArrayList<Object>> container = new ArrayList<ArrayList<Object>>();
ArrayList<Object> itemRow = new ArrayList<Object>();
JSONObject jsonObj = new JSONObject(result);
JSONArray allElements = jsonObj.getJSONArray("Table");
Log.i("allElements", "" + allElements);
for (int i = 0; i < allElements.length(); i++) {
itemRow.add(allElements.getJSONObject(i).getString("ParentName").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentEmailID").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentContact").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentAddress").toString());
itemRow.add(allElements.getJSONObject(i).getString("ParentProfilePictureName").toString());
itemRow.add(allElements.getJSONObject(i).getString("StudentName").toString());
Log.i("itemRow", "itemRow at index: " + i + ", " + itemRow);
container.add(((i*2)/2), itemRow);
itemRow.clear();
}
return container;
このコードでは、すべての要素を格納するための Arraylist と、単一行の要素を格納するための Arraylist を 2 つ用意しています。これらの Arraylist は JSONArray からロードされ、すべて正常に動作しており、アイテム行 (単一行を取る Arraylist) からデータを出力し、メインの Arraylist (コンテナー) に格納できます。
しかし、この Arraylist (コンテナー) を返して logcat に出力すると、次のような空の Arraylist が表示されます
[[], [], [], [], []].
なぜこれが起こるのか理解できません。この問題を解決するのを手伝ってください。
ありがとう。