我正尝试在从Web服务获得的recyclerview中显示学生的详细信息。该应用程序的运行方式是:在“活动”中打开学生列表,然后在与学生接触时打开另一个活动,该活动将在recyclerview中显示该特定学生的详细信息。
问题:
我有时在有效的数据上方或下方得到一个空的recyclerview项。我尝试致电StuDetailsDataAdapterClassList.clear();
,但无法解决问题。有时它可以正常工作,但我不明白为什么会这样。您的帮助对我很有价值。
public class StudentDetailsActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView recyclerView2;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.Adapter recyclerViewAdapter2;
LinearLayoutManager recyclerViewlayoutManager;
RequestQueue requestQueue ;
RequestQueue requestQueue2 ;
List<StudentDataModel> StuDetailsDataAdapterClassList;
String stuIDForParse;
StudentDataModel GetStuDataModel = new StudentDataModel();
.....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_personnel_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
collapsingToolbarLayout = (SubtitleCollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView4);
recyclerView2 =(RecyclerView)findViewById(R.id.recyclerView5);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
StuDetailsDataAdapterClassList=new ArrayList<>();
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView2.setLayoutManager(layoutManager);
//I have a horizontal recyclerview below the vertical recyclerview. This is the layout manager for that recyclerview.
recyclerView2.setNestedScrollingEnabled(false);
//Set collapse & expanded title color
collapsingToolbarLayout.setExpandedSubtitleTextColor(getResources().getColor(R.color.divider));
setSupportActionBar(toolbar);
//Enable back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
....
//clearing list
StuDetailsDataAdapterClassList.clear();
PIC__WEB_CALL();
STU_WEB_CALL();
}
public void STU_WEB_CALL(){
String HTTP_SERVER_URL= String.format("http://www.student.com/%1$s",stuIDForParse);
JsonArrayRequest jsArrRequest = new JsonArrayRequest
(Request.Method.GET, HTTP_SERVER_URL, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
RES_PER_PARSE_DATA_AFTER_WEBCALL(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
}){
};
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsArrRequest);
}
public void RES_PER_PARSE_DATA_AFTER_WEBCALL(JSONArray array){
for(int i = 0; i<array.length(); i++) {
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetStuDataModel.setDob(json.getString("Date_Of_Birth"));
GetStuDataModel.setStuName(json.getString("Name"));
GetStuDataModel.setGradeName(json.getString("Grade"));
//Set the title on collapsing toolbar
collapsingToolbarLayout.setTitle(GetStuDataModel.getFullName());
} catch (JSONException e) {
e.printStackTrace();
}
}
if (array.length() != 0) {
StuDetailsDataAdapterClassList.add(GetStuDataModel);
collapsingToolbarLayout.setSubtitle(GetStuDataModel.getPerTitle());
recyclerViewAdapter = new StudentDetailsAdapter(StuDetailsDataAdapterClassList, this);
recyclerView.setAdapter(recyclerViewAdapter);
}
}
public void Horizontal_RecyclerView_WEB_CALL(){
//....more code
}
适配器
public class StudentDetailsAdapter extends RecyclerView.Adapter<StudentDetailsAdapter.ViewHolder> {
Context context;
public List<StudentDataModel> dataModels;
public StudentDetailsAdapter(List<StudentDataModel> getDataAdapter, Context context){
super();
Log.i("LPLP","I'm here");
this.dataModels = getDataAdapter;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_details_card, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final StudentDetailsAdapter.ViewHolder viewHolder, final int position) {
final StudentDataModel dataAdapter = dataModels.get(position);
viewHolder.grade.setText(dataAdapter.getGradeName());
viewHolder.stuName.setText(dataAdapter.getStuName());
viewHolder.dob.setText(dataAdapter.getDob());
}
@Override
public int getItemCount() {
return dataModels.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView grade;
public TextView stuName;
public TextView dob;
ConstraintLayout constraintLayout;
public ViewHolder(View itemView) {
super(itemView);
grade=(TextView)itemView.findViewById(R.id.textViewGrade);
stuName=(TextView)itemView.findViewById(R.id.textViewStuName);
dob=(TextView)itemView.findViewById(R.id.textViewDOB)
constraintLayout=(ConstraintLayout)itemView.findViewById(R.id.constrainLayout);
}
}
}