我实际上是使用Android的api获取数据的。我试图在回收者视图中显示获取的数据。我写了一个函数来获取数据,它返回一个通知列表。已获取数据,但仍返回空列表。
这是home_activity,其中从上一个活动传递了一些数据,这些数据用于检查用户是教职员工还是学生。 如果用户是学生,则使用学生适配器填充回收者视图;如果用户是教师,则使用教师适配器。
public class home_activity extends AppCompatActivity {
private String user_desig;
private RecyclerView recyclerView_student;
private RecyclerView recyclerView_teacher;
private StudentNoticeRecyclerViewAdapter studentNoticeRecyclerViewAdapter;
private TeacherNoticeRecyclerViewAdapter teacherNoticeRecyclerViewAdapter;
private List<Notice> notice_list;
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_main);
Intent i =getIntent();
User recieved_user = (User)i.getSerializableExtra("userObj");
user_desig = recieved_user.getUser_designation();
//Log.d("Designation",user_desig);
queue = Volley.newRequestQueue(home_activity.this);
if (user_desig.contentEquals("student")){
//Log.d("Designation",user_desig);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_student = (RecyclerView)findViewById(R.id.recycler_user);
recyclerView_student.setHasFixedSize(true);
recyclerView_student.setLayoutManager(layoutManager);
notice_list = new ArrayList<>();
notice_list = getStudentNotices();
Log.d("Response:",notice_list.toString());
studentNoticeRecyclerViewAdapter = new StudentNoticeRecyclerViewAdapter(this, notice_list);
recyclerView_student.setAdapter(studentNoticeRecyclerViewAdapter);
studentNoticeRecyclerViewAdapter.notifyDataSetChanged();
}
else{
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_teacher = (RecyclerView)findViewById(R.id.recycler_user);
recyclerView_teacher.setHasFixedSize(true);
recyclerView_teacher.setLayoutManager(layoutManager);
notice_list = new ArrayList<>();
notice_list = getTeacherNotices();
teacherNoticeRecyclerViewAdapter = new TeacherNoticeRecyclerViewAdapter(this,notice_list);
recyclerView_teacher.setAdapter(teacherNoticeRecyclerViewAdapter);
teacherNoticeRecyclerViewAdapter.notifyDataSetChanged();
}
}
public List<Notice> getStudentNotices(){
notice_list.clear();
JsonObjectRequest noticeStudent = new JsonObjectRequest(Request.Method.GET,
constants.STUDENT_NOTICE_API_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray listNotices = response.getJSONArray("studentNoticeList");
//Log.d("Response", listNotices.toString());
for (int i=0;i<listNotices.length();i++){
JSONObject jsonObject = listNotices.getJSONObject(i);
Notice noticeObjs = new Notice();
noticeObjs.setNotice_title(jsonObject.getString("s_notice_title"));
noticeObjs.setNotice_id(Integer.parseInt(jsonObject.getString("s_notice_id")));
noticeObjs.setNotice_content(jsonObject.getString("s_notice_content"));
noticeObjs.setUser_id('s');
//Log.d("Response Obj",noticeObjs.toString());
notice_list.add(noticeObjs);
//Log.d("Response Object",notice_list.toString());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error : " ,error.getMessage());
}
}
);
queue.add(noticeStudent);
//Log.d("Response return:",notice_list.toString());
return notice_list;
}
public List<Notice> getTeacherNotices(){
notice_list.clear();
JsonObjectRequest noticeTeacher = new JsonObjectRequest(Request.Method.GET,
constants.TEACHER_NOTICE_API_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray listNotices = response.getJSONArray("teacherNoticeList");
if(listNotices.length() > 0){
for(int i=0;i<listNotices.length();i++){
JSONObject jsonObject = listNotices.getJSONObject(i);
Notice noticeObjt = new Notice();
noticeObjt.setUser_id('t');
noticeObjt.setNotice_id(Integer.parseInt(jsonObject.getString("t_notice_id")));
noticeObjt.setNotice_title(jsonObject.getString("t_notice_title"));
noticeObjt.setNotice_content(jsonObject.getString("t_notice_content"));
notice_list.add(noticeObjt);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error : " ,error.getMessage());
}
}
);
queue.add(noticeTeacher);
return notice_list;
}
}
答案 0 :(得分:0)
渲染onResponse
时,您不必等待RecyclerView
回调。
将以下代码放在OnResponse()
teacherNoticeRecyclerViewAdapter = new TeacherNoticeRecyclerViewAdapter(this,notice_list);
recyclerView_teacher.setAdapter(teacherNoticeRecyclerViewAdapter);
teacherNoticeRecyclerViewAdapter.notifyDataSetChanged();
答案 1 :(得分:0)
通常在后台线程而不是主线程中获取数据,当您返回notice_list并立即返回notice_list并且其为空时,您可以将接口发送到获取数据的方法来处理响应,或者像这样调用onResponse方法:>
public void getTeacherNotices(){
notice_list.clear();
JsonObjectRequest noticeTeacher = new JsonObjectRequest(Request.Method.GET,
constants.TEACHER_NOTICE_API_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray listNotices = response.getJSONArray("teacherNoticeList");
if(listNotices.length() > 0){
for(int i=0;i<listNotices.length();i++){
JSONObject jsonObject = listNotices.getJSONObject(i);
Notice noticeObjt = new Notice();
noticeObjt.setUser_id('t');
noticeObjt.setNotice_id(Integer.parseInt(jsonObject.getString("t_notice_id")));
noticeObjt.setNotice_title(jsonObject.getString("t_notice_title"));
noticeObjt.setNotice_content(jsonObject.getString("t_notice_content"));
notice_list.add(noticeObjt);
}
handleTeacherNotices(notice_list)
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error : " ,error.getMessage());
}
}
);
queue.add(noticeTeacher);
}
private void handleTeacherNoticesResponse( List<Notice> notice_list){
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_teacher = (RecyclerView)findViewById(R.id.recycler_user);
recyclerView_teacher.setHasFixedSize(true);
recyclerView_teacher.setLayoutManager(layoutManager);
teacherNoticeRecyclerViewAdapter = new TeacherNoticeRecyclerViewAdapter(this,notice_list);
recyclerView_teacher.setAdapter(teacherNoticeRecyclerViewAdapter);
teacherNoticeRecyclerViewAdapter.notifyDataSetChanged();
}
答案 2 :(得分:0)
尝试
public class home_activity extends AppCompatActivity {
private String user_desig;
private RecyclerView recyclerView_student;
private RecyclerView recyclerView_teacher;
private StudentNoticeRecyclerViewAdapter studentNoticeRecyclerViewAdapter;
private TeacherNoticeRecyclerViewAdapter teacherNoticeRecyclerViewAdapter;
private List<Notice> notice_list;
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_main);
Intent i =getIntent();
User recieved_user = (User)i.getSerializableExtra("userObj");
user_desig = recieved_user.getUser_designation();
//Log.d("Designation",user_desig);
queue = Volley.newRequestQueue(home_activity.this);
if (user_desig.contentEquals("student")){
//Log.d("Designation",user_desig);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_student = (RecyclerView)findViewById(R.id.recycler_user);
recyclerView_student.setHasFixedSize(true);
recyclerView_student.setLayoutManager(layoutManager);
notice_list = new ArrayList<>();
studentNoticeRecyclerViewAdapter = new StudentNoticeRecyclerViewAdapter(this, notice_list);
recyclerView_student.setAdapter(studentNoticeRecyclerViewAdapter);
getStudentNotices();
}
else{
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView_teacher = (RecyclerView)findViewById(R.id.recycler_user);
recyclerView_teacher.setHasFixedSize(true);
recyclerView_teacher.setLayoutManager(layoutManager);
notice_list = new ArrayList<>();
teacherNoticeRecyclerViewAdapter = new TeacherNoticeRecyclerViewAdapter(this,notice_list);
recyclerView_teacher.setAdapter(teacherNoticeRecyclerViewAdapter);
getTeacherNotices();
}
}
public void getStudentNotices(){
notice_list.clear();
JsonObjectRequest noticeStudent = new JsonObjectRequest(Request.Method.GET,
constants.STUDENT_NOTICE_API_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray listNotices = response.getJSONArray("studentNoticeList");
//Log.d("Response", listNotices.toString());
for (int i=0;i<listNotices.length();i++){
JSONObject jsonObject = listNotices.getJSONObject(i);
Notice noticeObjs = new Notice();
noticeObjs.setNotice_title(jsonObject.getString("s_notice_title"));
noticeObjs.setNotice_id(Integer.parseInt(jsonObject.getString("s_notice_id")));
noticeObjs.setNotice_content(jsonObject.getString("s_notice_content"));
noticeObjs.setUser_id('s');
//Log.d("Response Obj",noticeObjs.toString());
notice_list.add(noticeObjs);
//Log.d("Response Object",notice_list.toString());
}
studentNoticeRecyclerViewAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error : " ,error.getMessage());
}
}
);
queue.add(noticeStudent);
//Log.d("Response return:",notice_list.toString());
}
public void getTeacherNotices(){
notice_list.clear();
JsonObjectRequest noticeTeacher = new JsonObjectRequest(Request.Method.GET,
constants.TEACHER_NOTICE_API_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray listNotices = response.getJSONArray("teacherNoticeList");
if(listNotices.length() > 0){
for(int i=0;i<listNotices.length();i++){
JSONObject jsonObject = listNotices.getJSONObject(i);
Notice noticeObjt = new Notice();
noticeObjt.setUser_id('t');
noticeObjt.setNotice_id(Integer.parseInt(jsonObject.getString("t_notice_id")));
noticeObjt.setNotice_title(jsonObject.getString("t_notice_title"));
noticeObjt.setNotice_content(jsonObject.getString("t_notice_content"));
notice_list.add(noticeObjt);
}
}
teacherNoticeRecyclerViewAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error : " ,error.getMessage());
}
}
);
queue.add(noticeTeacher);
return notice_list;
}
}