Iconified TextList - The making of

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Iconified TextList - The making of

Postby plusminus » Fri Nov 23, 2007 4:39 pm

Steven Osborn granted permission to make a tutorial out if his code.
Iconified TextList - The making of


What you will learn: You will learn how to create your own ListAdapter: IconifiedListAdapter

:?: Problems/Questions: Write it right below...

Difficulty: 1.5 of 5 :)

What it will look like:
Image


Description:
The app (screenshot above) is created with the following simple code. So that is what we finally want to reach:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
  2.  
  3.         // Add four items
  4.         itla.addItem(new IconifiedText(
  5.                         "Surf Web", getResources().getDrawable(R.drawable.favicon)));
  6.         itla.addItem(new IconifiedText(
  7.                         "Report Bug", getResources().getDrawable(R.drawable.bug)));
  8.         itla.addItem(new IconifiedText(
  9.                         "Speak Spanish", getResources().getDrawable(R.drawable.locale)));
  10.         itla.addItem(new IconifiedText(
  11.                         "Vidoop", getResources().getDrawable(R.drawable.vidoop)));
  12.         // Display it
  13.         setListAdapter(itla);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


We simply create an: IconifiedTextListAdapter, throw some String+Drawable into and 'display it. To use such awesome simple code, we need to do some work.

1. Lets start with the very Base: The IconifiedText.java which is a pretty small class, which only contains a simply constructor and some getters & setters. It is only a class that combines a String and a Drawable(Icon), nothing special!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class IconifiedText{
  2.    
  3.         private String mText = "";
  4.         private Drawable mIcon;
  5.         private boolean mSelectable = true;
  6.  
  7.         public IconifiedText(String text, Drawable bullet) {
  8.                 mIcon = bullet;
  9.                 mText = text;
  10.         }
  11.         // ...
  12. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


2. Now we take a look at the IconifiedTextListAdapter.java which consists of about 50 Lines of Code, but does not much. It basically is a storage for a list of IconifiedTexts, to which you can "add(IconifiedText);", and that passes through some functions to the elements of the IconifiedText-List, like 'getItem(int position)' or 'isSelectable(int position)'.
Being an (specialized) Adapter , the second thing to provide a function through which one can access the views (all the things) it contains:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /** @param convertView The old view to overwrite, if one is passed
  2.          * @returns a IconifiedTextView that holds wraps around an IconifiedText */
  3.         public View getView(int position, View convertView, ViewGroup parent) {
  4.                 IconifiedTextView btv;
  5.                 if (convertView == null) {
  6.                         btv = new IconifiedTextView(mContext, mItems.get(position));
  7.                 } else { // Reuse/Overwrite the View passed
  8.                         // We are assuming(!) that it is castable!
  9.                         btv = (IconifiedTextView) convertView;
  10.                         btv.setText(mItems.get(position).getText());
  11.                         btv.setIcon(mItems.get(position).getIcon());
  12.                 }
  13.                 return btv;
  14.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

That was probably also not too hard to understand (if so, ask for explanation below).

3.The IconifiedTextView is also not too hard to understand.
It extends LinearLayout. :|
:idea: Lets remember, that GUIs in Android are nested. ( A view, can contain a View, can contain a View, can contain a View, can contain a View, can contain a View...)
:arrow: So it will be no problem, that our IconifiedTextView itself consists of two more Views, aIconView and a TextView.
The IconifiedTextView does it own setup in the Constructor:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public IconifiedTextView(Context context, IconifiedText aIconifiedText) {
  2.                 super(context);
  3.  
  4.                 /* First Icon and the Text to the right (horizontal),
  5.                  * not above and below (vertical) */
  6.                 this.setOrientation(HORIZONTAL);
  7.  
  8.                 mIcon = new ImageView(context);
  9.                 mIcon.setImageDrawable(aIconifiedText.getIcon());
  10.                 // left, top, right, bottom
  11.                 mIcon.setPadding(0, 2, 5, 0); // 2px up, 5px to the right
  12.                
  13.                 /* At first, add the Icon to ourself
  14.                  * (! we are extending LinearLayout) */
  15.                 addView(mIcon,  new LinearLayout.LayoutParams(
  16.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  17.                
  18.                 mText = new TextView(context);
  19.                 mText.setText(aIconifiedText.getText());
  20.                 /* Now the text (after the icon) */
  21.                 addView(mText, new LinearLayout.LayoutParams(
  22.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  23.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


So thats it =). You successfully created an IconifiedTextList(Adapter) :!:

The full source:

:arrow: :arrow: Icons (res/drawable/*.png) used in Demo App :!:

'src/your_package_structure/TestLayout.java'
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /* $Id: TestLayout.java 57 2007-11-21 18:31:52Z steven $
  2.  *
  3.  * Copyright 2007 Steven Osborn
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.anddev.android.testproject;
  18.  
  19. import android.app.ListActivity;
  20. import android.os.Bundle;
  21.  
  22. public class TestLayout extends ListActivity {
  23.  
  24.     @Override
  25.     public void onCreate(Bundle icicle) {
  26.         super.onCreate(icicle);
  27.         IconifiedTextListAdapter itla = new IconifiedTextListAdapter(this);
  28.  
  29.         // Add four items
  30.         itla.addItem(new IconifiedText(
  31.                         "Surf Web", getResources().getDrawable(R.drawable.favicon)));
  32.         itla.addItem(new IconifiedText(
  33.                         "Report Bug", getResources().getDrawable(R.drawable.bug)));
  34.         itla.addItem(new IconifiedText(
  35.                         "Speak Spanish", getResources().getDrawable(R.drawable.locale)));
  36.         itla.addItem(new IconifiedText(
  37.                         "Vidoop", getResources().getDrawable(R.drawable.vidoop)));
  38.         // Display it
  39.         setListAdapter(itla);
  40.     }
  41. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


'src/your_package_structure/IconifiedText.java'
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /*
  2.  * Copyright 2007 Steven Osborn
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.anddev.android.testproject;
  17.  
  18. import android.graphics.drawable.Drawable;
  19.  
  20. /** @author Steven Osborn - http://steven.bitsetters.com */
  21. public class IconifiedText implements Comparable<IconifiedText>{
  22.    
  23.         private String mText = "";
  24.         private Drawable mIcon;
  25.         private boolean mSelectable = true;
  26.  
  27.         public IconifiedText(String text, Drawable bullet) {
  28.                 mIcon = bullet;
  29.                 mText = text;
  30.         }
  31.        
  32.         public boolean isSelectable() {
  33.                 return mSelectable;
  34.         }
  35.        
  36.         public void setSelectable(boolean selectable) {
  37.                 mSelectable = selectable;
  38.         }
  39.        
  40.         public String getText() {
  41.                 return mText;
  42.         }
  43.        
  44.         public void setText(String text) {
  45.                 mText = text;
  46.         }
  47.        
  48.         public void setIcon(Drawable icon) {
  49.                 mIcon = icon;
  50.         }
  51.        
  52.         public Drawable getIcon() {
  53.                 return mIcon;
  54.         }
  55.  
  56.         /** Make IconifiedText comparable by its name */
  57.         @Override
  58.         public int compareTo(IconifiedText other) {
  59.                 if(this.mText != null)
  60.                         return this.mText.compareTo(other.getText());
  61.                 else
  62.                         throw new IllegalArgumentException();
  63.         }
  64. }
  65.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


'src/your_package_structure/IconifiedTextView.java'
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /* $Id: BulletedTextView.java 57 2007-11-21 18:31:52Z steven $
  2.  *
  3.  * Copyright 2007 Steven Osborn
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.anddev.android.testproject;
  18.  
  19. import android.content.Context;
  20. import android.graphics.drawable.Drawable;
  21. import android.widget.ImageView;
  22. import android.widget.LinearLayout;
  23. import android.widget.TextView;
  24.  
  25. public class IconifiedTextView extends LinearLayout {
  26.        
  27.         private TextView mText;
  28.         private ImageView mIcon;
  29.        
  30.         public IconifiedTextView(Context context, IconifiedText aIconifiedText) {
  31.                 super(context);
  32.  
  33.                 /* First Icon and the Text to the right (horizontal),
  34.                  * not above and below (vertical) */
  35.                 this.setOrientation(HORIZONTAL);
  36.  
  37.                 mIcon = new ImageView(context);
  38.                 mIcon.setImageDrawable(aIconifiedText.getIcon());
  39.                 // left, top, right, bottom
  40.                 mIcon.setPadding(0, 2, 5, 0); // 5px to the right
  41.                
  42.                 /* At first, add the Icon to ourself
  43.                  * (! we are extending LinearLayout) */
  44.                 addView(mIcon,  new LinearLayout.LayoutParams(
  45.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  46.                
  47.                 mText = new TextView(context);
  48.                 mText.setText(aIconifiedText.getText());
  49.                 /* Now the text (after the icon) */
  50.                 addView(mText, new LinearLayout.LayoutParams(
  51.                                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  52.         }
  53.  
  54.         public void setText(String words) {
  55.                 mText.setText(words);
  56.         }
  57.        
  58.         public void setIcon(Drawable bullet) {
  59.                 mIcon.setImageDrawable(bullet);
  60.         }
  61. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


'src/your_package_structure/IconifiedTextListAdapter.java'
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /* $Id: BulletedTextListAdapter.java 57 2007-11-21 18:31:52Z steven $
  2.  *
  3.  * Copyright 2007 Steven Osborn
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.anddev.android.testproject;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import android.content.Context;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.BaseAdapter;
  26.  
  27. /** @author Steven Osborn - http://steven.bitsetters.com */
  28. public class IconifiedTextListAdapter extends BaseAdapter {
  29.  
  30.         /** Remember our context so we can use it when constructing views. */
  31.         private Context mContext;
  32.  
  33.         private List<IconifiedText> mItems = new ArrayList<IconifiedText>();
  34.  
  35.         public IconifiedTextListAdapter(Context context) {
  36.                 mContext = context;
  37.         }
  38.  
  39.         public void addItem(IconifiedText it) { mItems.add(it); }
  40.  
  41.         public void setListItems(List<IconifiedText> lit) { mItems = lit; }
  42.  
  43.         /** @return The number of items in the */
  44.         public int getCount() { return mItems.size(); }
  45.  
  46.         public Object getItem(int position) { return mItems.get(position); }
  47.  
  48.         public boolean areAllItemsSelectable() { return false; }
  49.  
  50.         public boolean isSelectable(int position) {
  51.                 try{
  52.                         return mItems.get(position).isSelectable();
  53.                 }catch (IndexOutOfBoundsException aioobe){
  54.                         return super.isSelectable(position);
  55.                 }
  56.         }
  57.  
  58.         /** Use the array index as a unique id. */
  59.         public long getItemId(int position) {
  60.                 return position;
  61.         }
  62.  
  63.         /** @param convertView The old view to overwrite, if one is passed
  64.          * @returns a IconifiedTextView that holds wraps around an IconifiedText */
  65.         public View getView(int position, View convertView, ViewGroup parent) {
  66.                 IconifiedTextView btv;
  67.                 if (convertView == null) {
  68.                         btv = new IconifiedTextView(mContext, mItems.get(position));
  69.                 } else { // Reuse/Overwrite the View passed
  70.                         // We are assuming(!) that it is castable!
  71.                         btv = (IconifiedTextView) convertView;
  72.                         btv.setText(mItems.get(position).getText());
  73.                         btv.setIcon(mItems.get(position).getIcon());
  74.                 }
  75.                 return btv;
  76.         }
  77. }
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Last edited by plusminus on Fri Dec 28, 2007 2:44 pm, edited 4 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby lordhong » Wed Nov 28, 2007 5:11 am

this is great! based on your code, i build a list w/ 3 views on one item:
checkbox, icon, and string

and it looks great! :P :P :P

there's one problem for the checkbox, i have some of them pre-selected/checked, but on application start, the ones that should have been checked are not displayed as checked. when i scroll down the list, and then come back up the list, they show up as checked. looks like somewhere i'm missing something like invalidate() after making the checkbox checked?

here's the code snippet:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. // basically have 3 views for one ImageItem
  3.  
  4. // the isChecked determines checkbox's initial state, checked or not checked
  5.  
  6. public ImageItemView(Context context, String title, Drawable drawable, boolean isChecked) {
  7.  
  8.                 super(context);
  9.  
  10.                 this.setOrientation(HORIZONTAL);
  11.  
  12.                
  13.  
  14.                 checkbox = new CheckBox(context);
  15.  
  16.                 checkbox.setChecked(isChecked);
  17.  
  18.  
  19.  
  20. // missing something invalidate() here??
  21.  
  22.  
  23.  
  24.                 addView(checkbox, new LinearLayout.LayoutParams(
  25.  
  26.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  27.  
  28.                
  29.  
  30.                 image = new ImageView(context);
  31.  
  32.                 image.setImageDrawable(drawable);
  33.  
  34.                 // left, top, right, bottom
  35.  
  36.                 image.setPadding(0, 2, 5, 2);
  37.  
  38.                 addView(image, new LinearLayout.LayoutParams(
  39.  
  40.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  41.  
  42.                
  43.  
  44.                 text = new TextView(context);
  45.  
  46.                 text.setText(title);
  47.  
  48.                 addView(text, new LinearLayout.LayoutParams(
  49.  
  50.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  51.  
  52.         }
  53.  
  54.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


in the activity class (main entry point), i init. the list adpater for ImageItem:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onCreate(Bundle icicle) {
  3.  
  4.         super.onCreate(icicle);
  5.  
  6.         ImageItemArrayAdapter adapter = new ImageItemArrayAdapter(this);
  7.  
  8.         Resources res = this.getResources();
  9.  
  10.         // checked
  11.  
  12.         adapter.addItem(new ImageItem("Home", res.getDrawable(R.drawable.sliderhouse), true));
  13.  
  14.         // not checked
  15.  
  16.         adapter.addItem(new ImageItem("Bar", res.getDrawable(R.drawable.bar), false));
  17.  
  18.         // not checked
  19.  
  20.         adapter.addItem(new ImageItem("Book Store", res.getDrawable(R.drawable.bookstore), false));
  21.  
  22.         // checked
  23.  
  24.         adapter.addItem(new ImageItem("Cafe", res.getDrawable(R.drawable.cafe), true));
  25.  
  26.  
  27.  
  28.         setListAdapter(adapter);
  29.  
  30.  
  31.  
  32. ...
  33.  
  34.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


:roll: please help, not sure where went wrong...
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Wed Nov 28, 2007 10:06 am

Hey lordhong,

Nice upgrade =)

You could try to loop through all Views at the end of the onCreate()-method and invalidate(); them. Like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // <img src="http://www.anddev.org/images/smilies/icon_exclaim.gif" alt=":!:" title="Exclamation" /> Brain to Code - NOT TESTED <img src="http://www.anddev.org/images/smilies/icon_exclaim.gif" alt=":!:" title="Exclamation" />
  2. public void onCreate(Bundle icicle) {
  3.         super.onCreate(icicle);
  4.         ImageItemArrayAdapter adapter = new ImageItemArrayAdapter(this);
  5.         Resources res = this.getResources();
  6.         // add one items
  7.         adapter.addItem(new ImageItem("Home", res.getDrawable(R.drawable.sliderhouse), true));
  8.         // add some more items...
  9.         // ...
  10.         setListAdapter(adapter);
  11.        
  12.         for(int i = 0; i < adapter.getCount(); i++){
  13.                 adapter.getView(i, null, null).invalidate();
  14.         }
  15. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4

Let us know if it worked.

I'll have a closer look at it, when I'm back home from university.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby lordhong » Fri Nov 30, 2007 4:31 am

:cry: unfortunately this does not work... i guess the invalidate does not cause the checkboxes to re-paint...
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby plusminus » Fri Nov 30, 2007 7:56 am

hmhmhm...

This sucks. It has to work, I should do some code... later :cry:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

A little clarification

Postby Soul_Est » Mon Dec 10, 2007 3:06 pm

I'd first like to say thanks for setting up these wonderful forums plusminus! They have helped me immensely when it came to understanding how to read and understand Java and other OOP languages altogether. I was planning on producing a pretty advanced media player for the platform but needed to figure out how to do the basics. The tutorials on the Google's website and Sun's website only thought me so much. I tried Steve Osborn's website, but alas I got page loading problems due to the Opera browser's rendering engine (and it had a nice simple tabs example tutorial too). When I come to anddev.org, I'm amazed at how much I understand the code just by reading through it a few times. Due to the tutorial "Iconified TextList - The Making Of" and others, I can now proceed with producing that media player...and produce a tutorial for a more advanced media player hopefully in about a month.

I was reading this piece of code and eventhough I can understand some of it, I'm having trouble understanding how convertView and btv actually work to the ends that this program is going to achieve. Would anyone be able to help me out on this?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /** @param convertView The old view to overwrite, if one is passed
  2.  
  3.       * @returns a IconifiedTextView that holds wraps around an IconifiedText */
  4.  
  5.      public View getView(int position, View convertView, ViewGroup parent) {
  6.  
  7.           IconifiedTextView btv;
  8.  
  9.           if (convertView == null) {
  10.  
  11.                btv = new IconifiedTextView(mContext, mItems.get(position));
  12.  
  13.           } else { // Reuse/Overwrite the View passed
  14.  
  15.                // We are assuming(!) that it is castable!
  16.  
  17.                btv = (IconifiedTextView) convertView;
  18.  
  19.                btv.setText(mItems.get(position).getText());
  20.  
  21.                btv.setIcon(mItems.get(position).getIcon());
  22.  
  23.           }
  24.  
  25.           return btv;
  26.  
  27.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


P.S.:Thanks again for setting up these forums plusminus! You have produced an indispensable resource for all levels of Java developers trying to produce applications for the platform.
Soul_Est
Freshman
Freshman
 
Posts: 2
Joined: Thu Nov 29, 2007 12:57 am

Top

Postby plusminus » Mon Dec 10, 2007 7:59 pm

Hello Soul_Est,

the following is a valid (simplified) replacement for the original one.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      /** @param convertView The old view to overwrite, if one is passed
  2.       * @returns a IconifiedTextView that holds wraps around an IconifiedText */
  3.      public View getView(int position, View convertView, ViewGroup parent) {
  4.           return new IconifiedTextView(mContext, mItems.get(position));
  5.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


I (or better Steven Osborn) designed this, to be a more "good-style" solution.
In some case there is probably no need for new created Views, as an "old" one (convertView) can be reused.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

mSelectable = false

Postby GodsMoon » Tue Dec 11, 2007 11:49 pm

First: Thanks for posting this. It is exactly what I was looking for.
I'm planning on having buttons in my list and don't want the actual rows selectable. However, I do want the buttons associated with the other data on the same row. (If you can think of a better way to do this let me know)

My question is this: when I declare mSelectable as false

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      private boolean mSelectable = false;
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I get the following error: An error has occurred in process <my-package>.java.lang.IndexOutOfBoundsException

When I press UP on the 5-way pad. Also, pressing down does not select the other buttons on the screen.

Is this a problem with focus? Or what am I doing wrong?

Thank for you help
GodsMoon
Developer
Developer
 
Posts: 26
Joined: Mon Dec 10, 2007 5:13 pm

Postby plusminus » Tue Dec 11, 2007 11:54 pm

Hello GodsMoon,

digg into the debugger. On which line is the exception thrown :?:

I will have a look at my source tomorrow evening, because tomorrow there is a test at university :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby plusminus » Thu Dec 13, 2007 2:53 pm

Hello GodsMoon,

the reason for this is, that by doing that change you mentioned, no more item is selectable :!:

:idea: This way to fix it:
The IndexOutOfBOundException gets thrown within the IconifiedTextListAdapter.isSelectable()-method:

:idea: Change this function:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public boolean isSelectable(int position) {
  2.                 return mItems.get(position).isSelectable();
  3.         }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

:arrow: To this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public boolean isSelectable(int position) {
  2.                 try{
  3.                         return mItems.get(position).isSelectable();
  4.                 }catch (IndexOutOfBoundsException aioobe){
  5.                         return super.isSelectable(position);
  6.                 }
  7.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


:uarrow: Change it above, too :uarrow:

Regards,
plusminus
Last edited by plusminus on Fri Dec 14, 2007 11:50 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

kinda works...

Postby GodsMoon » Thu Dec 13, 2007 9:13 pm

Thanks! know I don't get the error.
However, that makes everything unselectable. Even the other buttons outside the List Adapter, well at least, they are not selectable with the 5-way pad. As soon as I click any of the buttons with the mouse Everything is selectable again even items in the list that are explicitly set to not be selectable.

Almost as if the list adapter sets the selectable for the whole activity and clicking any of the buttons with the mouse sets it to true.

Any ideas? Could this be a bug?

thanks again.
GodsMoon
Developer
Developer
 
Posts: 26
Joined: Mon Dec 10, 2007 5:13 pm

cut and paste error

Postby udi » Fri Dec 14, 2007 10:21 pm

very nice tutorial, thank you.
minor nit: the method IsSelectable() on IconifiedText seems to be a cut and paste from IconifiedTextListAdapter.
udi
Freshman
Freshman
 
Posts: 6
Joined: Fri Dec 14, 2007 12:21 am
Location: Austin TX

Postby plusminus » Fri Dec 14, 2007 11:53 pm

:oops: yes, I replaced the issue from 3 posts above one the right && on the wrong position ;)

Thx for mentioning :)
:src: Fixed

Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby udi » Mon Dec 17, 2007 10:50 pm

plusminus wrote:hmhmhm...

This sucks. It has to work, I should do some code... later :cry:

Regards,
plusminus


thnx again for the helpful tutorial.
it seems that if you accumulated IconifiedTextView rather than IconifiedText in the list of the adapter it would make the code somewhat simpler. you can call the constructor of IconifiedTextView in addItem(IconfiedText it) as follows:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public void addItem(IconifiedText it) {
  2.         _items.add(new IconifiedTextView(_context, it));
  3.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


that would reduce getView to:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public View getView(int position, View convertView, ViewGroup parent) {
  2.         return _items.get(position);
  3.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


/udi
udi
Freshman
Freshman
 
Posts: 6
Joined: Fri Dec 14, 2007 12:21 am
Location: Austin TX

Postby adnan1146 » Thu Jan 10, 2008 11:38 am

plusminus wrote:Hey lordhong,

Nice upgrade =)

You could try to loop through all Views at the end of the onCreate()-method and invalidate(); them. Like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // <img src="http://www.anddev.org/images/smilies/icon_exclaim.gif" alt=":!:" title="Exclamation" /> Brain to Code - NOT TESTED <img src="http://www.anddev.org/images/smilies/icon_exclaim.gif" alt=":!:" title="Exclamation" />
  2. public void onCreate(Bundle icicle) {
  3.         super.onCreate(icicle);
  4.         ImageItemArrayAdapter adapter = new ImageItemArrayAdapter(this);
  5.         Resources res = this.getResources();
  6.         // add one items
  7.         adapter.addItem(new ImageItem("Home", res.getDrawable(R.drawable.sliderhouse), true));
  8.         // add some more items...
  9.         // ...
  10.         setListAdapter(adapter);
  11.        
  12.         for(int i = 0; i < adapter.getCount(); i++){
  13.                 adapter.getView(i, null, null).invalidate();
  14.         }
  15. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4

Let us know if it worked.

I'll have a closer look at it, when I'm back home from university.

Regards,
plusminus


Hi plusminus

I have also modified the iconified list to have few checkboxes,text views and imagesviews, but I am facing the similar problem of list refresh after performing some action. If you could help in this regard, and write some code to refresh the list.

I also tried the code you written before using invalidate();
adnan1146
Freshman
Freshman
 
Posts: 4
Joined: Wed Jan 09, 2008 2:31 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests