ArrayList Collections Add new Item Sort Find Item Java example
How add item to ArrayList, sort ArrayList, search find index of item in ArrayList, min(), max() Java basic example.
import java.util.ArrayList;
import java.util.Collections;
public class MainClass {
public static void main(String[] arg) {
String[] arrayOfString = {"nothing", "Hello", "people"
, "bye-bye", "hello", "world!", "End" };
ArrayList<String> arrayList = new ArrayList<String>();
for(String s: arrayOfString)
arrayList.add(s);
Collections.sort(arrayList);
// foreach
for (String str: arrayList)
System.out.println(str);
Object objMin = Collections.min(arrayList);
System.out.println("Min is: " + objMin);
Object objMax = Collections.max(arrayList);
System.out.println("Max is: " + objMax);
int index = Collections.binarySearch(arrayList, "people");
System.out.println("Index of people is: " + index);
}
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
*/
396LW NO topic_id
AD
Další témata ....(Topics)
Download image file from URL to ImageView Java Android source example code.
Context context = thisClass.this;
Drawable image = ImageOperations(context,
"//www.okhelp.cz/images/android/ad_4.png"
,"image.jpg");
ImageView imgView;
imgView = (ImageView)findViewById(R.id.idImageView);
imgView.setImageDrawable(image);
private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
- download any mobile styles for example //www.artodia.com/phpbb-styles/mobile/
- unzip style into your forum style folder for example all art_mobile folder copy into 0:/myweb/forum/styles/
- folow instalation istructions //www.artodia.com/phpbb-styles/mobile/tutorials/mobile-detection/
- check url result on https://www.google.com/webmasters/tools/mobile-friendly
- unzip style into your forum style folder for example all art_mobile folder copy into 0:/myweb/forum/styles/
- folow instalation istructions //www.artodia.com/phpbb-styles/mobile/tutorials/mobile-detection/
- check url result on https://www.google.com/webmasters/tools/mobile-friendly
How start certain Activity if user clicked to ListView item
Start Activity from list – launches other activities from list - latest variant!!!!
Game class Game.java game.xml
You have to add class Game to AndroidManifest.xml
Start Activity from list – launches other activities from list - latest variant!!!!
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
ListView mlistView = (ListView) findViewById(R.id.idListView);
mlistView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new String[] {"Game", "Help", "Home site"}));
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text Game, Help, Home
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
String sText = ((TextView) view).getText().toString();
Intent intent = null;
if(sText.equals("Game"))
intent = new Intent(getBaseContext(),
Game.class);
//else if(sText.equals("Help")) ..........
if(intent != null)
startActivity(intent);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Game class Game.java game.xml
You have to add class Game to AndroidManifest.xml
<activity android:name=".Game" android:label="GameLabel">
</activity>
package cz.okhelp.listview;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you have to create game.xml
setContentView(R.layout.game);
}
}
Java double is 64 bit double precision type used when fractional
precision calculation is required.
Java double je datový typ (reálné číslo) o velikosti 64 bitů. Používá se například pro přesný výsledek po dělení za desetinnou tečkou. Pokud nepotřebuje tak veliké číslo použijte raději typ float, šetříte tím paměť mobilního telefonu.
precision calculation is required.
Java double je datový typ (reálné číslo) o velikosti 64 bitů. Používá se například pro přesný výsledek po dělení za desetinnou tečkou. Pokud nepotřebuje tak veliké číslo použijte raději typ float, šetříte tím paměť mobilního telefonu.
// declaration and assignment of value type double
double x = 18.41785;
//print formated value
System.out.printf("The value of x is %.3f%n", x); // 18.418
// declaring more variables in single statement
double d1 = 12.4, d2 = 564.5, d3 = 14.589;
// double range of value
System.out.println(Double.MIN_VALUE); // 4.9E-324
System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
// is NaN Not-a-Number
double f = (double) Math.sqrt(-15);
boolean bNaN = Double.isNaN(f);
System.out.print(bNaN); // true
// check if a string is a valid number in Java example
// convert string to double Java example
String sD = "12.8";
double dParse = Double.parseDouble(sD);
// convert strings to numbers
String sDl = "15.48";
double dFromString = (Double.valueOf(sDl)).doubleValue();
// format double, float or long value to string
DecimalFormat formatter = new DecimalFormat(".##");
String s = formatter.format(-.5678); // -0.57
// .### -0.568
// .#### -0.5678
// .000000 -.567800
// -123.456
// .## -123.46
// #.## -123.46
// #E0 -.1E3
// ##E0 -1.2E2
//###E0 -123E0
// double to string in Java example code
Double dObj = new Double(68.5);
String str = dObj.toString();
// else
Double dS = 11.6;
String sdouble = dS.toString();
// compare two double variables
Double dComp1 = 4.3;
if(dComp1.equals(4.3))
System.out.print("true");
// compares the two specified double values in Java example
// int i = compare(double d1, double d2);
int i = Double.compare(11.5, 11.7); // -1 first < second
// 0 first == second
// 1 first > second
System.out.print(i);
Important: Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
Example of usage:
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 :
android.R.layout.simple_list_item_1;
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(position, true);
Example of usage:
public void updateList() {
Context ctx = getActivity();
Notes notes = new Notes(ctx);
String[] from = { Notes.COLUMN_TITLE };
int[] to = { android.R.id.text1 };
int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;
ListAdapter adapter = new SimpleCursorAdapter(ctx,
layout, notes.getNotes(), from,
to, 0);
setListAdapter(adapter);
notes.close();
}
/..................
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Set the item as checked to be highlighted when in two-pane layout
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setItemChecked(position, true);
}
Editace: 2013-12-09 10:57:01
Počet článků v kategorii: 396
Url:arraylist-collection-sort-add-java-example