Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

List LinkedList add sort find Item in Java example



List LinkedList Collections add sort find search Item, get searched item to string in Java example.

MainClass.java

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = {"nothing", "Hello", "people"
				, "bye-bye", "hello", "world!", "End" };
		   
                   List<String> arrayList = new LinkedList<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);
		    // print word on index 5
		    System.out.println("Index 5 is: " + arrayList.get(5));
                    String sItem = arrayList.get(5); // get item from index 5 to string
		    
		  }
}
/*
End
Hello
bye-bye
hello
nothing
people
world!
Min is: End
Max is: world!
Index of people is: 5
Index 5 is: people
*/

397LW NO topic_id



Další témata ....(Topics)


57

Change TableRow background color if on row click Android example | change-tablerow-background-color-if-on-row-click-android-example


Change table row background color if user click on row Android example code.
MainActivity.java

public class MainActivity extends Activity {
Boolean bColorYellow = true;
TextView hTextView;
TableRow hTableRow;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        hTextView = (TextView)findViewById(R.id.idTextView);
        hTableRow = (TableRow)findViewById(R.id.idTableRow1);


        
        
    } // end onCreate
  
    
    public void myTableRowClickHandler(View view) {
		switch (view.getId()) {
		case R.id.idTableRow1:{
			
			if(bColorYellow){
			hTableRow.setBackgroundColor(Color.GREEN);
			bColorYellow = false;
			}
			else{
			hTableRow.setBackgroundColor(Color.YELLOW);
			bColorYellow = true;
			}
		}
		break;
		}
  }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
    <TableRow android:id="@+id/idTableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#5655AA"
    android:onClick="@string/myTableRowClick"
    android:focusable="true">
<TextView  
	android:id="@+id/idTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    </TableRow>
</TableLayout>
    
    
</LinearLayout>



strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World!</string>
    <string name="app_name">TableRow</string>
    <string name="myTableRowClick">myTableRowClickHandler</string>
</resources>






241

Admob ads not visible on emulator Android 4.1 | admob-ads-not-visible-on-emulator-android-4-1


If you using GoogleAdMobAdsSdk-4.0.4.jar in your project and set android:targetSdkVersion="17" in AndroidManifest.xml , ads will not visible on emulator with Android 4.0.3 or 4.1.
You have to set as android:targetSdkVersion="17"


<uses-sdk android:minSdkVersion="4" 
        android:targetSdkVersion="17" />


Update project.properties file - row with target to 17:

# Project target.
target=android-17
219

Throwing multiple exceptions Java | throwing-multiple-exceptions-java


Throws multiple exceptions Java example source code

	public void callFc() 
	throws IndexOutOfBoundsException, ArithmeticException
	{
         // my code for example:
         String[] sArray = {"aa","bb"};
         String str = sArray[5]; // IndexOutOfBoundsException

         int n = 5 / 0; // ArithmeticException
        }

public void someFC (){
try{
     callFc();
    }
 catch(IndexOutOfBoundsException e){
      Log.e("TAG", e.toString());
  }
 catch(ArithmeticException e2){ 
      Log.e2("TAG",e2.toString()); 
  }
}

97

How going life cycle if screen orientation changed Android example | how-going-life-cycle-if-screen-orientation-changed-android-example


onSaveInstanceState, onRestoreInstanceState life cycle if screen orientation changed from log file.

// starts activity
15:27:12.801: INFO/onCreate(1828): onCreate()
15:27:12.811: INFO/onStart(1828): onStart()
15:27:12.821: INFO/onResume(1828): onResume()
// activity is running 

15:27:33.651: DEBUG/dalvikvm(307): GC_EXPLICIT freed 186K, 53% 
free 2770K/5831K, external 981K/1038K, paused 99ms

// change emulator state Ctrl+F11 landscape, portrait
15:27:40.427: INFO/ActivityManager(74): Config changed: 
{ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18 uiMode=17 seq=64}

// saved all variable values if need
15:27:40.581: INFO/onSaveInstanceState(1828): onSaveInstanceState()

15:27:40.602: INFO/onPause(1828): onPause()
15:27:40.612: INFO/onStop(1828): onStop()
15:27:40.631: INFO/onDestroy(1828): onDestroy()

// activity goes back to onCreate !!!!!!!!!
15:27:40.692: INFO/onCreate(1828): onCreate()
15:27:40.701: INFO/onStart(1828): onStart()

// restore all saved values of variables 
15:27:40.711: INFO/onRestoreInstanceState(1828): onRestoreInstanceState()
// you can using saved values by onSaveInstanceState() in onResume
15:27:40.721: INFO/onResume(1828): onResume()




Diagram of life cycle onSaveInstanceState, onRestoreInstanceState

[caption id="attachment_1169" align="alignleft" width="229" caption="Life cycle onRestoreInstanceState"]life_cycle_onRestoreInstanceState[/caption]
393

Android 4.4 Lenovo A6000 black screen | android-4-4-lenovo-a6000-black-screen


Try reboot device.
Start device with start button on side of phone.
Screen will go to light black color.
Long press Start button for menu:
Power off
Reboot
Airplane mode

Reboot item is in same level like Start button.
Click on the screen - (Hear a click)
Now find OK button , it is under Reboot item on right side of screen.
Press OK (Hear a click)
Wait a moment.
If device to do nothing , try short press Start button (sreen will black)
and again to press Start button. If screen go to light black color,
try again find Reboot item as written above.


Editace Jana Žákovská : 2013-12-09 10:56:55
Počet článků v kategorii: 397
Url:list-linkedlist-add-sort-find-item-in-java-example
Share
Share
Tweet