Custom View adapter.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Custom View adapter.

Postby Ventrix » Thu Mar 05, 2009 4:53 pm

This is near to what I have in mind
Image

The view I would like to have in an Adapter is this:
viewlayout.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:layout_width="fill_parent" android:layout_height="fill_parent">
  6.  
  7.  
  8.  
  9.         <TableRow>
  10.  
  11.                 <ImageView android:id="@+id/image1" android:layout_width="wrap_content"
  12.  
  13.                         android:layout_height="wrap_content" android:layout_gravity="left"></ImageView>
  14.  
  15.                 <LinearLayout android:orientation="vertical">
  16.  
  17.                         <TextView android:id="@+id/text1" android:layout_width="wrap_content"
  18.  
  19.                                 android:layout_height="wrap_content" android:textSize="20sp"
  20.  
  21.                                 android:textStyle="bold" android:layout_gravity="center_horizontal">
  22.  
  23.                         </TextView>
  24.  
  25.  
  26.  
  27.                         <TextView android:id="@+id/text2" android:layout_width="wrap_content"
  28.  
  29.                                 android:layout_height="wrap_content" android:textSize="16sp"
  30.  
  31.                                 android:textStyle="italic" android:layout_gravity="center_horizontal">
  32.  
  33.                         </TextView>
  34.  
  35.                 </LinearLayout>
  36.  
  37.                 <ImageView android:id="@+id/image2" android:layout_width="wrap_content"
  38.  
  39.                         android:layout_height="wrap_content" android:layout_gravity="right"></ImageView>
  40.  
  41.         </TableRow>
  42.  
  43. </TableLayout>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


the constructor for the item is this:
Item.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Item {
  2.  
  3.         private Drawable firstIcon;
  4.  
  5.         private String name;
  6.  
  7.         private String date;
  8.  
  9.         private Drawable lastIcon;
  10.  
  11.  
  12.  
  13.         public Drawable getFirstIcon() {
  14.  
  15.                 return firstIcon;
  16.  
  17.         }
  18.  
  19.  
  20.  
  21.         public void setFirstIcon(Drawable firstIcon) {
  22.  
  23.                 this.firstIcon = firstIcon;
  24.  
  25.         }
  26.  
  27.  
  28.  
  29.         public String getName() {
  30.  
  31.                 return name;
  32.  
  33.         }
  34.  
  35.  
  36.  
  37.         public void setName(String name) {
  38.  
  39.                 this.name = name;
  40.  
  41.         }
  42.  
  43.  
  44.  
  45.         public String getDate() {
  46.  
  47.                 return date;
  48.  
  49.         }
  50.  
  51.  
  52.  
  53.         public void setDate(String date) {
  54.  
  55.                 this.date = date;
  56.  
  57.         }
  58.  
  59.  
  60.  
  61.         public Drawable getLastIcon() {
  62.  
  63.                 return lastIcon;
  64.  
  65.         }
  66.  
  67.  
  68.  
  69.         public void setLastIcon(Drawable lastIcon) {
  70.  
  71.                 this.lastIcon = lastIcon;
  72.  
  73.         }
  74.  
  75.  
  76.  
  77.         public Item(Drawable firstIcon, String name, String date, Drawable lastIcon) {
  78.  
  79.                 super();
  80.  
  81.                 this.firstIcon = firstIcon;
  82.  
  83.                 this.name = name;
  84.  
  85.                 this.date = date;
  86.  
  87.                 this.lastIcon = lastIcon;
  88.  
  89.         }
  90.  
  91.  
  92.  
  93. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The adapter is this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ItemAdapter extends BaseAdapter {
  2.  
  3.  
  4.  
  5.         private Context mContext;
  6.  
  7.  
  8.  
  9.         private List<Item> mItems = new ArrayList<Item>();
  10.  
  11.  
  12.  
  13.         public ItemAdapter(Context ctx) {
  14.  
  15.                 mContext = ctx;
  16.  
  17.         }
  18.  
  19.  
  20.  
  21.         public void addItem(Item it) {
  22.  
  23.                 mItems.add(it);
  24.  
  25.         }
  26.  
  27.  
  28.  
  29.         public void setListItems(List<Item> lit) {
  30.  
  31.                 mItems = lit;
  32.  
  33.         }
  34.  
  35.  
  36.  
  37.         @Override
  38.  
  39.         public int getCount() {
  40.  
  41.                 return mItems.size();
  42.  
  43.         }
  44.  
  45.  
  46.  
  47.         @Override
  48.  
  49.         public Object getItem(int arg0) {
  50.  
  51.                 return mItems.get(arg0);
  52.  
  53.         }
  54.  
  55.  
  56.  
  57.         @Override
  58.  
  59.         public long getItemId(int arg0) {
  60.  
  61.                 return arg0;
  62.  
  63.         }
  64.  
  65.  
  66.  
  67.         public View getView(int position, View convertView, ViewGroup parent) {
  68.  
  69.                 ItemView btv;
  70.  
  71.                 btv = new ItemView(mContext, mItems.get(position));
  72.  
  73.  
  74.  
  75.                 return btv;
  76.  
  77.         }
  78.  
  79.  
  80.  
  81. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


and the View that I think it has the problem is this:

ItemView.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class ItemView extends LinearLayout {
  2.  
  3.         private ImageView firstIcon;
  4.  
  5.         private TextView name;
  6.  
  7.         private TextView date;
  8.  
  9.         private ImageView lastIcon;
  10.  
  11.  
  12.  
  13.         public ItemView(Context ctx, Item vmi1) {
  14.  
  15.                 super(ctx);
  16.  
  17.  
  18.  
  19.                 this.setOrientation(HORIZONTAL);
  20.  
  21.  
  22.  
  23.                 firstIcon = new ImageView(ctx);
  24.  
  25.                 firstIcon.setImageDrawable(vmi1.getFirstIcon());
  26.  
  27.  
  28.  
  29.                 addView(firstIcon, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  30.  
  31.  
  32.  
  33.        
  34.  
  35.                 name = new TextView(ctx);
  36.  
  37.                 name.setText(vmi1.getName());
  38.  
  39.  
  40.  
  41.                 addView(name, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  42.  
  43.  
  44.  
  45.                 date = new TextView(ctx);
  46.  
  47.                 date.setText(vmi1.getDate());
  48.  
  49.                 date.setTextSize(10);
  50.  
  51.                 date.setGravity(Gravity.RIGHT);
  52.  
  53.  
  54.  
  55.                 addView(date, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  56.  
  57.                
  58.  
  59.                 lastIcon = new ImageView(ctx);
  60.  
  61.                 lastIcon.setImageDrawable(vmi1.getLastIcon());
  62.  
  63.  
  64.  
  65.                 addView(lastIcon, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  66.  
  67.  
  68.  
  69.         }
  70.  
  71.  
  72.  
  73.         public void setFirstIcon(ImageView firstIcon) {
  74.  
  75.                 this.firstIcon = firstIcon;
  76.  
  77.         }
  78.  
  79.  
  80.  
  81.         public void setName(TextView name) {
  82.  
  83.                 this.name = name;
  84.  
  85.         }
  86.  
  87.  
  88.  
  89.         public void setDate(TextView date) {
  90.  
  91.                 this.date = date;
  92.  
  93.         }
  94.  
  95.  
  96.  
  97.         public void setLastIcon(ImageView lastIcon) {
  98.  
  99.                 this.lastIcon = lastIcon;
  100.  
  101.         }
  102.  
  103.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


But I do not know how to connect the View with the layout xml I want to use...
What do I need to change?
http://www.androidgreece.gr - Greek Android Community
User avatar
Ventrix
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Mar 04, 2008 11:47 pm
Location: Greece

Top

Postby dragoff » Fri Mar 06, 2009 11:52 am

try mInflater.inflate(R.layout.your_xml.xml, null); instead your btv = new ItemView(mContext, mItems.get(position));

where LayoutInflater.from(context);
dragoff
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Feb 04, 2009 2:34 pm
Location: Odessa, Ukraine

Postby webguy » Sat Mar 14, 2009 8:28 pm

Also look at the API Demo here:
http://developer.android.com/guide/samp ... ist14.html

It also uses the inflater code also.
webguy
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Mar 14, 2009 8:18 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests