• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ArrayList Size Comes Back 0

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll admit that I don't know Android development as well as others, but maybe I can still help. Here are a few things that you can try which may help your out (then again they may not).
1) Using Final:
You say that you have this line somewhere as long as selectedApps will only/always be using that single ArrayList then you can replace it with. You will still be able to use all of the ArrayList methods like Add, Remove etc, you will just not be able to reinitialize the variable to point to something else.
This is a good thing. In doing this, if you get an error about trying to reinitialize the variable then you may have done something wrong.
At the same time, unless you are planning on subclassing or extending your Activity classes I think that you can make them final as well. You may be able to even make your Switch final
Some JVM languages ensure that you make everything final for classes and variable unless otherwise specified.
I believe that this is part of the "Effective Java" book/guidelines. You can read more about this if you Google for "effective java final variables"

2) Adding the onClick:
To add the onClick and/or onLongClick to your Recyclerview you have to do a couple of small things as noted in the following tutorials:
https://antonioleiva.com/recyclerview-listener/
https://android.jlelse.eu/click-listener-for-recyclerview-adapter-2d17a6f6f6c9
https://newfivefour.com/android-item-click-listener-recyclerview.html
Yes, there are different ways to do this. I think that one of the best would be to implement the view.onclick and view.onlongclick interfaces in the viewholder class.

3) Use different variable names for testing:
To make sure that names don't collide and that you are not overwriting any values.
I suggest that you do something like selectedAppsAct1 instead of selectedApps and applicationsActivity.selectedAppsAct2 instead of applicationsActivity.selectedApps.
With Android Studio this can take minutes to do using the Refactor commands.

4) Unit testing:
You have not mentioned too much or anything about Unit tests. You may want to add in some basic unit tests before too long so that you can be sure your code works when you need to update it.
There are many volumes on unit testing in general as well as for Android development.

5) Design Patterns:
Are you using any design patterns? There is MVC, MVVM, MVP and others that can be used with Android development. Using these well established design patterns some things like unit testing and code refactoring can be done easier.
There are many volumes on design patterns in general as well as for Android development.

In handling the above points you may walk away with your application being programmed in a totally different way then your first expected.
Please note that these are only some suggestions and nothing "has to be done" with the suggestions.
Also, everyone on this site does have full time jobs and they are volunteering their time an knowledge to help people out.

Adam Wentz wrote:Although I have suspicions as to what is happening, it is only speculation because I cannot collect enough debug info.


What do you suspect is happening?

Follow up questions:
  • I guess that you are using the Android Emulator for your development.
       If this is the case have you tried with a different emulated device running a different version of Android to see if the result is the same?
  • Have you tried running this code on a actual physical device?
       Was the result the same?

  • What is the "ideal" device, specification wise that you want your program to run on?
       Memory?
       Android Version?
       Chipset (intel, arm etc)?
     
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So, after over a week of fumbling with this problem, I have finally come to a resolution.

    I decided to create a separate ArrayList<String> in the Adapter:



    Then, before the Intent's put statement I cloned the new ArrayList to the one I included in the Intent:



    However, that threw an "Incompatible types" error:



    As to precisely why an ArrayList of type String was being called an Object, I have no idea. However, to resolve the issue I decided to simply try casting it:



    Voila! Problem solved.
    Now, the data is properly getting passed through with the Intent and written to the application's database.

    Granted, I do get an "Unchecked cast" warning using my solution above. However, since an Object cannot be checked at runtime, the warning is of no real
    pertinence. At least, as far as I know.
     
    Rancher
    Posts: 4801
    50
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Adam Wentz wrote:
    Then, in the Adapter, just before the Constructor, I grab an instance of the Activity:



    But that's a new instance of the Activity.
    It's not the same Activity that you populated the ArrayList in.
     
    Adam Wentz
    Ranch Hand
    Posts: 84
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Dave Tolls wrote:
    But that's a new instance of the Activity.
    It's not the same Activity that you populated the ArrayList in.




    You make a great point but I am not even using that line for anything in my solution. I did just go and remove it, though.
     
    no wonder he is so sad, he hasn't seen this tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic