notifyDatasetChanged does not work on Gridview, unless devices has been rotated - android

This is really an odd problem. The basic gist of it is what the title says. I have an adapter, which I am updating and calling notifyDatasetChanged() The problem however, is it does not work, unless the device has been rotated at least once. I can't for the life of me figure out why, what is being done differently after a rotation occurs?
The code in question is here:
The ASyncTask that handles it..
protected void onPostExecute(ArrayList<Records> result) {
if (ca == null)
{
ca = new CoverAdapter<Records>(c, R.layout.grid_cover_with_text_item, result);
}
if (gv.getAdapter() == null)
{
gv.setAdapter(ca);
}
else
{
new AdapterHelper().update((CoverAdapter) ca, result);
ca.notifyDataSetChanged;
}
}
With "ca" being my adapter, "gv" being my GridView and AdapterHelper().update being a method I found here to clear the adapter and add all the results of the arraylist to it, so it should be being updated properly.
Remember, this code works after the device has been rotated. Very confused right now, any insight would be appreciated. Thanks in advance.

Use the debugger and step through the code to check what is expected actually happens.
Glad you found the problem... Now you can ditch the AdapterHelperclass which is a waste.

Related

Checking if two objects have the same y-axis

I have been creating a simple android game, but have been encountering some problems in checking if an object falling down has passed another object on the y-axis.
This is my code:
//SKULLY is the object falling down
//USER is the object that SKULLY will have to pass through to add a point
public void checkPassed()
{
if (skullY == user.getY())
{
scoreCount++;
txtScore.setText("SCORE: " + String.valueOf(scoreCount));
}
}
What I am trying to do is that, when SKULLY passes through or is equal to the y-axis of the USER then it will add a single point.
When I change the condition to > or < it works perfectly fine no matter what position USER is in. But when I put it in ==== no point is added.
If you have to downvote me, please leave a comment on why was I downvoted. Thanks in advance for any help or insight on this problem! :D
since its sound like a Game equals ("==") almost never happens.. my Suggestion is
if(skullY < user.getY() && skullY > user.getY() + 10) {
// Do Stuff here..
}
try to lower 10 as much as possible until you will get the desire result..

How to change color dynamically on a circle programmatically

Update: Thx for the answers, problem solved.
Yes code is missing, I am using Mapsforge library.
It had nothing to do with anything else than a bad comparison in a sqlite lookup
which resulted no calling of toggleColor(). Now it works just fine!
Hi I am having trouble changing the color of a circle drawn on a canvas. The other color represents a different state of the circle.
It works fine with onTap i.e when I tap the circle on the screen, but when I try to do it programmatically like
circle.toggleColor() and then
circle.requestRedraw() nothing happens.
How can I make this work programmatically?
#Override
public boolean onTap(LatLong geoPoint, Point viewPosition, Point tapPoint) {
if (this.contains(viewPosition, tapPoint)) {
toggleColor();
this.requestRedraw();
return true;
}
return false;
}
#Override
private void toggleColor() {
if (this.getPaintFill().equals(LongPressAction.GREEN)) {
this.setPaintFill(LongPressAction.RED);
} else {
this.setPaintFill(LongPressAction.GREEN);
}
}
Thx for answering
Yes code is missing in the snippet, I am using Mapsforge library.
It had nothing to do with anything else than a bad comparison in a sqlite lookup which resulted no calling of toggleColor(). Too much time spent on chasing that stupid mistake...
Now it works just fine!

Find out if WebView has been destroyed

If I call WebView.destroy() and I have a reference to that view somewhere else in my code, is there a way to detect that the webView has been destroyed? I was looking for something like WebView.isDestroyed().
As a hack right now I have added the following in my code. I have no idea how reliable or useful checking the context will be. Does destroy set the context to null, I quickly glanced at the source code for WebView.java in android, but it was a little over my head, almost all calls just got forwarded to mProvider, I didn't want to dig much longer if StackOverflow has my answer.
public void isWebViewDestroyed(WebView v)
{
return v.getContext() == null;
}
if(webView==null)
should be sufficient.

Android: code acts crazy

I have an extremely strange Problem, while trying to develop for android on eclipse
my program always returned non-sense statements, so I tried debugging it.
And Now I understand what the problem is....
...the code acts literally crazy - it's absolutely illogical
this is my code:
ArrayList<String[]> formatedVerses = readAndSplitDatabaseFile(database, books);
if (formatedVerses.size() < 1) { // check if readAndSplitDatabaseFile worked
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
}
return formatedVerses;
basically I create this ArrayList
But when I debug, it doesnt just go trough the code from top to bottom, it makes weird illogical jumps
the debugger arrives at the line:
ArrayList<String[]> formatedVerses = readAndSplitDatabaseFile(database, books);
then jumps to the if-statement (so far so good)
if (formatedVerses.size() < 1) {
but "formatedVerses.size() < 1" is false, so it should jump over the if-block
but instead the debugger not only jumps INTO the if-block, but it actually jumps into THE SECOND LINE of the block:
formatedVerses = readAndSplitDatabaseFile(database, books);
which makes no sense whatsoever.
I feel like somebody is playing jokes on me.
I just cant wrap my mind around this.....
I've also tried restructuring the code...
but it only gets weirder.
I've also tried other implementations. for example like this:
if (formatedVerses.size() < 1) {
return array1;
} else{
doSomethingElse();
return array2;
}
in that case, it jumps into the true-statement of the if-block
return array1;
and then, when I click on next-step, it actually JUMPS from that line to the
return array2 line inside of the else-block, while completely leaving out the doSomethingElse()-line
anybody got any ideas?
because I'm going crazy right now...
Usually when I see behavior like this, it is because I am not running the code I think I am running. I have not done Android development, so I can't speak specifically to that, but do you have "build automatically" under the project menu checked? I have found that to reduce the number of times I see this behavior tremendously.
Have you tried cleaning the code and doing a complete rebuild? Are you running this out of Eclipse or on a remote device? If on a remote device, have you published the code to the device?
As I said, I haven't done any Android development, so some of these questions may seem obvious from an Andriod developing standpoint.
Hope those ideas help.
To me, it actually sounds like your braces are misaligned in some way. It appears to be ignoring the braces around the if statement, treating it like this.
if (formatedVerses.size() < 1) // check if readAndSplitDatabaseFile worked
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
Doesn't really explain your second example though.
EDIT: Or maybe your second block is also parsing oddly.. doesn't explain why the code continues after a return statement though.
if (formatedVerses.size() < 1)
return array1;
else
doSomethingElse();
return array2;
just to elaborate on what aperkins said, which is all correct, the Eclipse debugger uses line number information from when your code was compiled into a .class file, and uses your source .java file to display what line of code is being run. So say you write this :
if (readAndSplitDatabaseFile(database, books).size() < 1) {
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
}
return formatedVerses;
and you build and run, and then you add a line of code and a blank line:
ArrayList<String[]> formatedVerses = readAndSplitDatabaseFile(database, books);
if (formatedVerses.size() < 1) { // check if readAndSplitDatabaseFile worked
createDatabase(database, false); // if not. the database wasnt initialized
formatedVerses = readAndSplitDatabaseFile(database, books); // try again
}
return formatedVerses;
now when you are debugging, you are stepping through the first example, but looking at the second. The solution as aperkins mentioned is just to build and run again, or to undo your changes until they match up with what you had when you last compiled.
Okay, I figured it out.
Strangely it was something completely different.
I made some mistakes on the complete other side of my program. It had nothing to do with this code-segment (the problems weren't even in the same class.)
once that was fixed, it started to act normal again.
I don't understand it and at this point, I don't want to understand it.
Thanks for the help, everybody.

Android java.lang.VerifyError only on 1.5

I have the following code which gets call in my main activity's onCreate method
public static ErrorReporter getInstance(){
if (instance == null){
instance = new ErrorReporter();
}
return instance;
}
Only on android 1.5 calling the above method causes java.lang.VerifyError. I am not able to figure out why this is happening. Any hints on how to solve this problem
Simply do a build on 1.5 and you'll see where is the culprit...
I got exactly the same problem when i try to set the listadatper for a listview :)
check this
private void setResultListListAdapter() {
mListAdapter_ = new ListAdapter(mContext_,
R.layout.dsg_detailed_list_row, mLstStops_);
setListAdapter(mListAdapter_);
}
gets a VerifyError before mListAdapter_ gets initialized.. so something with this...
new ListAdapter(mContext_,
R.layout.dsg_detailed_list_row, mLstStops_);
but there's nothing which is just available in 1.5 :=//
strange thing...
also in 2 other classes this code works just fine... :=)
hope someone knowes more, thanks a lot!
(everything initialized, everything checked...setListAdapter never gets called)
SOLUTION (for me)
it really was a method which wasn't supported in Android 1.5
mConvertView_.setTag(uniqueIntID, ViewHolder);
ViewHolder is a static class, instead of using normal View.gettag(),
because of different layouts i was using the above method.. so :=)
the second is supported, View.getTag()
I was using a function in ErrorReporter class which was not available in 1.5. Used reflection to take care of the unavailable function and the error is gone.

Categories

Resources