I posted this question on stackoverflow.com but started to feel that this 
may be too technical than most users on that site can handle, as it seems 
to me that something is not quite right with the way the adapter is 
handling the ArrayList passed to it.

I am trying to learn how to use the RecyclerView and have found that only 
the last item in an ArrayList are displayed in both positions (there are 
only two) and can't figure out why. I have used LogCat to show what is 
going on so look to the LogCat output below.


   - Lines 2 and 3 show that the cursor has accessed the DB.
   - Lines 4 thru 8 show that the names are retrieved from the DB and 
   passed to ArrayLists.
   - The variable "name" stores the element from the cursor and then is 
   used to populate the ArrayLists: pN is an ArrayList and is used to show the 
   actual name (lines 5 and 7) and petNameList is an ArrayList and shows what 
   I assume is a pointer to the name object (lines 8, 10, 16 and 22).
   - As shown in line 9, the Adapter is entered successfully and as shown 
   in line 10 the petNameList ArrayList is past to the adater's items 
   ArrayList (they have the same references to the PetName objects). Note that 
   line 11 shows the number of adapter items that it is operating on.
   
Now the problem:

As can be seen in lines 17 and 23, there are two positions in the ArrayList 
yet only the second (the last item in the ArrayList) name "qwerty" is 
passed to the ViewHolder (as shown in lines 18 and 24 ) and thus displayed 
on the Android Studio emulator's screen. I can't understand that if the 
ArrayList positions are correct and the references are different (one being 
to the "fert" name object and the other to the "qwerty" name object), why 
only "qwerty" would be display? Any help is appreciated.

It seems that the adapter is ignoring the name at position 0.

For clarity and space, I copied only what I think is the relevant 
MainActivity code below the LogCat output and the Adapter code below that. 
Each shows the Log.d() statements that should help with correlation.

*LogCat*:

1. !------MainActivity: Starts here-----!
2. DB has: 2 rows
3. cursor has: 2 rows
4. name from cursor is: fert
5. pN arraylist[0] is: fert
6. name from cursor is: qwerty
7. pN arraylist[1] is: qwerty
8. petNameList is: [com.example.jbiss.petminder.PetName@f411810, 
com.example.jbiss.petminder.PetName@6c4f09]
9. made it to: SelectNameAdapter constructor
10. items is: [com.example.jbiss.petminder.PetName@f411810, 
com.example.jbiss.petminder.PetName@6c4f09]
11. mAdapter count is: 2
12. OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
13. made it to: onCreateViewHolder
14. made it to: ViewHolder
15. made it to: onBindViewHolder
16. mItems is: [com.example.jbiss.petminder.PetName@f411810, 
com.example.jbiss.petminder.PetName@6c4f09]
17. position in ArrayList is: 0
18. onBindViewHolder[0]: passed qwerty to ViewHolder
19. made it to: onCreateViewHolder
20. made it to: ViewHolder
21. made it to: onBindViewHolder
22. mItems is: [com.example.jbiss.petminder.PetName@f411810, 
com.example.jbiss.petminder.PetName@6c4f09]
23. position in ArrayList is: 1
24. onBindViewHolder[1]: passed qwerty to ViewHolder

*The relevant code in MainActivity:*

    Log.d("!------MainActivity", "Starts here-----!");

    //populate the list with the names returned from the pet name DB
    long numItems = mDbHelper.getPetCount();
    Log.d("DB has", String.valueOf(numItems) + " rows");
    if (numItems != 0) {
        cursor = mDbHelper.getPetNames();
        numItems = cursor.getCount();
        Log.d("cursor has", String.valueOf(numItems) + " rows");
        cursor.moveToFirst();
        for (int i = 0; i < numItems; i++) {
            PetName name = new PetName(cursor.getString(PET_NAMES));
            Log.d("name from cursor is", name.getPetName());
            petNameList.add(name);
            PN = cursor.getString(PET_NAMES);
            pN.add(PN);
            Log.d("pN arraylist" + "[" + i + "] is", String.valueOf(PN));
            cursor.moveToNext();
        }
    } else{
        //otherwise, if no pet names exist int he DB, make the no pets 
found 
    message visible
        msgTv.setText(R.string.no_pets);
        msgTv.setVisibility(View.VISIBLE);
    }

    //the following outputs URIs for the names meaning that the arraylist
    //is a set of pointers to the PetName objects?
    Log.d("petNameList is", String.valueOf(petNameList));
    mAdapter = new SelectNameAdapter(this, petNameList);
    mRecyclerView.setAdapter(mAdapter);
    Log.d("mAdapter count is", String.valueOf(mAdapter.getItemCount()));

*Adapter code:*

public class SelectNameAdapter extends 
RecyclerView.Adapter<SelectNameAdapter.ViewHolder> {

private ArrayList<PetName> mItems;
private Context mContext;

public SelectNameAdapter(Context context, ArrayList<PetName> items) {
    Log.d("made it to", "SelectNameAdapter constructor");
    //the following outputs URIs for the names meaning that the arraylist
    //is a set of pointers to the PetName objects?
    Log.d("items is", String.valueOf(items));
    this.mContext = context;
    this.mItems = items;
}

@Override
public SelectNameAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
int i) {
    Log.d("made it to", "onCreateViewHolder");
    LayoutInflater inflater = LayoutInflater.from(mContext);
    View itemView = inflater.inflate(R.layout.pet_info, parent, false);
    ViewHolder viewHolder = new ViewHolder(itemView);
    return viewHolder;
}

@Override
public void onBindViewHolder(SelectNameAdapter.ViewHolder holder, int 
position) {
    Log.d("made it to", "onBindViewHolder");
    PetName item = mItems.get(position);
    Log.d("mItems is", String.valueOf(mItems));
    Log.d("position in ArrayList is", String.valueOf(position));
    holder.tvName.setText(item.getPetName());
    Log.d("onBindViewHolder[" + position + "]", "passed " + 
item.getPetName() + " to ViewHolder");
}

@Override
public int getItemCount() { {

        return mItems.size();

    }


    public static class ViewHolder extends RecyclerView.ViewHolder {


        public TextView tvName;

        public ViewHolder(View itemView) {

            super(itemView);

            Log.d("made it to", "ViewHolder");

            tvName = (TextView) itemView.findViewById(R.id.petname);

        }

    }

}

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/b82180a3-321a-49c7-bace-def81d9f5f51%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to