android
Android URLConnection ReadUTF
by ventrix on May.08, 2010, under android
URLConnection conn;
DataInputStream dis = null;
String url = “http://test.com/lalala.html”;
StringBuffer sb1 = new StringBuffer();
try {
URL updateURL = new URL(url);
conn = updateURL.openConnection();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), “UTF8″));
String s = “”;
while ((s = rd.readLine()) != null) {
sb1.append(s);
}
Log.i(”AAAAA”, sb1.toString());
Vertical AND Horizontal scroll on android
by ventrix on May.08, 2010, under android
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:orientation=”vertical” android:background=”#919191″>
<ScrollView android:id=”@+id/ScrollView01″
android:layout_width=”wrap_content” android:layout_height=”wrap_content”>
<HorizontalScrollView android:layout_width=”wrap_content” android:layout_height=”wrap_content”>
<ImageView android:id=”@+id/ImageView01″
android:layout_width=”fill_parent” android:layout_height=”fill_parent”></ImageView>
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
Button up, listview in the middle, button bottom
by ventrix on May.08, 2010, under android
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:orientation=“vertical”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
>
<TextView
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:text=“You textview”
/>
<ListView
android:id=“@android:id/list”
android:layout_width=“fill_parent”
android:layout_weight=“1″
android:layout_height=“wrap_content” />
<TextView
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:layout_gravity=“bottom”
android:text=“Another textview but now beneath the ListView”
/>
</LinearLayout>
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);
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
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);
Copy the content of the input stream into the output stream
by ventrix on Dec.10, 2008, under android, j2ee, j2me, j2se, java
Copy the content of the input stream into the output stream
/**
* Copy the content of the input stream into the output stream, using a temporary
* byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
*
* @param in The input stream to copy from.
* @param out The output stream to copy to.
*
* @throws IOException If any error occurs during the copy.
*/
private static final int IO_BUFFER_SIZE = 4 * 1024;
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}