Demo:
1)MainActivity.java:
package
com.venky.sortingarraylist;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
String value = "";
TextView tv1, tv2;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)
findViewById(R.id.textView1);
tv2 = (TextView)
findViewById(R.id.textView2);
// Create and Add Values
ArrayList<String>
list = new ArrayList<String>();
list.add("Welcome");
list.add("Congrats");
list.add("First");
list.add("A");
list.add("Z");
list.add("G");
tv1.setText("Data\n" +
list.toString());
// for sort the list
Collections.sort(list);
// set arrayList data
tv2.setText("Sorted Data\n" +
list.toString());
}
}
2)activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5F04B4"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="TextView"
android:textColor="#ffffff"
android:textSize="15dp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="TextView"
android:textColor="#ffffff"
android:textSize="15dp"
android:textStyle="bold"
/>
</LinearLayout>
3)Download this code Click Here
No comments:
Post a Comment