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 7 show that the names are retrieved from the DB and passed 
to ArrayLists.

- Lines 8 and 10 show that the ArrayList petNameList is succefully passed 
to the adapter (as items).

- Line 17 shows what position in the ArrayList is being pointed to

- Line 18 shows what is stored in ArrayList[0] and line 19 shows what is 
now in "item". (I assume that what is shown is a pointer).

- Line 20 states that the values in lines 18 and 19 should be the same name 
and they are.

- Line 21 shows what onBindViewHolder has passed to the ViewHolder and it 
is NOT the first name stored in the DB or in the
  ArrayList. It should be "fert" (see line 5).

- Line 26 shows what is stored in ArrayList[1] and line 27 shows what is 
now in "item". (I assume that what is shown is a pointer).

- Line 28 states that the values in lines 26 and 27 should be the same name 
and they are.

- Line 29 shows what onBindViewHolder has passed to the ViewHolder and it 
is the name stored in the DB and in the
  ArrayList.

So, it appears that my "setText statement is passing an incorrect value, 
"qwerty", to the ViewHolder is wrong and I currently do not know why. I am 
researching this and will post an answer if I find it before someone ansers 
this question.

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@fd82c44, 
       com.example.jbiss.petminder.PetName@d1fc12d]
    9. made it to: SelectNameAdapter constructor
    10. items is: [com.example.jbiss.petminder.PetName@fd82c44, 
        com.example.jbiss.petminder.PetName@d1fc12d]
    11. mAdapter count is: 2
    12. pN arraylist[0] is: fert
    13. pN arraylist[1] is: qwerty
    14. made it to: onCreateViewHolder
    15. made it to: ViewHolder
    16. made it to: onBindViewHolder
    17. position in ArrayList is: 0
    18. mItems[0] is: com.example.jbiss.petminder.PetName@fd82c44
    19. item is: com.example.jbiss.petminder.PetName@fd82c44
    20. NOTE: item should equal mItems[0]
    21. onBindViewHolder passed: item.getPetName() qwerty to ViewHolder
    22. made it to: onCreateViewHolder
    23. made it to: ViewHolder
    24. made it to: onBindViewHolder
    25. position in ArrayList is: 1
    26. mItems[1] is: com.example.jbiss.petminder.PetName@d1fc12d
    27. item is: com.example.jbiss.petminder.PetName@d1fc12d
    28. NOTE: item should equal mItems[1]
    29. onBindViewHolder passed: item.getPetName() 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));
        //setup the adapter and apply it to the RecyclerView
        mAdapter = new SelectNameAdapter(this, petNameList);
        mRecyclerView.setAdapter(mAdapter);
        Log.d("mAdapter count is", String.valueOf(mAdapter.getItemCount()));

        for(int i = 0; i < pN.size(); i++){
            Log.d("pN arraylist" + "[" + i + "] is", 
String.valueOf(pN.get(i)));
        }


**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");
        Log.d("position in ArrayList is", String.valueOf(position));
        PetName item = mItems.get(position);
        Log.d("mItems[" + position + "] is", 
String.valueOf(mItems.get(position)));
        Log.d("item is", String.valueOf(item));
        Log.d("NOTE", "item should equal mItems[" + position + "]");
        holder.tvName.setText(item.getPetName());
        Log.d("onBindViewHolder passed ", "item.getPetName() " + 
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/722c2364-7768-4bb8-a511-3fc585e6b75f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to