In the below example, first I have created ArrayAdapter class and then extend ListActivity. In next line I have store ListItems in String then I have setListAdapter. ”ArrayAdapter class can handle a list item as input, By using this class we are managing the items in the list”. You can see below program it will clearly describe you how to use ArrayAdapter in android.
public class ArrayAdapter extends ListActivity {
TextView selection;
String[] items = { "one", "two ", "three", "four"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
items));
selection=(TextView)findViewById(R.id.selection);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String text = " position:" + position + " " + items[position];
selection.setText(text);
}
}
0 Comment(s)