我正在将包含值的数组列表传递给另一个活动 传递的数组列表包含以下值:
"Details": [
{
"ID": 1,
"Name": "ABC",
"Supervisor": [
{
"supervisorID": 1,
"Name": "XYZ"
}
]
}
]
我想从中获取主管的详细信息,并在回收者视图中显示我如何实现此目的
ArrayList<String> myList = (ArrayList<String>)
getIntent().getSerializableExtra("site");
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,
LinearLayoutManager.VERTICAL));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
答案 0 :(得分:0)
创建带有变量名的模型类作为您的需求,并向我展示获取响应的函数 您可以在其中使用JSON对象和字符串键进行解析 像这样
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(JSON_STRING);
// fetch JSONObject named employee
JSONObject employee = obj.getJSONObject("employee");
// get employee name and salary
name = employee.getString("name");
salary = employee.getString("salary");
// set employee name and salary in TextView's
employeeName.setText("Name: "+name);
employeeSalary.setText("Salary: "+salary);
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
完整的代码
公共类MainActivity扩展了AppCompatActivity {
// ArrayList for person names, email Id's and mobile numbers
ArrayList<String> personNames = new ArrayList<>();
ArrayList<String> emailIds = new ArrayList<>();
ArrayList<String> mobileNumbers = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
// set a LinearLayoutManager with default vertical orientation
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
try {
// get JSONObject from JSON file
JSONObject obj = new JSONObject(loadJSONFromAsset());
// fetch JSONArray named users
JSONArray userArray = obj.getJSONArray("users");
// implement for loop for getting users list data
for (int i = 0; i < userArray.length(); i++) {
// create a JSONObject for fetching single user data
JSONObject userDetail = userArray.getJSONObject(i);
// fetch email and name and store it in arraylist
personNames.add(userDetail.getString("name"));
emailIds.add(userDetail.getString("email"));
// create a object for getting contact data from JSONObject
JSONObject contact = userDetail.getJSONObject("contact");
// fetch mobile number and store it in arraylist
mobileNumbers.add(contact.getString("mobile"));
}
} catch (JSONException e) {
e.printStackTrace();
}
// call the constructor of CustomAdapter to send the reference and data to Adapter
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, personNames, emailIds, mobileNumbers);
recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("users_list.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}