Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.

Similar presentations


Presentation on theme: "Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList."— Presentation transcript:

1 Presenting Lists of Data

2 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

3 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

4 ScrollView widget within Activity

5 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

6 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" />

7 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" />

8 ListView widget within an Activity

9 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

10 Sample.xml code - ListView <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lvList" />

11 Sample.xml code – Additional.xml file Filename: listrow.xml <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvText" android:layout_width="wrap_content" android:layout_height="wrap_content" >

12 Sample.java code – ListView ListView lv = (ListView)findViewById(R.id.lvList); … ArrayAdapter myAdapter = new ArrayAdapter (this, R.layout.listrow, R.id.tvText, myStringArray); lv.setAdapter(myAdapter);

13 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()); } });

14 ListActivity

15 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

16 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

17 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 (this, android.R.layout.simple_list_item_1, myStringArray)); }

18 Sample.java code – ListView Retrieving selected element from a ListActivity’s ListView 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)); } });


Download ppt "Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList."

Similar presentations


Ads by Google