# [Q] ListViews and selecting all CheckBoxes



## SkullOne (Jul 9, 2011)

I have a listview with checkboxes but I can't get all the checkboxes to populate. Now I understand that a ListView usually only shows what's on screen and as you scroll items get drawn and items recycled. GetView is already being overridden.

Now I have used setItemChecked to set the flags on all items (setchecked only populates whatever is on screen at that time). So if I scroll up and down the check populates. Is there a way to make it populate before hand or am I stuck having to save the rules after they are selected/cleared and reload the listview to make it stick?

Thanks!


----------



## JBirdVegas (Jun 11, 2011)

Sounds like your doing it right can you post your adapter code? You should be able to get all child views; try to cast to a CheckBox if it works then mark it as selected.

List views are always going to recycle your views... Generally 3 or 4 times (lots of redundancy in list views)


----------



## SkullOne (Jul 9, 2011)

Here's the code. https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/MainActivity.java

showApplications is the ListView function and where I have the checks for select/clear all.

Thanks for the help! Really interested to know what I'm doing wrong.


----------



## JBirdVegas (Jun 11, 2011)

First and I can't stress this enough. More important than your code working is that you start RIGHT NOW encrypting passwords. I recommend converting inputted password to md5 checksum. There are LOTS of VERY well documented ways to NOT STORE MY PASSWORD IN PLAIN TEXT!!!

I would be apologetic but this is not a negotiable feature. If you need a password then you MUST protect that trust the user gives you.

Now to some general notes on the class:

please read http://source.androi...code-style.html
- your code is all over the place with random tabs... if you copy paste code thats fine but make the indents match the structure of the class.
- Android uses four whitespaces instead of tabs... Please conform to the open source standard.
- I mean really look at this code: https://github.com/s...ivity.java#L243 its hard to read.

Now I think you may have more problems than you think:
Memory leak your ignoring: https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/MainActivity.java#L72
in onPause you null the adapter but don't reset it in the onResume: https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/MainActivity.java#L118
I'm not sure if your IDE is setup wrong or if you followed a bad guide but its not important to make nonField variables final unless they are accessed by an innerclass. {see all most all vars declared final}

After seeing your code what exactly is the problem because your not setting the checked state for in this if block: https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/MainActivity.java#L284
only in its else block

but to be honest its all really hard to tell as the formatting is completely random... but like I said you need to first encrypt the password and username your storing. I don't want any app/hacker/phone theif to have my passwords.


----------



## JBirdVegas (Jun 11, 2011)

please don't take those comments as discouraging. That was not the intent. I'm just sensitive about personal infomation and I feel we all should be.

now the code style stuff... not fixing that is just lazy and asking for people to not look at your code. Not to mention it makes it infinitely harder for you to maintain in the future and I promise you will want to go back and change things.


----------



## SkullOne (Jul 9, 2011)

JBirdVegas said:


> please don't take those comments as discouraging. That was not the intent. I'm just sensitive about personal infomation and I feel we all should be.
> 
> now the code style stuff... not fixing that is just lazy and asking for people to not look at your code. Not to mention it makes it infinitely harder for you to maintain in the future and I promise you will want to go back and change things.


No discouragement or bad feelings. Really it's helpful. Been a long time since I've coded and 90% of the code isn't mine. It was all originally from the Droidwall project and I've been slowly fixing the making the formatting the same. Yarly mentioned to me before about Eclipse being able to auto format and I just keep forgetting to change the settings and have it do it.

The not fixing the password sooner is totally on me because I really haven't touched any of that code yet.. I'll fix the memory leak and the check boxes as well today. Then I'll focus on reformatting the code to something more readable before continuing work on the app.

Thanks for all the help!

Sent from my Galaxy Nexus using Tapatalk 2


----------



## yarly (Jun 22, 2011)

If I can offer a suggestion, learn shortcut keys for things you do constantly. If you can't remember them, set them to keys you'll remember. It'll save you lots of time.


----------



## JBirdVegas (Jun 11, 2011)

I think we all got distracted but let us know if you run into another question or comment. Happy to help people learn. Good luck.


----------



## SkullOne (Jul 9, 2011)

I cleaned up the code across the board with Eclipse's auto format (shift + ctrl + f just for Yarly  ). So have a look and see if it's any better for you.

I still have the issue with the check boxes not populating unless I scroll up and down. Did I not do as you suggested? I added an MD5 hash to the password as well. 

Also how would you fix the Handler issue. That's original code so I have no idea why it was done that way instead of in a proper manner.

Thanks!


----------



## JBirdVegas (Jun 11, 2011)

have you tried calling ArrayAdapter.html#notifyDataSetChanged()?

after setting the adapter

and I would have to look to be sure but

```
<br />
@Override<br />
protected void onResume() {<br />
  super.onResume();<br />
<br />
// ** I'm not 100% sure about this I'll have to reread the docs but **<br />
// listview may not be null but it may be an instance from a previous view<br />
// so you may want to just grab a valid reference<br />
  if (this.listview == null) {<br />
   this.listview = (ListView) this.findViewById(R.id.listview);<br />
   // update listview<br />
   this.listview.notifyDataSetChanged();<br />
  }<br />
  refreshHeader();<br />
  final String pwd = getSharedPreferences(Api.PREFS_NAME, 0).getString(<br />
	Api.PREF_PASSWORD, "");<br />
  if (pwd.length() == 0) {<br />
   // No password lock<br />
   showOrLoadApplications();<br />
  } else {<br />
   // Check the password<br />
   requestPassword(pwd);<br />
  }<br />
}<br />
```


----------



## SkullOne (Jul 9, 2011)

JBirdVegas said:


> have you tried calling ArrayAdapter.html#notifyDataSetChanged()?
> 
> after setting the adapter
> 
> ...


I have tried notifyDataSetChanged. It doesn't work. While researching that I found this article which gives a reason as to why it doesn't work in this instance: http://stackoverflow.com/questions/3669325/notifydatasetchanged-example/5092426#5092426

Two questions:

1. Is the code formatting OK now?
2. How hard would it be to switch from the ListView to a Linear Layout with Scroll view? This ListView that the original author used is apparently more problematic than it's worth so if I can swap it out for something else without having to rewrite the whole app I'll do it. Otherwise Select/Clear All will just go away. Don't know why a user would ever select all apps to allow them in the first place in a firewall.


----------



## JBirdVegas (Jun 11, 2011)

Just extend ListActivity. Move your adapter to its own class. Did you try option 4 runOnUiThread?

Yea the formatting looks much better! See how much easier it is to read. Also good work with the password simple and very effective. Good work!


----------



## SkullOne (Jul 9, 2011)

JBirdVegas said:


> Just extend ListActivity. Move your adapter to its own class. Did you try option 4 runOnUiThread?
> 
> Yea the formatting looks much better! See how much easier it is to read. Also good work with the password simple and very effective. Good work!


runOnUiThread isn't working either.

I'm down to 4 options at this point since I can't figure this out.

1. Just forget about Select All.
2. Leave select all as is and hope people read release notes that they have to scroll to the bottom and back up again.
3. auto scroll to the bottom of the ListView and back up again. I toyed with this and can get it to scroll down but not back up.
4. Change from listview to a linearlayout scroll view.


----------



## SkullOne (Jul 9, 2011)

I ran into an issue with Cancelling the dialog box for the password now that the MD5 hashing has been added. Clicking cancel doesn't do anything. The dialog box doesn't disappear.

https://github.com/s...ivity.java#L636

That's the method where it gets stuck. No idea why it gets stuck there all of the sudden. It was all working correctly before the MD5 hashing code was added. The only code that changed with the Password was the MD5 hash code. No more no less. Any ideas? I'm completely lost right now.


----------



## JBirdVegas (Jun 11, 2011)

your calling the handler to send the message but you don't dismiss the dialog

https://github.com/skullone/android_firewall/blob/master/src/com/jtschohl/androidfirewall/PassDialog.java#L98
after that line add

```
dismiss();
```


----------



## SkullOne (Jul 9, 2011)

JBirdVegas said:


> your calling the handler to send the message but you don't dismiss the dialog
> 
> https://github.com/s...Dialog.java#L98
> after that line add
> ...


I've tried that. It doesn't work. That's why I'm so puzzled.


----------



## JBirdVegas (Jun 11, 2011)

Humm not sure maybe try canceling the dialog?


----------



## SkullOne (Jul 9, 2011)

JBirdVegas said:


> Humm not sure maybe try canceling the dialog?


cancel() doesn't work either. My god..wtf is going on here? 

I do know this. the msg.obj sent back in NULL. Can I use that in any way to end the dialog?


----------



## SkullOne (Jul 9, 2011)

I fixed it. I simply added this to the onClick.

if (v.getId() == R.id.pass_cancel){
dismiss();
}

Didn't think I needed to do that though. Well at least it works I guess.


----------



## JBirdVegas (Jun 11, 2011)

You can send back anything in the handler message but if left unset then null should be expected.


----------

