+ All Categories
Home > Documents > Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more...

Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more...

Date post: 27-Jul-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
45
1 Array ArrayList Cursor Spinner ListView RecylcerView
Transcript
Page 1: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

1

• Array• ArrayList• Cursor

• Spinner• ListView• RecylcerView

Page 2: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Adapter

• An Adapter object acts as a bridge between an AdapterView and the underlying data for that view

• The Adapter provides access to the data items

• The Adapter is also responsible for making a View for each item in the data set

• Adapters provide a common interface to the data model behind a selection-style widget, such as a listbox, spinner etc

2www.sisoft.in

Page 3: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

• Android's adapters are responsible for providing the roster of data for widgets/views plus converting individual elements of data into specific views to be displayed inside the widget/views

• The adapter is assigned to the View via the setAdapter method on the View object.

• An adapter extend the BaseAdapter class. Android provides some standard adapters. The most important are ArrayAdapterand CursorAdapter.

3www.sisoft.in

Use of Adapter

Page 4: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Types of Adapters

• Base Adapter

– ArrayAdapter

– SimpleAdapter

– CursorAdapter

• SimpleCursorAdapter

4www.sisoft.in

Page 5: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

• BaseAdapter is an abstract base class for the Adapter interface to simplify implementing adapters

• You could implement your own, but the framework provides some pretty flexible adapters already

5www.sisoft.in

Page 6: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Array Adapter

• Binds an array of data to a view

• override getView() to inflate, populate, and return a custom view for the given index in the array.

• The getView() method includes an opportunity to reuse views via the convertView parameter.

6www.sisoft.in

Page 7: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Using ArrayAdapter

• The ArrayAdapter can handle Java array or java.util.List as input eg

String[] items={"this", "is", "a","really", "silly", "list"};

new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

7www.sisoft.in

Page 8: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

ArrayAdapter constructor takes three parameters:

• The Context to use (typically this will be your activity instance)

• The resource ID of a view to use (such as a built-in system resource ID, as shown above)

• The actual array or list of items to use

8www.sisoft.in

Page 9: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

• Backing the list as an ArrayList of Maps

• Each entry in ArrayList corresponds to an row

• The Maps contains the data for each column

9www.sisoft.in

Page 10: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

ArrayList<HashMap<String,Object>> listdata=new ArrayList<HashMap<String,Object>>();

for (int i=0;i<3;i++) {

HashMap<String, Object> hm = new HashMap<String, Object>();

hm.put("title", "title");

hm.put("context", "context");

listdata.add(hm); }

String[] from = {"title", "context"};

int[] to={R.id.title,R.id.context};

SimpleAdapter adapter = new SimpleAdapter(this, listdata, R.layout.list_item, from, to);

10www.sisoft.in

Page 11: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Cursor Adapter

• Binds data from a cursor (like a database cursor) to a view

• Abstract so you don't use it directly, use a subclass or derive your own

• Implement the abstract method newView() to inflate, populate, and return the desired view for the current cursor position

• Implement the abstract method bindView() to populate an existing view that is being reused

11www.sisoft.in

Page 12: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Simple Cursor Adapter

• Concrete implementation of CursorAdapter

• It can take a row layout and a mapping of cursor columns to row layout widgets

• Supports text and images, but can customize using setViewText and setViewImage

• Can support other types and can customize bindings through a hook

12www.sisoft.in

Page 13: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

An adapter needs to know the following in its constructor//the context in which it will operate (required)//the source data (required)//the list item layout (required)//the target fields -- if more than one. //cursor – if this is a cursorAdapter

//you must then set the adapter to the Target View 13www.sisoft.in

Page 14: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

14www.sisoft.in

Adapter View: Class Diagram

Page 15: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

AdapterView

• An AdapterView is a view whose children are determined by an Adapter. Commonly used subclasses of Adapterview are as follows:

– Spinner

– ListView

– RecyclerView

15www.sisoft.in

Page 16: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Spinner

• In Android, the Spinner is the equivalent of the drop-down selector you might find in other toolkits (e.g., JComboBox in Java/Swing)

• Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value

• You can add a spinner to your layout with the Spinner object. You should usually do so in your XML layout with a <Spinner> element.

16www.sisoft.in

Page 17: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Spinner

• In Android, the Spinner is the equivalent of the drop-down selector you might find in other toolkits (e.g., JComboBox in Java/Swing)

• Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value

• You can add a spinner to your layout with the Spinner object. You should usually do so in your XML layout with a <Spinner> element.

17www.sisoft.in

Page 18: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Spinner : Choices population

• Using XML

– android:entries (Reference to an array resource)

• Invoke setAdapter to add Choices in spinner. The data may come either from ArrayAdapter, if choices are available in array or from Cursor Adapter, if choices are available in database

18www.sisoft.in

Page 19: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Spinner : Selection

• When the user selects an item from the drop-down, the Spinner object receives an on-item-selected event and its interface implementation be specified by calling setOnItemSelectedListener()

• To define the selection event handler for a spinner, implement the AdapterView.OnItemSelectedListener interface. This interface requires the onItemSelected() and onNothingSelected() callback methods

19www.sisoft.in

Page 20: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent” >

<TextViewandroid:id="@+id/selection"android:layout_width="fill_parent"android:layout_height="wrap_content"/>

<Spinner android:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"android:drawSelectorOnTop="true"/></LinearLayout>

20www.sisoft.in

Page 21: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

public class SpinnerDemo extends Activityimplements AdapterView.OnItemSelectedListener {TextView selection;String[] items= {“Table“, “Chair”, “Laptop”, “Mobile”, “Desktop”,”File”};

@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(R.layout.main);selection=(TextView)findViewById(R.id.selection);Spinner spin=(Spinner)findViewById(R.id.spinner);spin.setOnItemSelectedListener(this);ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items);

21www.sisoft.in

Page 22: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spin.setAdapter(aa);}

public void onItemSelected(AdapterView parent, View v,

int position, long id) {selection.setText(items[position]);}public void onNothingSelected(AdapterView parent) {

selection.setText("");}

}

22www.sisoft.in

Page 23: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

23www.sisoft.in

Page 24: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

24www.sisoft.in

Page 25: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

ListView

• The classic listbox widget in Android is known as ListView. Following need to be implemented for listview

– Include listview in your layout

– Invoke setAdapter() to supply data and child views

– Attach a listener via setOnItemSelectedListener() to find out when the selection has been made

25www.sisoft.in

Page 26: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Custom Adapter & List View

• Design layout XML file, which will represent one row in the ListView

• Extend the Adapter class

• Implement constructor to pass data to sub class

• Override getView method to map each row with data elements

26www.sisoft.in

Page 27: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Custom Adapter & List View

• @Override• public int getCount() {

• return topic_qcount.length;• }

• @Override• public Object getItem(int position) {

• return position;• }

• @Override• public long getItemId(int position) {

• return position;• }

• @Override• public View getView(final int position, View convertView, ViewGroup parent) {

• }

27www.sisoft.in

Page 28: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Recycle View & Card View

www.sisoft.in 28

Page 29: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

• To create complex lists and cards with material design styles in yourapps, you can use the RecyclerView and CardView widgets.

• The RecyclerView widget is a more advanced and flexible versionof ListView. This widget is a container for displaying large data setsthat can be scrolled very efficiently by maintaining a limited numberof views.

• Use the RecyclerViewwidget when you have data collections whoseelements change at runtime based on user action or network events.

• To use the RecyclerView widget, you have to specify an adapter and alayout manager. To create an adapter, extendthe RecyclerView.Adapter class.

www.sisoft.in 29

Recycle View

Page 30: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

• RecyclerView provides these built-in layout managers:

• LinearLayoutManager shows items in a vertical or horizontal scrolling list.

• GridLayoutManager shows items in a grid.

• StaggeredGridLayoutManager shows items in a staggered grid.

• To create a custom layout manager, extend theRecyclerView.LayoutManager class.

www.sisoft.in 30

Recycle View

Page 31: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

To create Recycle View do following steps.

You need to import appcompat_v7 library if it is not important by default.

1. Create xml file and add v7.widget.RecycleView.

2. To feed data to RecyleView, create Custom Adapter class MyAdapter thatextends RecyclerView.Adapter <MyAdapter.ViewHolder>

3. Now in activity, need to get handle of RecycleView object, connect it to alayout manager, and attach an adapter for the data to be displayed:

4. Create Item Class to show the data in Recycle View.

www.sisoft.in 31

Recycle View

Page 32: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

RecyclerView provides these built-in layout managers:

• LinearLayoutManager shows items in a vertical or horizontal scrolling list.

• GridLayoutManager shows items in a grid.

• StaggeredGridLayoutManager shows items in a staggered grid.

• To create a custom layout manager, extend theRecyclerView.LayoutManager class.

www.sisoft.in 32

Recycle View

Page 33: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Create a xml file and add RecycleView.

www.sisoft.in 33

Page 34: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="match_parent“>

<android.support.v7.widget.RecyclerView

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/recyclerView"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

/>

</RelativeLayout>

www.sisoft.in 34

Page 35: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Create a xml file for item layout

www.sisoft.in 35

Page 36: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="80dp"

android:background="@drawable/border"

>

<!-- icon -->

<ImageView

android:id="@+id/item_icon"

android:layout_width="64dp"

android:layout_height="64dp"

android:layout_alignParentLeft="true"

android:layout_marginLeft="8dp"

android:layout_marginRight="8dp"

android:layout_marginTop="1dp"

android:layout_marginBottom="1dp"

android:contentDescription="icon” android:src="@drawable/ic_launcher"

/>

www.sisoft.in 36

<!-- title --><TextView

android:id="@+id/item_title"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_toRightOf="@+id/item_icon

"android:layout_alignBaseline="@+id/item_i

con"android:textColor="@android:color/darker_

gray"android:layout_marginLeft="8dp"android:layout_marginRight="8dp"android:layout_marginTop="8dp"android:textSize="22dp" />

</RelativeLayout>

Page 37: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Create Main Activity Class

www.sisoft.in 37

Page 38: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

public class MainActivity extends Activity {

@Overrideprotected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 1. get a reference to recyclerView RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

// this is data fro recycler viewItemData itemsData[] = { new ItemData("Help",R.drawable.help),

new ItemData("Delete",R.drawable.content_discard),new ItemData("Cloud",R.drawable.collections_cloud),new ItemData("Favorite",R.drawable.rating_favorite),new ItemData("Like",R.drawable.rating_good),new ItemData("Rating",R.drawable.rating_important)};

// 2. set layoutMangerrecyclerView.setLayoutManager(new LinearLayoutManager(this));// 3. create an adapter MyAdapter mAdapter = new MyAdapter(itemsData);// 4. set adapterrecyclerView.setAdapter(mAdapter);// 5. set item animator to DefaultAnimatorrecyclerView.setItemAnimator(new DefaultItemAnimator());

}}

www.sisoft.in 38

Page 39: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Create MyAdapter Class & extends RecyclerView.Adapter <MyAdapter .

ViewHolder>

www.sisoft.in 39

Page 40: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

public class MyAdapter extends RecyclerView.Adapter <MyAdapter.ViewHolder> {

private ItemData [] itemsData;

public MyAdapter (ItemData[] itemsData) {

this.itemsData = itemsData;

}

// Create new views (invoked by the layout manager)

@Override

public MyAdapter . ViewHolder onCreateViewHolder (ViewGroup parent,

int viewType) {

// create a new view

View itemLayoutView = LayoutInflater.from (parent.getContext())

.inflate(R.layout.item_layout, null);

// create ViewHolder

ViewHolder viewHolder = new ViewHolder(itemLayoutView);

return viewHolder;

www.sisoft.in 40

Page 41: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

}

// Replace the contents of a view (invoked by the layout manager)

@Override

public void onBindViewHolder (ViewHolder viewHolder , int position) {

// - get data from your itemsData at this position

// - replace the contents of the view with that itemsData

viewHolder.txtViewTitle.setText(itemsData[position].getTitle());

viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());

}

// inner class to hold a reference to each item of RecyclerView

public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView txtViewTitle;

public ImageView imgViewIcon;

www.sisoft.in 41

Page 42: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

public ViewHolder(View itemLayoutView) {

super(itemLayoutView);

txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);

imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);

}

}

// Return the size of your itemsData (invoked by the layout manager)

@Override

public int getItemCount() {

return itemsData.length;

}

}

www.sisoft.in 42

Page 43: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Create a class Item

www.sisoft.in 43

Page 44: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

public class ItemData {

private String title;

private int imageUrl;

public ItemData(String title,int imageUrl){

this.title = title;

this.imageUrl = imageUrl;

}

// getters & setters

}

www.sisoft.in 44

Page 45: Array Spinner ArrayList ListView Curso RecylcerVie · • The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large

Run

www.sisoft.in 45


Recommended