Somehow the name in the first ArrayList position (see Line 6) was 
overwritten with that in the second (see Line 13). Can anyone offer a 
reason as to what might be happening?

Refer to LogCat section for the numbers:

- Lines 2 and 3 show that the cursor has accessed the DB.

- Lines 4 thru 9 show that the names are retrieved from the DB and passed 
to ArrayLists.

- Lines 10 through 14 show the petNameList contents (see A) passed to the 
adapter (see B).

- Lines 15 through 18 show petNameList contents passed from MainActivity 
(see C).

- Lines 22 through 27 show that the first position of the ArrayList holds 
"qwerty".

- Lines 31 through 36 show that the second position of the ArrayList holds 
"qwerty".

I will keep experimenting to see if I can find out what happens and post it 
if I find the reason.

**LogCat:**

    1. !------MainActivity: Starts here-----!
    2. DB has: 2 rows
    3. cursor has: 2 rows
    4. The following should be the same:  
    5. name from cursor is: fert
    6. petNameList[0] is: fert
    7. The following should be the same:  
    8. name from cursor is: qwerty
    9. petNameList[1] is: qwerty
    10. petNameList is: [com.example.jbiss.petminder.PetName@ea7cc11, 
com.example.jbiss.petminder.PetName@3c54176]
    11. petNameList count is: 2
    12. petNameList elements passed to mAdapter:  
    13. petNameList[0] passed to the adapter constructor is: qwerty
    14. petNameList[1] passed to the adapter constructor is: qwerty

    15. made it to: SelectNameAdapter constructor
    16. items is: [com.example.jbiss.petminder.PetName@ea7cc11, 
com.example.jbiss.petminder.PetName@3c54176]
    17. ArrayList[0] passed to the adapter constructor is: qwerty
    18. ArrayList[1] passed to the adapter constructor is: qwerty

    19. made it to: onCreateViewHolder
    20. made it to: ViewHolder
    21. made it to: onBindViewHolder
    22. position in ArrayList is: 0
    23. mItems[0] (pointer) is: com.example.jbiss.petminder.PetName@ea7cc11
    24. pNm (pointer) is): com.example.jbiss.petminder.PetName@ea7cc11
    25. pNm (String ) is: qwerty
    26. NOTE: item should equal mItems[0]
    27. onBindViewHolder passed: item.getPetName() qwerty to ViewHolder
    28. made it to: onCreateViewHolder

    29. made it to: ViewHolder

    30. made it to: onBindViewHolder
    31. position in ArrayList is: 1
    32. mItems[1] (pointer) is: com.example.jbiss.petminder.PetName@3c54176
    33. pNm (pointer) is): com.example.jbiss.petminder.PetName@3c54176
    34. pNm (String ) is: qwerty
    35. NOTE: item should equal mItems[1]
    36. onBindViewHolder passed: item.getPetName() qwerty to ViewHolder


**The relevant code in MainActivity:**

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

        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();

A)

            for (int i = 0; i < numItems; i++) {
                PetName name = new PetName(cursor.getString(PET_NAMES));
                Log.d("The following should be the same", " ");
                Log.d("name from cursor is", name.getPetName());
                petNameList.add(name);
                //the following is for troubleshooting, it gets the 
currently added petname
                PN = petNameList.get(i);
                pName = PN.getPetName();
                Log.d("petNameList[" + i + "] is", pName);
                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);
        }

B)

        Log.d("petNameList is", String.valueOf(petNameList));
        num = petNameList.size();
        Log.d("petNameList count is", String.valueOf(num));
        Log.d("petNameList elements passed to mAdapter", " ");
        for(int j =0; j < num; j++){
            PN = petNameList.get(j);
            pName = PN.getPetName();
            Log.d("petNameList[" + j + "] passed to the adapter constructor 
is", pName);
        }
        //setup the adapter and apply it to the RecyclerView
        mAdapter = new SelectNameAdapter(this, petNameList);
        mRecyclerView.setAdapter(mAdapter);


**Adapter code:**

    private ArrayList<PetName> mItems;
    private Context mContext;

    public SelectNameAdapter(Context context, ArrayList<PetName> items) {
        Log.d("made it to", "SelectNameAdapter constructor");
        Log.d("items is", String.valueOf(items));
        PetName PN;
        String name;

C)

        int num = items.size();
        for(int j =0; j < num; j++){
            PN = items.get(j);
            name = PN.getPetName();
            Log.d("ArrayList[" + j + "] passed to the adapter constructor 
is", name);
        }
        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));
        Log.d("mItems[" + position + "] (pointer) is", 
String.valueOf(mItems.get(position)));
        PetName pNm = mItems.get(position);
        Log.d("pNm (pointer) is)", String.valueOf(pNm));
        String n = pNm.getPetName();
        Log.d("pNm (String ) is", n);
        Log.d("NOTE", "item should equal mItems[" + position + "]");
        holder.tvName.setText(pNm.getPetName());
        Log.d("onBindViewHolder passed ", "item.getPetName() " + 
pNm.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 [email protected].
To post to this group, send email to [email protected].
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/89eaa894-e324-487c-adae-f094a83e26c4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to