How to sort an ArrayList in Java
by ventrix on Sep.22, 2009, under android, java
Example:
Class Test {
public String name;
public String sirName;
}
ArrayList<Test> res = new ArrayList<Test>();
res.add(…);
res.add(…);
Comparator<Test> comperator = new Comparator<Test>() {
@Override
public int compare(Test object1, Test object2) {
return object1.name.compareToIgnoreCase(object2.name);
}
};
Collections.sort(res, comperator);
the list is now sorted by name.
![]()
Android Keep Screen on
by ventrix on Sep.20, 2009, under android
You do not need the PowerManager.
Just call: View.setKeepScreenOn
http://developer.android.com/reference/android/view/View.html#setKeepScreenOn(boolean)
Android ListView Autoscroll
by ventrix on May.12, 2009, under android
If you are adding items in a listview and you want to autoscroll down, then you should call the
getListView().setSelection(NumberOfItems);
php string contains
by ventrix on Feb.12, 2009, under Uncategorized
$text = “this is a happy cow”;
$a = “happy”;
if (strlen(strstr($text,$a))>0) {
echo “contained”;
} else {
echo “not contained”;
}
Read all the raw data posted in a php script
by ventrix on Feb.04, 2009, under php
Read all the raw data posted in a php script
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$str = trim(file_get_contents('php://input'));
}
Java socket timeout
by ventrix on Jan.30, 2009, under android, j2ee, j2me, j2se, java
private String socketIP="192.168.1.1"; private int socketPort=2424; private int timeout=5000; //5 seconds Socket socket1 = new Socket(); socket1.connect(new InetSocketAddress(this.socketIP, this.socketPort), timeout); //or else Socket socket1 = new Socket(); socket1.connect(new InetSocketAddress(this.socketIP, this.socketPort)); socket1.setSoTimeout(timeout);
http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html
C++ for Python Programmers
by ventrix on Jan.08, 2009, under c/c++, python
C++ for Python Programmers
http://web.cse.msu.edu/~cse231/python2Cpp.html
Java float problems
by ventrix on Dec.23, 2008, under android, j2ee, j2se, java
If you want 0.6917 - 0.6911 to be 0.0006 then:
http://www.velocityreviews.com/forums/showpost.php?s=bfd8bcba71b3b530733952c65e86772a&p=1604265&postcount=4
There are three solutions:
1. If you’ve got enough accuracy but just need the numbers to look
better when you display them, then you can simply use
java.text.DecimalFormat to format the answers to a more reasonable
length when they are displayed. Rounding will occur as appropriate in
the display layer, and the result should look just fine.
2. If you aren’t getting enough precision, then you could look into the
double data type instead of float. Double has not only an immensely
larger range, but also approximately twice the precision in digits.
3. If you need exact math in decimal (often required for financial
calculations, for example) then the class java.math.BigDecimal is what
you’re looking for. BigDecimal lets you store values of any arbitrary
precision; it stores them in decimal, so you need not worry about
rounding that results from binary conversion, and you can customize the
rounding mode on operations that still need it, like division… or even
disabling rounding entirely so that the runtime will throw an exception
to let you know when a calculation won’t have an exact result.
Closing a stream
by ventrix on Dec.10, 2008, under android, j2ee, j2me, j2se, java
/**
* Closes the specified stream.
*
* @param stream The stream to close.
*/
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e("IO", "Could not close stream", e);
}
}
Use it like: closeStream(in);