Tuesday, 10 September 2013

how to set array list to list view in android

say you have a array list like as follows

String names[] =new String[]{"first","second","third"};

how do we set to a listview in android

ListView lv2=(ListView)findViewById(R.id.listView2);
        ArrayAdapter <String> adapter1 = new ArrayAdapter<String>(MainActivity.this,
        android.R.layout.simple_list_item_1, android.R.id.text1,names);
lv2.setAdapter(adapter1);


here names is the list of values you wanna set for array list



to implement click on array list and return position on which where it was clicked

lv2.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
        final int position, long id) {
         pos=position;

}});

variable pos hold the value of position in where listview is clicked

No comments:

Post a Comment