This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!

Adam Wentz

Ranch Hand
+ Follow
since Jul 29, 2016
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Adam Wentz

Pete Letkeman wrote:Here you go
Take a look at which I've copied and pasted the part that may interest you

Yes! This seems to be what I was looking for. I have been scouring the web all the day and never came across this.

Thanks a bunch, Pete!  
11 months ago
I am creating a binded service to use for catching duplicates before they are written to my database.

In the service, I want to query everything from the database onStartCommand and write the results to an ArrayList that I can use in multiple methods with conditionals, etc.

Is there a viable way to achieve adding all of the database's records to an ArrayList?
11 months ago
Also, share as much of your code as you can, especially your manifest and layout.
11 months ago

Adam Wentz wrote:Which version of Android are you targeting in your project?

The "Landroid" portion of the failed resolution suggests you might be targeting a beta version. Is that so?

As you were, L Android was the codename used for Android Lollipop. Apart from that correction, which version of Android are you targeting?
11 months ago
Which version of Android are you targeting in your project?

The "Landroid" portion of the failed resolution suggests you might be targeting a beta version. Is that so?
11 months ago
It seems like I finally nailed this thing down.

I eliminated the pkgStrings ArrayList altogether and just did a comparison between the currentPkgSelections and the packageName field of the packageList (which is a String).

I declare a new ArrayList<String> at the top of my class:

Then, in my onCreateViewHolder() method (I'm working in my custom RecyclerView Adapter class), I first clear switchTheseOn and then run a loop with a nested conditional:

I am still getting a weird output in my log, but the new array contains only those packages that are similar between currentPkgSelections and packageList. That fixes my problem at hand!

Apart from that, I did mention that I needed the common values to be able to set the initial state of the switches in my RecyclerView's row layout. If you're interested in knowing how I handled that part of the equation,
In my adapter's onBindViewHolder() method, after I set the package TextView, but before I create the onClickListener for the Switch view, I defined a loop and nested two conditionals inside of it, one to check the value of the appPackage TextView against the values in my switchTheseOn ArrayList<String> and the other to check the current state of the Switch view:

I still have no idea why the output in my log is all messed up but the actual functionality of my ArrayList<String> is fine, so I'm moving on.

Thanks to everyone who took the time to offer valuable insight!!  

11 months ago
I tried different code to get a clean ArrayList, but I still get the 10 x 10 output:


11 months ago

Adam Wentz wrote:Yes, there is a loop involved.

I forgot to say that before I can do a comparison, I must get the package names from packageList as Strings. I use a loop to do that:

That's what's producing 10 lines of 10 packages, and throwing off the later comparison.
11 months ago

Piet Souris wrote:10 lines, while each line has 10 elements. Are these lines printed in a loop or so? I would expect to see just a single line.

Yes, there is a loop involved.

Here is what I am trying to accomplish:

1. I have List<PackageInfo> packageList, which is a ListArray containing system-generated information about applications installed on the device. It has various bits of information, like name, package name, etc., etc.
2. This list is displayed in a RecyclerView that includes a Switch in each row.
3. The user clicks a Switch on and the package name for the relevant row is written to an ArrayList<String>.
4. The ArrayList<String> currentPkgSelections contains all of these selected package names.
5. When the user goes back to that same Activity at a later time/date, the aforementioned values are queried from a database for the purpose of setting relevant switches on, based on what's already been saved by the user.
6. However, before that can happen, packageList is once again populated by the system with applications currently installed on the device. There needs to be a comparison performed between currentPkgSelections and packageList
   to weed out any of the previously-selected applications that are no longer installed (User uninstalled between then and now). Otherwise, once I run a loop to set the relevant Switches on, I would get a NullPointerException for
   those packages that no longer exist.
11 months ago
But look what I get when:


11 months ago

Piet Souris wrote:Sorry Carey, but better two replies than none!

Hmm... but what do you get with

11 months ago

Piet Souris wrote:Then do a simple second test. Say, compare the lists A and B.

I tried this:

I got this:

And I believe that it is due to my first loop messing up my comparison.
11 months ago

Carey Brown wrote:Need to see how you are setting up switchOnThese. Every time you call this block of code this list (?) will get longer. If you don't re-initialize it you will get duplicates.

switchTheseOn is initialized in its declaration. packageList is passed through the constructor, as is currentPkgSelections.
11 months ago

Piet Souris wrote:If you have these lists: {a, a} and {a, b, c} what would you expect as outcome? Is the operation commutative? Are the elements implemeting the equals method correctly? And do have a look at the method 'retainAll'.

From those lists, I would expect a new list of {a}. For the record, though, {a, a} would never be a possibility in my case.
11 months ago
Here is a broader look at what is happening:

I think my first loop is possibly throwing off my second loop. The first loop is creating a new ArrayList<String> from an ArrayList<PackageInfo>.
I was having issues comparing packageList to currentPkgSelections since they are of different types.
11 months ago