180 likes | 303 Views
Presenting Lists of Data. Lists of Data. Issues involved unknown number of elements allowing the user to scroll Data sources most common ArrayList (can also use arrays) database (Cursor) other possible sources user input files. Approaches. TextView
E N D
Lists of Data • Issues involved • unknown number of elements • allowing the user to scroll • Data sources • most common • ArrayList(can also use arrays) • database (Cursor) • other possible sources • user input • files
Approaches • TextView • myTextView.setMovementMethod(new ScrollingMovementMethod()); • limited functionality • ScrollView widget within an Activity • used to allow scrolling • most widgets do not scroll by default • multiple uses, including lists • ListView widget within an Activity • most flexibility • greatest code complexity • ListActivity • direct subclass of Activity • most common approach in mobile programming • simple way to provide a list
ScrollView • Automatically allows scrolling • Two options • ScrollView • vertical scrolling • HorizontalScrollView • horizontal scrolling • Defined within associated .xml file • Contains a single child • Child can be a Layout
Sample .xml code - ScrollView <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvList" /> </ScrollView>
Sample .xml code - ScrollView <ScrollView android:layout_width=“match_parent" android:layout_height="wrap_content" android:text="Scrolling" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tvInScroll" android:text="Texting 1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Buttonning 1" /> </LinearLayout> </ScrollView>
ListView widget within an Activity • Allows for greatest functionality • additional widgets can be associated with Activity • greater code complexity • More than one column requires a Map object • expensive – generally a table layout or database should be used instead • Bound to an Adapter data source • Bound via setAdapter() method • ArrayAdapter • CursorAdapter • Adapter is associated with a widget • widget is within an additional.xml file
Sample .xml code - ListView <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lvList" />
Sample .xml code – Additional .xml file Filename: listrow.xml <TextViewxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvText" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TextView>
Sample .java code – ListView ListViewlv = (ListView)findViewById(R.id.lvList); … ArrayAdapter<String> myAdapter = new ArrayAdapter<String> (this, R.layout.listrow, R.id.tvText, myStringArray); lv.setAdapter(myAdapter);
Sample .java code – ListView Retrieving selected element from ListView lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //arg0 is the ListView //arg1 is the TextView within the View //arg2 is the position of the current TextView (same as arg3 for now) //arg3 is the selected row System.out.println(“Selected row: “ + arg3)); TextView current= (TextView)arg0.getChildAt((int) arg3); System.out.println(“Selected value: “ + current.getText()); } });
ListActivity • Class extends ListActivity • ListActivity is a direct subclass of Activity • No associated .xml file • provides for simple coding • limits functionality as no additional widgets can be used • still most common approach • setContentView() method is not used • ListActivity automatically hosts a ListView • if needed, ListView can be obtained via the getListView() method
ListActivity • ListView associated with ListActivity works similar to a ListView defined within an .xml file • Bound to an Adapter data source • bound via setListAdapter() method • ArrayAdapter • CursorAdapter • Adapter is associated with a prebuilt (Android defined) layout • android.R.layout.simple_X • X represents a valid layout • see R.layout class for exhaustive list of layouts
Sample .java code – ListActivity public class MyListActivity extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Note the absence of setContentView() call … setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, myStringArray)); } }
Sample .java code – ListView Retrieving selected element from a ListActivity’sListView getListView().setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { //arg0 is the ListView //arg1 is the TextView within the View //arg2 is the position of the current TextView (same as arg3 for now) //arg3 is the selected row System.out.println(“Selected row: “ + arg3)); System.out.println(“Selected value: “ + myArrayList.get((int)arg3)); } });