1 / 18

Presenting Lists of Data

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

lenore
Download Presentation

Presenting Lists of Data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

  8. ListView widget within an Activity

  9. 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

  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 <TextViewxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tvText" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TextView>

  12. 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);

  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. 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

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

  18. 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)); } });

More Related