我正在尝试从本地资源文件夹解析JSON对象。我有一个阵列名称联系人。在公司内部我有对象行名称,电子邮件,移动等。我尝试过以下代码。但在运行应用程序后,我得到了空的页面。我没有得到我错误的哪一行。我的模型类看起来像这样。
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;
}
}
我的主要活动类是
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
我的适配器类
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);
}
}
}
答案 0 :(得分:0)
参见代码。
throw er; // Unhandled 'error' event
Error: addMembership ENODEV
答案 1 :(得分:0)
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);
}
}
}
尝试此代码,如果它没有解决您的问题,请发布您的xml
答案 2 :(得分:0)
非常感谢您帮助我解决问题。在这里,我修改了正确的答案。我在主要活动类中遇到了问题。
public class TestDbContext : IdentityDbContext<User>
{
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{
}
}
public class User : IdentityUser
{
}
[Fact]
public void Test1()
{
var builder = new DbContextOptionsBuilder<TestDbContext>();
builder.UseInMemoryDatabase();
var context = new TestDbContext(builder.Options);
}