[英]Android:How do I display data from multiple array of strings in a local JSON file?
我一直在嘗試從本地 JSON 文件填充我的 listView。 JSON 文件有多個字符串數組,所以我不得不多次使用 for 循環,但現在的問題是,當我運行我的應用程序時,它只重復第一個 JSON 對象(id1 即 i=1)的細節,而忽略了休息。 任何幫助將非常感激。
編輯:我認為 for 循環以某種方式相互干擾。 我只是不知道怎么做,但我嘗試了很多不同的事情,結果仍然沒有改變。 如果沒有第三個和第二個和第三個 for 循環,代碼將返回所有對象(即 i=0,1,2,3,4),但添加這些循環以訪問其他數組僅顯示 i=0 的所有情況的元素一世。 這是我的 JSON:
[{
"id": 1,
"start_year": 1990,
"end_year": 2010,
"gender": "male",
"countries": ["China", "South Africa", "france", "Mexico", "Japan", "Estonia", "Colombia", "China"],
"colors": ["Green", "Violet", "Yellow", "Blue", "Teal", "Maroon", "Red", "Aquamarine", "Orange", "Mauv"]
}, {
"id": 2,
"start_year": 1990,
"end_year": 2010,
"gender": "",
"countries": ["China", "South Africa", "france", "Mexico", "Japan", "Estonia", "Colombia", "China"],
"colors": ["Green", "Violet", "Yellow", "Blue", "Teal", "Maroon", "Red", "Aquamarine", "Orange", "Mauv"]
}, {
"id": 3,
"start_year": 1980,
"end_year": 2002,
"gender": "female",
"countries": [],
"colors": ["Green", "Violet", "Yellow", "Blue", "Teal", "Maroon", "Red", "Aquamarine", "Orange", "Mauv"]
}, {
"id": 4,
"start_year": 1990,
"end_year": 2000,
"gender": "",
"countries": [],
"colors": []
}, {
"id": 5,
"start_year": 1990,
"end_year": 2009,
"gender": "",
"countries": ["China", "South Africa", "france", "Mexico", "Japan", "Estonia", "Colombia", "China"],
"colors": []
}]
這是我的 MainActivity.java:
package com.android.oghenemaroafenogho;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Filter> filtersList = new ArrayList<Filter>();
private FiltersAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView filtersListView = findViewById(R.id.list);
mAdapter = new FiltersAdapter(this, new ArrayList<Filter>());
filtersListView.setAdapter(mAdapter);
getJsonData();
}
// JSON method to get JSON locally
public ArrayList<Filter> getJsonData() {
String json;
try {
InputStream venten = getAssets().open("assessment.json");
int size = venten.available();
byte[] buffer = new byte[size];
venten.read(buffer);
venten.close();
json = new String(buffer, "UTF-8");
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int startYear = jsonObject.getInt("start_year");
int endYear = jsonObject.getInt("end_year");
String gender = jsonObject.getString("gender");
if (gender.length() == 0) {
gender = "no gender";
}
JSONArray countryArray = jsonObject.getJSONArray("countries");
for (int j = 0; j < countryArray.length(); j++) {
String country = countryArray.getString(j);
if (countryArray.length() == 0) {
country = "No country found";
}
JSONArray colorArray = jsonObject.getJSONArray("colors");
for (int n = 0; n < colorArray.length(); n++) {
String color = colorArray.getString(n);
if (colorArray.length() == 0) {
color = "No color found";
}
Filter carFilters = new Filter(startYear, endYear, gender, country, color);
filtersList.add(carFilters);
if (carFilters != null) {
mAdapter.addAll(carFilters);
}
}
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return filtersList;
}
}
過濾器適配器:
package com.android.oghenemaroafenogho;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
public class FiltersAdapter extends ArrayAdapter<Filter> {
public FiltersAdapter(Context context, ArrayList<Filter> filters) {
super(context, 0, filters);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null) {
listView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
Filter currentFilter = getItem(position);
TextView startYear = listView.findViewById(R.id.start_year);
startYear.setText(Integer.toString(currentFilter.getYear()));
TextView endYear = listView.findViewById(R.id.end_year);
endYear.setText(Integer.toString(currentFilter.getEndYear()));
TextView gender = listView.findViewById(R.id.gender);
gender.setText(currentFilter.getGender());
TextView country = listView.findViewById(R.id.country);
country.setText(currentFilter.getCountry());
TextView color = listView.findViewById(R.id.color);
color.setText(currentFilter.getColor());
}
return listView;
}
}
嘗試
mAdapter = new FiltersAdapter(this, getJsonData());
filtersListView.setAdapter(mAdapter);
代替
mAdapter = new FiltersAdapter(this, new ArrayList<Filter>());
filtersListView.setAdapter(mAdapter);
getJsonData();
並且不要忘記刪除此塊
if (carFilters != null) {
mAdapter.addAll(carFilters);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.