Friday, 1 March 2013

ListView in Alphabetic Order in ANDROID

Java class: 
public class MainActivity extends ListActivity {
       static final String[] items = new String[] { "Babbage","Boole", "Berners-Lee",
                      "Atanasoff", "Allen","Cormack", "Cray","Dijkstra","Dix","Dewey",
                      "Erdos","Zebra" };

    ArrayAdapter<string> arrayadapter;
    ListView listview;
    ArrayList<string> arraylist;
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        arraylist= new ArrayList<String>();
        for(int i=0;i<items.length;i++)
        {
            arraylist.add(FRUITS[i]);
        }
         Collections.sort(arraylist );//for sorting arraylist
        arrayadapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arraylist);
        listview=getListView();
        listview.setAdapter(arrayadapter);
        listview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent,View view,
                                                 int position, long id) 
            {
                Toast.makeText(getApplicationContext(),
                            ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
            }});}}
                                                                                          
                 

No comments:

Post a Comment