从firebase数据库检索数据并存储在数组列表中

我试图以Android应用程序的forms创建一个小课程聚合器程序。

我的课程都存储在Firebase实时数据库中,可以从firebase控制台查看,一切都很好。

问题是我编写了一个java方法来连接数据库,从数据库中检索数据,将数据转换为自定义Java对象Course ,将其附加到另一个自定义Java对象CourseCardModel ,然后将CourseCardModel对象保存到数组列表。

与数据库的连接成功完成,并且它生成的快照包含正确的信息,我已通过循环快照并成功打印每个Course对象的变量Course Name来validation这些信息。 问题是当方法完成时,由于某种原因,arrayList返回为空,即使在整个快照迭代中检查ArrayList的大小时,我也可以看到CardModel对象被添加到它。

如果有人帮助解决这个问题,那将非常感激,我对Firebase来说相当新。

GenerateCourses()方法

 private ArrayList generateCourseCards() { courseCardModelList = new ArrayList(); cardModel = new CourseCardModel(); dbref = FirebaseDatabase.getInstance().getReference().child("courses"); // Retrieve the course data from firebase db and cast as Course object dbref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { Log.e("Count " ,"" + snapshot.getChildrenCount()); for (DataSnapshot postSnapshot: snapshot.getChildren()) { c = postSnapshot.getValue(Course.class); System.out.println("COURSE INFO: " + c.getCourseName()); cardModel.setCourse(c); courseCardModelList.add(cardModel); System.out.println("COURSE CARD MODEL LIST SIZE: " + courseCardModelList.size()); } } @Override public void onCancelled(DatabaseError databaseError) { Log.e("The read failed: ", databaseError.getMessage()); } }); System.out.print("END OF METHOD ARRAY SIZE CHECK: " + courseCardModelList.size()); return courseCardModelList; } 

LOGCAT输出

 01-11 09:57:57.105 28709-28780/coursematch.coursematchuk D/FA: Connected to remote service 01-11 09:57:57.105 28709-28780/coursematch.coursematchuk V/FA: Processing queued up service tasks: 4 01-11 09:57:59.625 28709-28709/coursematch.coursematchuk E/Count: 200 01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: END OF METHOD ARRAY SIZE CHECK: 0COURSE INFO: Physiotherapy BSc (Hons) 01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 1 01-11 09:57:59.650 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: History and Archaeology BA (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 2 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Arabic and French MA (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 3 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Spanish BA (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 4 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (French and Spanish) and Greek (with year abroad) MA (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 5 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Politics BA (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 6 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Nursing (Child) BSc (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 7 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical and Electronic Engineering MEng (Hons) 01-11 09:57:59.655 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 8 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: English and Russian MA (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 9 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and Medieval History MA (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 10 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and Modern History MA (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 11 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Physics with Sports Science BSc (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 12 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering with Management BEng (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 13 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Optometry BSc (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 14 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Sociology BA (Hons) 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 15 01-11 09:57:59.660 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and French and Russian (with year abroad) MA (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 16 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Statistics Economics and Finance BSc (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 17 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mechanical Engineering BEng (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 18 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business Management (Spain) BBA (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 19 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Ancient History and Archaeology BA (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 20 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering MEng (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 21 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Drama and Music BA (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 22 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Chemistry BSc (Hons) 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 23 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Medicine MB 01-11 09:57:59.665 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 24 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Linguistics MA (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 25 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Aeronautical Engineering BEng (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 26 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business and Marketing BSc (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 27 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: German and Theatre and Performance BA (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 28 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Anthropology BA (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 29 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Spanish BA (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 30 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (French and Spanish) and Greek MA (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 31 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Accounting and Mathematics and Statistics BA (Hons) 01-11 09:57:59.670 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 32 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Art History and French MA (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 33 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Accounting and Business Analysis and Technology BA (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 34 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Modern Languages (Russian and Spanish) and English (with integrated year abroad) MA (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 35 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Policy and Crime BA (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 36 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: International Business and Marketing BSc (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 37 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Statistics Economics and a Language BSc (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 38 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Social Work BA (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 39 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Combined Honours in Social Sciences BA (Hons) 01-11 09:57:59.675 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 40 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and Russian and Spanish (with year abroad) MA (Hons) 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 41 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Automotive Engineering BEng (Hons) 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 42 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical Engineering MEng (Hons) 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 43 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Comparative Literature and Russian and Spanish MA (Hons) 01-11 09:57:59.680 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 44 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Economics and Finance (with European study) BA (Hons) 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 45 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mathematics Statistics and Finance BSc (Hons) 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 46 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: French and German Studies BA (Hons) 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 47 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Sociology BA (Hons) 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 48 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electronic Engineering with Management MEng (Hons) 01-11 09:57:59.685 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 49 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Mechanical Engineering BEng (Hons) 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 50 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Medicine (graduate entry) BMBS 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 51 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Electrical Engineering BEng (Hons) 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 52 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE INFO: Automotive Engineering MEng (Hons) 01-11 09:57:59.690 28709-28709/coursematch.coursematchuk I/System.out: COURSE CARD MODEL LIST SIZE: 53 

调用dbref.addValueEventListener()的结果在onDataChange回调中异步返回…这很可能在您从该方法返回时没有发生,这就是courseCardModelList为空的原因。 您需要相应地构建逻辑。

[ 在此处输入图像描述 ] [1

]这是在Activity类中,下面的代码可以帮助您从数据库中检索单个值,也可以检索Arraylist中的所有数据和存储,以及Hashmap,其中Key值对显示在注释行中。 在此之后,您可以根据需要填写Recyclerview或Listview中的所有检索数据:

 public class NavigationActivity extends AppCompatActivity{ CourseListNewAdapter courseListNewAdapter; private DatabaseReference mDatabase; private ArrayList userCourseDetailsesList; private RecyclerView rv_course_detail; @Override protected void onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_navigation); initializeAuthListener(); mDatabase = FirebaseDatabase.getInstance().getReference(); rv_course_detail = (RecyclerView) findViewById(R.id.rv_course_detail); } private void initializeAuthListener() { mAuth = FirebaseAuth.getInstance(); mAuthListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { firebaseUser = firebaseAuth.getInstance().getCurrentUser(); try { if (firebaseAuth != null) { loadUserDetails(); Log.d("@@@@", "thread:signed_in:" + firebaseUser.getUid()); } else { Log.d("@@@@", "thread:signed_out"); Intent login = new Intent(NavigationActivity.this, LoginActivity.class); startActivity(login); finish(); } }catch (Exception e){ Intent login = new Intent(NavigationActivity.this, LoginActivity.class); startActivity(login); finish(); } } }; mAuth.addAuthStateListener(mAuthListener); } private void loadUserDetails() { DatabaseReference userReference = mDatabase .child("users").child(firebaseUser.getUid()); // displayUserDetails(userReference); userReference.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { user = dataSnapshot.getValue(User.class); tv_user_name.setText(user.getDisplayName()); tv_user_email_nav.setText(user.getEmail()); Glide.with(NavigationActivity.this) .load(user.getPhotoUrl()) .placeholder(R.mipmap.profile) .centerCrop() .dontAnimate() .bitmapTransform(new CropCircleTransformation(NavigationActivity.this)) .into(iv_user_image); loadUserCourseList(); // String PhoneNumber = user.getPhoneNumber(); // Log.e("UserPhone",PhoneNumber); } @Override public void onCancelled(DatabaseError databaseError) { // Toast.makeText(ThreadActivity.this, R.string.error_loading_user, Toast.LENGTH_SHORT).show(); // finish(); } }); } private void loadUserCourseList(){ DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Course").child(firebaseUser.getUid()); databaseReference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { userCourseDetailsesList = new ArrayList<>(); // AlluserCourseDetailsesList = new ArrayList<>(); if(dataSnapshot.exists()) { for(DataSnapshot postSnapShot:dataSnapshot.getChildren()) { // if (firebaseUser.getUid().equals(postSnapShot.getKey())) { Course userCourseDetails = postSnapShot.getValue(UserCourseDetails.class); userCourseDetailsesList.add(new UserCourseDetails(userCourseDetails.getCourse_type(), userCourseDetails.getCourse_id(), userCourseDetails.getCourse_Pic_url())); } } courseListNewAdapter = CourseListNewAdapter(NavigationActivity.this, userCourseDetailsesList); RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(NavigationActivity.this); rv_Course_detail.setLayoutManager(layoutmanager); rv_Course_detail.setItemAnimator( new DefaultItemAnimator()); rv_Course_detail.setAdapter(CourseListNewAdapter); // Constants.AllCourse = new ArrayList>(); // HashMap map = new HashMap(); // // map.put("course_id",userCourseDetails.getCourse_id()); // map.put("course_Pic_url", userCourseDetails.getCourse_Pic_url()); // map.put("course_type", userCourseDetails.getCourse_type()); // // Constants.AllCourseList.add(map); // courseListAdapter = new CourseListAdapter(NavigationActivity.this, userCourseDetailsesList); // lv_Course_detail.setAdapter(courseListAdapter); }else{ Intent i = new Intent(NavigationActivity.this, AddCourseActivity.class); startActivity(i); finish(); // NavigationActivity.this.overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_in_left); } Log.e("Course List size",""+userCourseDetailsesList.size()); } @Override public void onCancelled(DatabaseError databaseError) { } }); } } 

}

Recycler适配器:

 import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; import java.util.ArrayList; public class CourseListNewAdapter extends RecyclerView.Adapter { Context context; ArrayList userCourseDetailses; public CourseListNewAdapter(Context context, ArrayList userCourseDetailses) { this.context = context; this.userCourseDetailses = userCourseDetailses; } // Provide a suitable constructor (depends on the kind of dataset) public class MyViewHolder extends RecyclerView.ViewHolder { public ImageView iv_course_photo; public TextView tv_course_type; public TextView tv_course_id; public MyViewHolder(View itemView) { super(itemView); iv_course_photo = (ImageView) itemView.findViewById(R.id.iv_course_photo); tv_course_type = (TextView) itemView.findViewById(R.id.tv_course_type); tv_course_id = (TextView) itemView.findViewById(R.id.tv_course_id); } } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_user_Course_list, parent, false); return new MyViewHolder(view); } MyViewHolder holderView; @Override public void onBindViewHolder(final MyViewHolder holder, final int position) { holderView = holder; try { if (userCourseDetailses.size() > 0) { final UserCourseDetails s = userCourseDetailses.get(position); holder.tv_course_type.setText(s.getCourse_type()); holder.tv_course_number.setText(s.getCourse_id()); // Loading Course image Glide.with(context).load(s.getCourse_Pic_url()) .crossFade() .thumbnail(0.5f) .diskCacheStrategy(DiskCacheStrategy.ALL) .placeholder(R.drawable.plaeholder_item) .listener(new RequestListener() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { holder.iv_course_photo.setImageDrawable(resource); return false; } }) .into(holder.iv_course_photo); } } catch (Exception e) { } } @Override public int getItemCount() { return userCourseDetailses.size(); } public boolean isEmpty() { return getItemCount() == 0; } } 

Listview适配器:

 import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.target.Target; import java.util.ArrayList; import java.util.HashMap; public class CourseListAdapter extends BaseAdapter { Context context; ArrayList userCourseDetailses; ImageView iv_number_plate_photo; public CourseListAdapter(Context c, ArrayList userCourseDetailses) { this.context = c; this.userCourseDetailses = userCourseDetailses; } @Override public int getCount() { return userCourseDetailses.size(); } @Override public Object getItem(int pos) { return userCourseDetailses.get(pos); } @Override public long getItemId(int pos) { return pos; } @Override public View getView(int position, View convertView, ViewGroup viewGroup) { if(convertView==null) { convertView= LayoutInflater.from(context).inflate(R.layout.adapter_user_Course_list,viewGroup,false); } iv_number_plate_photo = (ImageView) convertView.findViewById(R.id.iv_number_plate_photo); TextView tv_Course_brand= (TextView) convertView.findViewById(R.id.tv_Course_brand); TextView tv_Course_type= (TextView) convertView.findViewById(R.id.tv_Course_type); TextView tv_Course_number= (TextView) convertView.findViewById(R.id.tv_Course_number); final UserCourseDetails s= (UserCourseDetails) this.getItem(position); tv_Course_brand.setText(s.getCourse_brand()); tv_Course_type.setText(s.getCourse_type()); tv_Course_number.setText(s.getCourse_id()); // Loading profile image Glide.with(context).load(s.getCourse_number_plate_url()) .crossFade() .thumbnail(0.5f) .diskCacheStrategy(DiskCacheStrategy.ALL) .placeholder(R.drawable.plaeholder_item) .listener(new RequestListener() { @Override public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { iv_number_plate_photo.setImageDrawable(resource); return false; } }) .into(iv_number_plate_photo); convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //OPEN DETAIL // openDetailActivity(s.getName(),s.getDescription(),s.getPropellant()); } }); return convertView; } }