解析来自 JSON 的数据后,Recyclerview 中的数据未显示
Data in Recyclerview is not showing after parsing data from JSON
我正在尝试从本地资产文件夹中解析 JSON 对象。我有一个数组名称联系人。在公司内部,我有对象行名、电子邮件、手机等。我尝试使用以下代码。但是在 运行 应用程序之后我得到了空页面。我没有弄错哪一行。我的模型 class 看起来像这样。
public class MyColleageModel {
private String _id;
private String dn;
private String mobile;
private String mail;
private String name ;
private String company ;
private String department;
private String title ;
private int __v ;
private String updated_at;
public MyColleageModel() {
}
public MyColleageModel(String mobile, String mail, String name, String company, String department, String title) {
this.mobile = mobile;
this.mail = mail;
this.name = name;
this.company = company;
this.department = department;
this.title = title;
}
public String get_id() {
return _id;
}
public String getDn() {
return dn;
}
public String getMobile() {
return mobile;
}
public String getMail() {
return mail;
}
public String getName() {
return name;
}
public String getCompany() {
return company;
}
public String getDepartment() {
return department;
}
public String getTitle() {
return title;
}
public int get__v() {
return __v;
}
public String getUpdated_at() {
return updated_at;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void setMail(String mail) {
this.mail = mail;
}
public void setName(String name) {
this.name = name;
}
public void setCompany(String company) {
this.company = company;
}
public void setDepartment(String department) {
this.department = department;
}
public void setTitle(String title) {
this.title = title;
}
}
我的主要 Activity Class 是
public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
colleagueObject = new ArrayList<MyColleageModel>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
my_clg=new MyColleageModel();
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.getString("company");
String val5 = jo_inside.getString("department");
String val6 = jo_inside.optString("title");
//Add your values in your `ArrayList` as below:
m_li = new HashMap<String, String>();
m_li.put("mobile", val1);
m_li.put("mail", val2);
m_li.put("name", val3);
m_li.put("company", val4);
m_li.put("department", val5);
m_li.put("title", val6);
colleagueObject.add(my_clg);
formList.add(m_li);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new MyColleaguesAdapter(colleagueObject, this);
recyclerView.setAdapter(adapter);
}
我的 JSON 文件看起来像这样-
{
"contacts":[
{
"_id": "57f762f1aa44f7230b8f5bb0",
"dn": "CN=Andreas Oswald,OU=Mitarbeiter,OU=Kelten,DC=blu-pa,DC=com",
"mobile": "+491733630322",
"mail": "andreas.oswald@blu-pa.com",
"name": "Andreas Oswald",
"company": "blu Portals & Applications GmbH",
"department": "Roche",
"title": "externer Mitarbeiter",
"__v": 0,
"updated_at": "2016-10-07T08:55:13.909Z"
},
{
"_id": "57f762f2aa44f7230b8f5bb1",
"dn": "CN=Bernard Landgraf,OU=Mitarbeiter,OU=Kelten,DC=blu-pa,DC=com",
"mobile": "+4917610660687",
"mail": "bernard.landgraf@blu-pa.com",
"name": "Bernard Landgraf",
"company": "blu Portals & Applications GmbH",
"department": "Inhouse",
"__v": 0,
"updated_at": "2016-10-07T08:55:13.935Z"
},
{ ........lot of object similar like the above 2
我的适配器Class
public class MyColleaguesAdapter extends RecyclerView.Adapter<MyColleaguesAdapter.ColleagueHolder> {
private List<MyColleageModel> colleagueObject;
private Context context;
public MyColleaguesAdapter(List<MyColleageModel> colleagueObject, Context context) {
this.colleagueObject = colleagueObject;
this.context = context;
}
@Override
public ColleagueHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.colleage_row_layout,parent,false);
return new ColleagueHolder(view);
}
@Override
public void onBindViewHolder(ColleagueHolder holder, int position) {
MyColleageModel myColleague=colleagueObject.get(position);
//holder.colleaguePicture.setImageResource(myColleague.getImageResourceId());
holder.mobile.setText(myColleague.getMobile());
holder.mail.setText(myColleague.getMail());
holder.name.setText(myColleague.getName());
holder.company.setText(myColleague.getCompany());
holder.department.setText(myColleague.getDepartment());
holder.title.setText(myColleague.getTitle());
}
@Override
public int getItemCount() {
return colleagueObject.size();
}
public class ColleagueHolder extends RecyclerView.ViewHolder{
public ImageView colleaguePicture;
public TextView mobile;
public TextView mail;
public TextView name;
public TextView company;
public TextView department;
public TextView title;
public ColleagueHolder(View itemView) {
super(itemView);
//colleaguePicture=(ImageView)itemView.findViewById(R.drawable.profile_image);
mobile=(TextView)itemView.findViewById(R.id.colleague_mobile);
mail=(TextView) itemView.findViewById(R.id.colleague_mail);
name=(TextView) itemView.findViewById(R.id.colleague_name);
company=(TextView)itemView.findViewById(R.id.company_name);
department=(TextView) itemView.findViewById(R.id.department_name);
title=(TextView) itemView.findViewById(R.id.job_title);
}
}
}
查看代码。
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.getString("company");
String val5 = jo_inside.getString("department");
String val6 = jo_inside.optString("title");
my_clg = new MyColleageModel(val1,val2,val3,val4,val5,val6);
// here set the data on my_clg object using setters methods.
// here add this object to list.
colleagueObject.add(my_clg);
formList.add(m_li);
}
public class MyColleaguesAdapter extends RecyclerView.Adapter {
private View view;
Context context;
List<MyColleageModel> colleagueObject
public MyColleaguesAdapter(List<MyColleageModel> colleagueObject, Context context) {
this.colleagueObject = colleagueObject;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.colleage_row_layout,parent,false);
return new ColleagueHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder1, int position) {
final ColleagueHolder holder= (ColleagueHolder ) holder;
MyColleageModel myColleague=colleagueObject.get(position);
holder.mobile.setText(myColleague.getMobile());
holder.mail.setText(myColleague.getMail());
holder.name.setText(myColleague.getName());
holder.company.setText(myColleague.getCompany());
holder.department.setText(myColleague.getDepartment());
holder.title.setText(myColleague.getTitle());
}
@Override
public int getItemCount() {
return colleagueObject.size();
}
public class ColleagueHolder extends RecyclerView.ViewHolder{
public ImageView colleaguePicture;
public TextView mobile;
public TextView mail;
public TextView name;
public TextView company;
public TextView department;
public TextView title;
public ColleagueHolder(View itemView) {
super(itemView);
//colleaguePicture=(ImageView)itemView.findViewById(R.drawable.profile_image);
mobile=(TextView)itemView.findViewById(R.id.colleague_mobile);
mail=(TextView) itemView.findViewById(R.id.colleague_mail);
name=(TextView) itemView.findViewById(R.id.colleague_name);
company=(TextView)itemView.findViewById(R.id.company_name);
department=(TextView) itemView.findViewById(R.id.department_name);
title=(TextView) itemView.findViewById(R.id.job_title);
}
}
}
尝试此代码,如果它没有解决您的问题,那么 post 您的 xml
非常感谢你帮我解决了我的问题。这里我修改了正确答案。我在 Main Activity Class.
中遇到问题
public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
colleagueObject = new ArrayList<MyColleageModel>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
//my_clg=new MyColleageModel();
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.optString("company");
String val5 = jo_inside.optString("department");
String val6 = jo_inside.optString("title");
my_clg = new MyColleageModel(val1,val2,val3,val4,val5,val6);
colleagueObject.add(my_clg);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new MyColleaguesAdapter(colleagueObject, this);
recyclerView.setAdapter(adapter);
}
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("colleagues.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;
}
}
我正在尝试从本地资产文件夹中解析 JSON 对象。我有一个数组名称联系人。在公司内部,我有对象行名、电子邮件、手机等。我尝试使用以下代码。但是在 运行 应用程序之后我得到了空页面。我没有弄错哪一行。我的模型 class 看起来像这样。
public class MyColleageModel {
private String _id;
private String dn;
private String mobile;
private String mail;
private String name ;
private String company ;
private String department;
private String title ;
private int __v ;
private String updated_at;
public MyColleageModel() {
}
public MyColleageModel(String mobile, String mail, String name, String company, String department, String title) {
this.mobile = mobile;
this.mail = mail;
this.name = name;
this.company = company;
this.department = department;
this.title = title;
}
public String get_id() {
return _id;
}
public String getDn() {
return dn;
}
public String getMobile() {
return mobile;
}
public String getMail() {
return mail;
}
public String getName() {
return name;
}
public String getCompany() {
return company;
}
public String getDepartment() {
return department;
}
public String getTitle() {
return title;
}
public int get__v() {
return __v;
}
public String getUpdated_at() {
return updated_at;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public void setMail(String mail) {
this.mail = mail;
}
public void setName(String name) {
this.name = name;
}
public void setCompany(String company) {
this.company = company;
}
public void setDepartment(String department) {
this.department = department;
}
public void setTitle(String title) {
this.title = title;
}
}
我的主要 Activity Class 是
public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
colleagueObject = new ArrayList<MyColleageModel>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
my_clg=new MyColleageModel();
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.getString("company");
String val5 = jo_inside.getString("department");
String val6 = jo_inside.optString("title");
//Add your values in your `ArrayList` as below:
m_li = new HashMap<String, String>();
m_li.put("mobile", val1);
m_li.put("mail", val2);
m_li.put("name", val3);
m_li.put("company", val4);
m_li.put("department", val5);
m_li.put("title", val6);
colleagueObject.add(my_clg);
formList.add(m_li);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new MyColleaguesAdapter(colleagueObject, this);
recyclerView.setAdapter(adapter);
}
我的 JSON 文件看起来像这样-
{
"contacts":[
{
"_id": "57f762f1aa44f7230b8f5bb0",
"dn": "CN=Andreas Oswald,OU=Mitarbeiter,OU=Kelten,DC=blu-pa,DC=com",
"mobile": "+491733630322",
"mail": "andreas.oswald@blu-pa.com",
"name": "Andreas Oswald",
"company": "blu Portals & Applications GmbH",
"department": "Roche",
"title": "externer Mitarbeiter",
"__v": 0,
"updated_at": "2016-10-07T08:55:13.909Z"
},
{
"_id": "57f762f2aa44f7230b8f5bb1",
"dn": "CN=Bernard Landgraf,OU=Mitarbeiter,OU=Kelten,DC=blu-pa,DC=com",
"mobile": "+4917610660687",
"mail": "bernard.landgraf@blu-pa.com",
"name": "Bernard Landgraf",
"company": "blu Portals & Applications GmbH",
"department": "Inhouse",
"__v": 0,
"updated_at": "2016-10-07T08:55:13.935Z"
},
{ ........lot of object similar like the above 2
我的适配器Class
public class MyColleaguesAdapter extends RecyclerView.Adapter<MyColleaguesAdapter.ColleagueHolder> {
private List<MyColleageModel> colleagueObject;
private Context context;
public MyColleaguesAdapter(List<MyColleageModel> colleagueObject, Context context) {
this.colleagueObject = colleagueObject;
this.context = context;
}
@Override
public ColleagueHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.colleage_row_layout,parent,false);
return new ColleagueHolder(view);
}
@Override
public void onBindViewHolder(ColleagueHolder holder, int position) {
MyColleageModel myColleague=colleagueObject.get(position);
//holder.colleaguePicture.setImageResource(myColleague.getImageResourceId());
holder.mobile.setText(myColleague.getMobile());
holder.mail.setText(myColleague.getMail());
holder.name.setText(myColleague.getName());
holder.company.setText(myColleague.getCompany());
holder.department.setText(myColleague.getDepartment());
holder.title.setText(myColleague.getTitle());
}
@Override
public int getItemCount() {
return colleagueObject.size();
}
public class ColleagueHolder extends RecyclerView.ViewHolder{
public ImageView colleaguePicture;
public TextView mobile;
public TextView mail;
public TextView name;
public TextView company;
public TextView department;
public TextView title;
public ColleagueHolder(View itemView) {
super(itemView);
//colleaguePicture=(ImageView)itemView.findViewById(R.drawable.profile_image);
mobile=(TextView)itemView.findViewById(R.id.colleague_mobile);
mail=(TextView) itemView.findViewById(R.id.colleague_mail);
name=(TextView) itemView.findViewById(R.id.colleague_name);
company=(TextView)itemView.findViewById(R.id.company_name);
department=(TextView) itemView.findViewById(R.id.department_name);
title=(TextView) itemView.findViewById(R.id.job_title);
}
}
}
查看代码。
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.getString("company");
String val5 = jo_inside.getString("department");
String val6 = jo_inside.optString("title");
my_clg = new MyColleageModel(val1,val2,val3,val4,val5,val6);
// here set the data on my_clg object using setters methods.
// here add this object to list.
colleagueObject.add(my_clg);
formList.add(m_li);
}
public class MyColleaguesAdapter extends RecyclerView.Adapter {
private View view;
Context context;
List<MyColleageModel> colleagueObject
public MyColleaguesAdapter(List<MyColleageModel> colleagueObject, Context context) {
this.colleagueObject = colleagueObject;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.colleage_row_layout,parent,false);
return new ColleagueHolder(view);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder1, int position) {
final ColleagueHolder holder= (ColleagueHolder ) holder;
MyColleageModel myColleague=colleagueObject.get(position);
holder.mobile.setText(myColleague.getMobile());
holder.mail.setText(myColleague.getMail());
holder.name.setText(myColleague.getName());
holder.company.setText(myColleague.getCompany());
holder.department.setText(myColleague.getDepartment());
holder.title.setText(myColleague.getTitle());
}
@Override
public int getItemCount() {
return colleagueObject.size();
}
public class ColleagueHolder extends RecyclerView.ViewHolder{
public ImageView colleaguePicture;
public TextView mobile;
public TextView mail;
public TextView name;
public TextView company;
public TextView department;
public TextView title;
public ColleagueHolder(View itemView) {
super(itemView);
//colleaguePicture=(ImageView)itemView.findViewById(R.drawable.profile_image);
mobile=(TextView)itemView.findViewById(R.id.colleague_mobile);
mail=(TextView) itemView.findViewById(R.id.colleague_mail);
name=(TextView) itemView.findViewById(R.id.colleague_name);
company=(TextView)itemView.findViewById(R.id.company_name);
department=(TextView) itemView.findViewById(R.id.department_name);
title=(TextView) itemView.findViewById(R.id.job_title);
}
}
}
尝试此代码,如果它没有解决您的问题,那么 post 您的 xml
非常感谢你帮我解决了我的问题。这里我修改了正确答案。我在 Main Activity Class.
中遇到问题public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
recyclerView = (RecyclerView) findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
colleagueObject = new ArrayList<MyColleageModel>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
MyColleageModel my_clg;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
//my_clg=new MyColleageModel();
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.optString("company");
String val5 = jo_inside.optString("department");
String val6 = jo_inside.optString("title");
my_clg = new MyColleageModel(val1,val2,val3,val4,val5,val6);
colleagueObject.add(my_clg);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter = new MyColleaguesAdapter(colleagueObject, this);
recyclerView.setAdapter(adapter);
}
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("colleagues.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;
}
}