Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat a complex Layout for each record

I have an app that queries a database and get a recordset, on the display, I need to present these data with a row that is a complex layout. Each row contains some ImageView, many TextView etc...

It's really difficult to create the row layout programmatically, is there any way to get the entire row layout (container and children of the row layout) from an xml, edit some of the properties (like the TextViews of the row layout) and add the result to a LinearLayout?

like image 321
Marco Faion Avatar asked Dec 10 '22 11:12

Marco Faion


2 Answers

is there any way to get the entire row layout (container and childs of the row layout) from an xml

What you are looking for is how to inflate a view (LayoutInflator)

Now that you have the right term, it should be easy to find examples, inflate is popular in ListView tutorials. For an example, take a look at at getView() in this tutorial for an example:

HowTo: ListView, Adapter, getView and different list items’ layouts in one ListView
http://android.amberfog.com/?p=296

mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
...
convertView = mInflater.inflate(R.layout.item1, null);

... edit some of the properties (like the TextViews of the row layout) ...

Once you inflate the view, you can search for the widgets within it, so you can manipulate it.

holder.textView = (TextView) convertView.findViewById(R.id.text);

If your view is fairly complex and/or you will often be looking for widgets within it, I want to point out the ViewHolder technique, shown in the example referenced, relevant bits below:

// Data structure to save lookups
public static class ViewHolder {
    public TextView textView;
}
...
// Save lookups to widgets for this view in ViewHolder in tag
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.text);
view.setTag(holder);
...
// Grab saved widgets - no need to search tree for them via lookup again
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.textView.setText(mData.get(position));

... and add the result to a LinearLayout?

Presumably, you're already programatically adding to the LinearLayout, but if you want to see some code, here's an example that shows setting some layout parameters:

Android LinearLayout
http://developerlife.com/tutorials/?p=312

  // main "enclosing" linearlayout container - mainPanel
  final LinearLayout mainPanel = new LinearLayout(ctx);
  {
    mainPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                               LayoutParams.FILL_PARENT));
    mainPanel.setOrientation(LinearLayout.VERTICAL);
    ...
  }
  ...
  // top panel
  LinearLayout topPanel = new LinearLayout(ctx);
  {
    // WEIGHT = 1f, GRAVITY = center
    topPanel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                                              LayoutParams.WRAP_CONTENT,
                                              1));
    ...
  }
  ...
  // bottom panel
  LinearLayout bottomPanel = new LinearLayout(ctx);
  {
    LayoutUtils.Layout.WidthFill_HeightWrap.applyLinearLayoutParams(bottomPanel);
    ...
  }
  ...    
  // add the panels
  mainPanel.addView(topPanel);
  mainPanel.addView(bottomPanel);
  ...

Lastly, you can do alot (including custom rows) with the AdapterView / Adapter paradigm, e.g. using ListView with a SimpleCursorAdapter. It may save you some code by looking into it. Some babbling about it here:

Android ListView with different layouts for each row

like image 128
Bert F Avatar answered Dec 21 '22 04:12

Bert F


As a rule of thumb, whenever you create an instance of View using code, think twice.

Review LayoutInflater on how to create layout from resources. In your special case, you might also want to check ResourceCursorAdapter if you want to show multiple rows in a ListView.

like image 39
Michael Avatar answered Dec 21 '22 03:12

Michael