Map TreeMap key value pair sort by value Java Android example
Map TreeMap sorted by value Java Android example.
MainClass.java
MainClass.java
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class MainClass {
public static void main(String[] arg) {
// english;germany dictionary
String[] arrayOfString = { "one;eine", "two;zwei", "three;drei",
"four;vier" };
Map<String, String> map = new TreeMap<String, String>();
for (String s : arrayOfString) {
String[] array = s.split(";");
String sKey = "", sValue = "";
if (array.length > 1) {
sKey = array[0];
sValue = array[1];
map.put(sKey, sValue);
}
}
for (Entry<String, String> entry: mapSortedByValues(map)) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> mapSortedByValues(
Map<K, V> map) {
SortedSet<Map.Entry<K, V>> sortedSetOfEntries = new TreeSet<Map.Entry<K, V>>(
new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> entry_1, Map.Entry<K, V> entry_2) {
int res = entry_1.getValue().compareTo(entry_2.getValue());
return res != 0 ? res : 1;
// return entry_1.getValue().compareTo(entry_2.getValue());
}
});
sortedSetOfEntries.addAll(map.entrySet());
return sortedSetOfEntries;
}
}
/*
three = drei
one = eine
four = vier
two = zwei
*/
396LW NO topic_id
AD
Další témata ....(Topics)
Set in AndroidManifest.xml android:theme="@android:style/Theme.NoTitleBar" AndroidManifest.xml example source code.
AndroidManifest.xml
AndroidManifest.xml
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="com.myexample.without_titlebar">
<application android:label="My app">
<activity android:name="NoTitleBar"
android:theme="@android:style/Theme.NoTitleBar"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Android development example source code
Get supported language:
// import
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
// you have to add implementation
public class Main extends Activity implements TextToSpeech.OnInitListener {
private int _langTTSavailable = -1; // set up in onInit method
// declaration
private TextToSpeech mTts;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// assigned handle - initialisation
mTts = new TextToSpeech(this,
(OnInitListener) this // TextToSpeech.OnInitListener
);
}
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
// Set preferred language to US english.
_langTTSavailable = mTts.setLanguage(Locale.US); // Locale.FRANCE etc.
if (_langTTSavailable == TextToSpeech.LANG_MISSING_DATA ||
_langTTSavailable == TextToSpeech.LANG_NOT_SUPPORTED) {
} else if ( _langTTSavailable >= 0) {
mTts.speak("Good morning",
TextToSpeech.QUEUE_FLUSH, // Drop all pending entries in the playback queue.
null);
}
} else {
// Initialization failed.
}
}
@Override
public void onDestroy() {
// TTS shutdown!
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
}
Get supported language:
private TextToSpeech mTts;
// public void onInit(int status){
int result;
String s;
result = mTts.setLanguage( Locale. CANADA ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " CANADA not supported<br>" ;}else{s+=" CANADA supported<br>";}
result = mTts.setLanguage( Locale. CANADA_FRENCH ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " CANADA_FRENCH not supported<br>" ;}else{s+=" CANADA_FRENCH supported<br>";}
result = mTts.setLanguage( Locale. CHINA ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " CHINA not supported<br>" ;}else{s+=" CHINA supported<br>";}
result = mTts.setLanguage( Locale. CHINESE ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " CHINESE not supported<br>" ;}else{s+=" CHINESE supported<br>";}
result = mTts.setLanguage( Locale. ENGLISH ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " ENGLISH not supported<br>" ;}else{s+=" ENGLISH supported<br>";}
result = mTts.setLanguage( Locale. FRANCE ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " FRANCE not supported<br>" ;}else{s+=" FRANCE supported<br>";}
result = mTts.setLanguage( Locale. FRENCH ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " FRENCH not supported<br>" ;}else{s+=" FRENCH supported<br>";}
result = mTts.setLanguage( Locale. GERMAN ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " GERMAN not supported<br>" ;}else{s+=" GERMAN supported<br>";}
result = mTts.setLanguage( Locale. GERMANY ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " GERMANY not supported<br>" ;}else{s+=" GERMANY supported<br>";}
result = mTts.setLanguage( Locale. ITALIAN ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " ITALIAN not supported<br>" ;}else{s+=" ITALIAN supported<br>";}
result = mTts.setLanguage( Locale. ITALY ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " ITALY not supported<br>" ;}else{s+=" ITALY supported<br>";}
result = mTts.setLanguage( Locale. JAPAN ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " JAPAN not supported<br>" ;}else{s+=" JAPAN supported<br>";}
result = mTts.setLanguage( Locale. JAPANESE ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " JAPANESE not supported<br>" ;}else{s+=" JAPANESE supported<br>";}
result = mTts.setLanguage( Locale. KOREA ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " KOREA not supported<br>" ;}else{s+=" KOREA supported<br>";}
result = mTts.setLanguage( Locale. KOREAN ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " KOREAN not supported<br>" ;}else{s+=" KOREAN supported<br>";}
result = mTts.setLanguage( Locale. PRC ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " PRC not supported<br>" ;}else{s+=" PRC supported<br>";}
result = mTts.setLanguage( Locale. ROOT ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " ROOT not supported<br>" ;}else{s+=" ROOT supported<br>";}
result = mTts.setLanguage( Locale. SIMPLIFIED_CHINESE ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " SIMPLIFIED_CHINESE not supported<br>" ;}else{s+=" SIMPLIFIED_CHINESE supported<br>";}
result = mTts.setLanguage( Locale. TAIWAN ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " TAIWAN not supported<br>" ;}else{s+=" TAIWAN supported<br>";}
result = mTts.setLanguage( Locale. TRADITIONAL_CHINESE ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " TRADITIONAL_CHINESE not supported<br>" ;}else{s+=" TRADITIONAL_CHINESE supported<br>";}
result = mTts.setLanguage( Locale. UK ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " UK not supported<br>" ;}else{s+=" UK supported<br>";}
result = mTts.setLanguage( Locale. US ); if (result == TextToSpeech.LANG_MISSING_DATA ||result == TextToSpeech.LANG_NOT_SUPPORTED) {s += " US not supported<br>" ;}else{s+=" US supported<br>";}
Start Stop Stopwatch Timer Android example source code.
private static long mStartTime = 0L;
Calendar cal;
TextView hTextViewVypis = (TextView)findViewById(R.id.idTextVypis);
void start(){
cal = Calendar.getInstance();
mStartTime = cal.getTimeInMillis();
}
void stop(){
prinOutStopWatchTime();
}
private void prinOutStopWatchTime() {
final long start = mStartTime;
cal = Calendar.getInstance();
long stopTime = cal.getTimeInMillis();
long millis = stopTime - start;
long milisekundy = millis % 1000;
int seconds = (int) (millis / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int hour = minutes / 60;
hour = hour % 60;
if (seconds < 10) {
hTextViewVypis.setText(hour + ":" + minutes + ":0" + seconds + ":" + milisekundy);
} else {
hTextViewVypis.setText(hour + ":" + minutes + ":" + seconds + ":" + milisekundy);
}
}
Click Handler Android Java example source code. Open new class as activity and open URL in browser.
In layout.xml file:
In string.xml
In MainClass.java file
In layout.xml file:
android:layout_height="wrap_content" android:onClick="@string/clickHandler">
In string.xml
myClickHandler
In MainClass.java file
public void myClickHandler(View view) {
switch (view.getId()) {
// open new class as activity
case R.id.btnOpenClass: {
Intent pictureActivity = new Intent(getBaseContext(),
MyClass.class);
startActivity(pictureActivity);
}
break;
case R.id.btnAbout: {
//aboutDialogCreate();
}
break;
// open url
case R.id.btnHome: {
String url = "//android.okhelp.cz/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}break;
}
}// end myClickHandler
// listener .. if click on button will scrolling to mTextView bottom
private OnClickListener mButtonListener = new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
int nBottom = mTextView.getBottom();
hScrollView.scrollTo(0, nBottom);
}
};
Google Android button example source code for developers.
// get handle
Button myButton;
myButton = (Button)findViewById(R.id.idMyButton);
//set focus
myButton.requestFocus();
// set background image
myButton.setBackgroundResource(R.drawable.backImage);
// or
myButton.setBackgroundDrawable(getResources().getDrawable( R.drawable.someImage));
// set visibility
myButton.setVisibility(View.INVISIBLE); // VISIBLE
///////// SET LISTENER
Button myButton =(Button)findViewById(R.id.button1);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "AHOJ",
Toast.LENGTH_LONG).show();
}
});
// or set onClickListener
myButton.setOnClickListener(myListener);
//end onCreate .....
private OnClickListener myListener = new OnClickListener() {
public void onClick(View v) {
}
}
Editace: 2013-12-09 13:29:30
Počet článků v kategorii: 396
Url:map-treemap-key-value-pair-sort-by-value-java-android-example