简体   繁体   中英

retrieving data from firebase database and storing in an array list

I am attempting to create a small course aggregator program in the form of an android application.

My courses are all stored in a Firebase Realtime Database, which is viewable from the firebase console and everything appears fine.

The issue is that I have written a java method to connect to the DB, retrieve the data from the DB, cast the data as a Custom Java object Course , attach it to another custom Java object CourseCardModel , and then save the CourseCardModel object into an ArrayList.

The connection to the database is made successfully, and the snapshot it generates contains the correct information, which I have verified by looping through the snapshot and printing a variable Course Name of each Course object out successfully. The problem is that when the method completes, for some reason the arrayList is returns is empty, even though upon checking the size of the ArrayList throughout the snapshot iteration I can see the CardModel objects being added to it.

If anybody has any help in solving this it would be hugely appreciated, I am fairly new to Firebase.

GenerateCourses() Method

private ArrayList<CourseCardModel> generateCourseCards() {

    courseCardModelList = new ArrayList<CourseCardModel>();
    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 Output

 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 

The results from call to dbref.addValueEventListener() are returned asynchronously in onDataChange callback...which more than likely hasn't occurred at point that you return from that method which is why courseCardModelList is empty. You'll need to structure your logic accordingly.

[ 在此处输入图片说明 ][1

]This is in Activity Class, Below code help you to retrieve single value from the database and also for retrive all data and store in Arraylist and also for Hashmap with Key value pair shows in commented line. After this you can fill all retrieval data in Recyclerview or Listview as you wish:

public class NavigationActivity extends AppCompatActivity{
    CourseListNewAdapter courseListNewAdapter;
    private DatabaseReference mDatabase;
    private ArrayList<Course> 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<String, String>>();
    //                    HashMap<String, String> map = new HashMap<String, String>();
    //
    //                    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 Adapter :

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<CourseListNewAdapter.MyViewHolder> {
    Context context;
    ArrayList<UserCourseDetails> userCourseDetailses;

    public CourseListNewAdapter(Context context, ArrayList<UserCourseDetails> 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<String, GlideDrawable>() {
                            @Override
                            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> 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 Adapter:

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<UserCourseDetails> userCourseDetailses;

    ImageView iv_number_plate_photo;
    public CourseListAdapter(Context c, ArrayList<UserCourseDetails> 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<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM