[android-developers] ArrayList[0] through ArrayList[n-1] element being overwritten by ArrayList[n]

2017-08-07 Thread Jeff Biss
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 mItems;
private Context mContext;

public SelectNameAdapter(Context context, ArrayList 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", 

[android-developers] Why is only the last item in an ArrayList shown in RecyclerView?

2017-08-03 Thread Jeff Biss
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