• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
List adapter with multiple objects

List adapter with multiple objects






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    List adapter with multiple objects List adapter with multiple objects Presentation Transcript

    • Disclaimer:This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
    • LIST ADAPTER WITH MULTIPLE OBJECTS• FREDDY PV email/facebook id : f4freddy@gmail.com
    • What is an adapter in android?• 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.
    • What is AdapterView Class?• The AdapterView is a child class of ViewGroup> A special kind of container of view objects (list items)• Typically you are going to use subsclasses of AdapterView class instead of using it directly• Example subclasses of AdapterView class> ListView> Spinner> Gallery• An AdapterView access the data through Adapter object Instead of accessing data directly itself
    • ListView• ListView is a view group that displays a list of scrollable items. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database query and converts each item result into a view thats placed into the list.
    • First create a class for listactivitypublic class ListMobileActivity extends ListActivity { static final String[] name = new String[] { "freddy", "jaison", "kiran", "syam"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_mobile, // R.id.label, MOBILE_OS)); setListAdapter(new MobileArrayAdapter(this, name)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { //get selected items String selectedValue = (String) getListAdapter().getItem(position); Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show(); }}
    • Then create a class for the arrayadapterpublic class MobileArrayAdapter extends ArrayAdapter<String> { private final Context context; private final String[] values; public MobileArrayAdapter(Context context, String[] values) { super(context, R.layout.list_name, values); this.context = context; this.values = values; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View rowView = inflater.inflate(R.layout.list_name, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.label); ImageView imageView = (ImageView) rowView.findViewById(R.id.logo); textView.setText(values[position]); // Change icon based on name String s = values[position]; System.out.println(s); if (s.equals("freddy")) { imageView.setImageResource(R.drawable.freddy_image); } else if (s.equals("jaison")) { imageView.setImageResource(R.drawable.jaison_image); } else if (s.equals("kiran")) { imageView.setImageResource(R.drawable.kiran_image); } else { imageView.setImageResource(R.drawable.syam_imge); } return rowView; }}
    • Then create an xml file to view this list•• <?xml version="1.0" encoding="utf-8"?>• <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"• android:layout_width="wrap_content"• android:layout_height="wrap_content"• android:padding="5dp" >•• <ImageView• android:id="@+id/logo"• android:layout_width="50px"• android:layout_height="50px"• android:layout_marginLeft="5px"• android:layout_marginRight="20px"• android:layout_marginTop="5px"• android:src="@drawable/windowsmobile_logo" >• </ImageView>•• <TextView• android:id="@+id/label"• android:layout_width="wrap_content"• android:layout_height="wrap_content"• android:text="@+id/label"• android:textSize="30px" >• </TextView>•• </LinearLayout>•
    • • If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance.• www.baabtra.com | www.massbaab.com |ww w.baabte.com