我在解析json Data和填充列表视图时遇到问题。
下面的示例是我正在尝试解析的json数据以及显示我想要实现的内容的其余示例代码。
[
{
"id": 123,
"name": "abcd",
"department": {
"name": "xyz",
"code": 4536
}
},
{
"id": 123,
"name": "abcd",
"department": {
"name": "xyz",
"code": 4536
}
}
]
这是我的主要活动类
public class MainActivity extends Activity {
private ListView employeeListView;
private Employee employees[];
private ArrayAdapter<Employee> employeeArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
employeeListView=(ListView)findViewById(R.id.employeeListView);
}
public void updateListView(){
String a="[{\"id\":123,\"name\":\"abcd\",\"department\":{\"name\":\"xyz\",\"code\":4536}},{\"id\":123,\"name\":\"abcd\",\"department\":{\"name\":\"xyz\",\"code\":4536}}]";
try{
JSONArray jsonArray=new JSONArray(a);
JSONObject jsonObject;
employees=new Employee[jsonArray.length()];
Employee employee;
Employee.Department department;
int x=0;
while(x<jsonArray.length()){
jsonObject=jsonArray.getJSONObject(x);
employee=new Employee();
department=employee.new Department();
employee.setCode(jsonObject.optInt("id"));
employee.setName(jsonObject.optString("name"));
JSONObject jsonDepartment=jsonObject.getJSONObject("department");
department.setDepartmentName(jsonDepartment.optString("name"));
department.setDepartmentCode(jsonDepartment.optInt("code"));
employee.setDepartment(department);
employees[x]=employee;
x++;
}
employeeArrayAdapter=new ArrayAdapter<Employee>(getApplicationContext(),android.R.layout.simple_list_item_1,employees);
employeeListView.setAdapter(employeeArrayAdapter);
}catch (Throwable t){
Toast.makeText(getApplicationContext(),t.toString(),Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onButtonClick(View view) {
updateListView();
}
}
这是我的pojo课程
public class Employee {
private int code;
private String name;
private Department department;
//getter setter for code, name and Department
public String toString(){
return this.name+" "+this.code;
}
public class Department{//inner class
int departmentCode;
String departmentName;
//getter setter for departmentCode and departmentName
}
}
OnButtonClick函数我正在调用updateListView,它解析json字符串并填充列表视图。这里的问题是我无法在列表视图中获取部门数据,而员工数据正在正确填充。
这是模拟器的片段,显示了运行代码后我得到的列表视图。
任何人都可以纠正我的错误。我如何才能显示部门数据?
答案 0 :(得分:0)
我希望以上步骤对您有所帮助。
在你的班级中,你使用相同的对象进行while循环。
employee=new Employee();
department=employee.new Department();
但它应该是
Employee employee=new Employee();
Department department=employee.new Department();
在你的while循环中。
答案 1 :(得分:0)
好吧,既然您正在使用默认的ArrayAdapter,那么您实际上无法控制太多...如果您想要很好地格式化所有内容并且能够真正控制自定义对象显示的内容(您可能会这样做,您需要为Employee对象创建一个自定义ArrayAdapter并从那里覆盖getView()方法。
但是,如果您现在只想解决此问题...您可以编辑您的员工覆盖toString()方法以包含部门数据,如下所示:
@Override
public String toString(){
String toDisplay = this.name + " " + this.code;
if(department != null) toDisplay += "\n" + department.toString();
return toDisplay;
}
然后在Department对象中覆盖toString:
@Override
public String toString(){
return departmentName;
}
此外,从面向对象的角度来看,员工似乎应该是部门的成员(而不是反向,这是你现在拥有的)...即。一个部门可以有很多员工,但是一个员工应该(通常)只有一个部门......至少那是我对它的看法......
无论哪种方式,祝你好运〜