Android Open Source - BookWorm Table Adapter

From Project

Back to project page BookWorm.


The source code is released under:

/** ? 2014, Brandon T. All Rights Reserved. * * This file is part of the BookWorm project. * You may use this file only for your personal, and non-commercial use. * You may not modify or use ...

If you think the Android project BookWorm listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**  ? 2014, Brandon T. All Rights Reserved.
 *//w  w w .ja v a2s  .c o m
 *  This file is part of the BookWorm project.
 *  You may use this file only for your personal, and non-commercial use.
 *  You may not modify or use the contents of this file for any purpose (other
 *  than as specified above) without the express written consent of the author.
 *  You may not reproduce, republish, post, transmit, publicly display,
 *  publicly perform, or distribute in print or electronically any of the contents
 *  of this file without express consent of rightful owner.
 *  This notice must be retained in all files and may not be removed.
 *  This License is subject to change at any time without notice/warning.
 *            Author : Brandon T.
 *            Contact:

package com.example.layout;

import java.util.ArrayList;
import java.util.List;

import com.example.test.R;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TableAdapter extends ArrayAdapter<TableRow> implements Filterable {
  private Filter mFilter = null;
  private List<TableRow> dataList = null;
  private List<TableRow> mOriginalValues = null;
  public TableAdapter(Context context, List<TableRow> table) {
    super(context, R.layout.activity_main, table);
    this.dataList = table;

  public int getCount() {
    return this.dataList.size();

  public TableRow getItem(int position) {
    return this.dataList.get(position);

  public long getItemId(int position) {
    return position;
  public View getView(int position, View convertView, ViewGroup parent) {
    return new ViewHolder(getContext(), this.dataList.get(position));
  private class ViewHolder extends LinearLayout {

    public ViewHolder(Context context, TableRow row) {
      for (int i = 0; i < row.getCount(); ++i) {
        TableCell cell = row.getCell(i);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(cell.getWidth(), cell.getHeight(), cell.getWeight());
        params.setMargins(cell.getMargins()[0], cell.getMargins()[1], cell.getMargins()[2], cell.getMargins()[3]);
        if (cell.getType() == TableCellTypes.TEXT) {
          TextView view = new TextView(context);
          view.setTypeface(view.getTypeface(), cell.getTextStyle());
          this.addView(view, params);
        } else if (cell.getType() == TableCellTypes.IMAGE) {
          ImageView view = new ImageView(context);
          this.addView(view, params);
        } else if (cell.getType() == TableCellTypes.VIEW) {
          this.addView((View)cell.getValue(), params);

  public Filter getFilter() {
    if (mFilter == null) {
            mFilter = new ArrayFilter();
        return mFilter;
  private class ArrayFilter extends Filter {

    protected FilterResults performFiltering(CharSequence constraint) {
      FilterResults results = new FilterResults();
      List<TableRow> FilteredList = new ArrayList<TableRow>();
      if (mOriginalValues == null) {
                mOriginalValues = new ArrayList<TableRow>(dataList);
      if (constraint == null || constraint.length() == 0) {
                results.values = mOriginalValues;
                results.count = mOriginalValues.size();
            } else {
              constraint = constraint.toString().toLowerCase().trim();
              for (int i = 0; i < mOriginalValues.size(); ++i) {
                    String data = mOriginalValues.get(i).toString();
                    if (data.toLowerCase().contains(constraint.toString())) {
              results.count = FilteredList.size();
                results.values = FilteredList;

      return results;

    protected void publishResults(CharSequence constraint, FilterResults results) {
      dataList = (List<TableRow>) results.values;

Java Source Code List