IT tutorials
 
Mobile
 

Android : Getting Fancy with Lists - Inflating Rows Ourselves

2/8/2013 11:09:17 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

The preceding version of the DynamicDemo application works fine. However, sometimes ArrayAdapter cannot be used even to set up the basics of our row. For example, it is possible to have a ListView where the rows are materially different, such as category headers interspersed among regular rows. In that case, we may need to do all the work ourselves, starting with inflating our rows. We will do that after a brief introduction to inflation.

1. A Sidebar About Inflation

"Inflation" means the act of converting an XML layout specification into the actual tree of View objects the XML represents. This is undoubtedly a tedious bit of code: take an element, create an instance of the specified View class, walk the attributes, convert those into property setter calls, iterate over all child elements, lather, rinse, and repeat.

The good news is that the fine folks on the Android team wrapped up all that into a class called LayoutInflater, which we can use ourselves. When it comes to fancy lists, for example, we want to inflate a View for each row shown in the list, so we can use the convenient shorthand of the XML layout to describe what the rows are supposed to look like.

For example, let's look at a slightly different implementation of the DynamicDemo class, from the FancyLists/DynamicEx project:

public class DynamicDemo extends ListActivity {
  TextView selection;
  private static final String[] items={"lorem", "ipsum", "dolor",
          "sit", "amet",
          "consectetuer", "adipiscing", "elit", "morbi", "vel",
          "ligula", "vitae", "arcu", "aliquet", "mollis",
          "etiam", "vel", "erat", "placerat", "ante",
          "porttitor", "sodales", "pellentesque", "augue", "purus"};

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    setListAdapter(new IconicAdapter());
    selection=(TextView)findViewById(R.id.selection);
  }

  public void onListItemClick(ListView parent, View v,
                             int position, long id) {
  selection.setText(items[position]);
  }

  class IconicAdapter extends ArrayAdapter<String> {
    IconicAdapter() {
      super(DynamicDemo.this, R.layout.row, items);
    }

    public View getView(int position, View convertView,
                       ViewGroup parent) {
      LayoutInflaterinflater=getLayoutInflater();
      View row=inflater.inflate(R.layout.row, parent, false);
      TextView label=(TextView)row.findViewById(R.id.label);

      label.setText(items[position]);

      ImageView icon=(ImageView)row.findViewById(R.id.icon);

      if (items[position].length()>4) {
        icon.setImageResource(R.drawable.delete);
      }
      else {
        icon.setImageResource(R.drawable.ok);
      }

      return(row);
    }
  }
}

					  

Here we inflate our R.layout.row layout by use of a LayoutInflater object, obtained from our Activity via getLayoutInflater(). This gives us a View object back, which, in reality, is our LinearLayout with an ImageView and a TextView, just as R.layout.row specifies. However, rather than having to create all those objects ourselves and wire them together, the XML and LayoutInflater handle the "heavy lifting" for us.

2. And Now, Back to Our Story

So we have used LayoutInflater to give us a View representing the row. This row is "empty," since the static layout file has no idea what actual data goes into the row. It is our job to customize and populate the row as we see fit before returning it, as follows:

  • Fill in the text label for our label widget, using the word at the supplied position

  • See if the word is longer than four characters and, if so, find our ImageView icon widget and replace the stock resource with a different one

The user sees nothing different—we have simply changed how those rows are being created. Obviously, this was a fairly contrived example, but you can see that this technique could be used to customize rows based on any sort of criteria.

 
Others
 
- Android : Getting Fancy with Lists - A Dynamic Presentation
- Android : Getting Fancy with Lists - Getting to First Base
- Windows Phone 7 : Getting Started with XNA - Displaying Text
- Windows Phone 7 : Getting Started with XNA - Useful Sprite Effects
- iphone Programming : Integrating Your Application - Using the Address Book
- iphone Programming : Integrating Your Application - Media Playback
- Windows Phone 8 : Controls in XAML (part 2) - RichTextBox Control, Content Controls, List Controls
- Windows Phone 8 : Controls in XAML (part 1) - Using Keyboards
- Windows Phone 8 : XAML Overview - XAML Styling
- Enter Java ME on Symbian OS : Crossing to the Native Land of Symbian OS
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us