Android Open Source - Abstract-Model Model List Adapter






From Project

Back to project page Abstract-Model.

License

The source code is released under:

Apache License

If you think the Android project Abstract-Model 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

package com.logician.abstractModel;
//from  www  .  j a v a  2 s.c o  m
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.logician.abstractModel.Model.Row;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 
 * A simple ListAdapter implementation that displays a list of Models.
 * This class uses Model.getDisplayValues(Row) to get values for display.
 * The Row passed supports {@code put} operations for Strings and all primitive types (except
 * byte[]), and {@code containsKey(String key)}. The only supported {@code get} method is {@code getString(String key)}.
 * 
 * @see Model#getDisplayValues(Row)
 * @author Logician
 *
 * @param <T> The subclass of Model this adapter will be displaying.
 */
public class ModelListAdapter<T extends Model> extends BaseAdapter {
  private final LayoutInflater inflater;
  private final List<T> mModels;
  private final String[] mColumns;
  
  private final int mLayout;
  private final int[] mViews;
  
  
  /**
   * Construct a ModelListAdapter.
   * 
   * @param context The Context to use when inflating the layout.
   * @param models The Models to pull values from.
   * @param columns The column names with which to retrieve values from the Models. Should be the same length as views[].
   * @param layout The layout ID to inflate.
   * @param views The TextView IDs to map to. Should be the same length as columns[].
   */
  public ModelListAdapter(Context context, List<T> models, String[] columns, int layout, int[] views){
    inflater = LayoutInflater.from(context);
    mModels = models;
    mColumns = columns;
    mLayout = layout;
    mViews = views;
  }

  @Override
  public int getCount() {    
    return mModels.size();
  }

  @Override
  public Object getItem(int arg0) {
    return null;
  }

  @Override
  public long getItemId(int arg0) {
    return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    Model model = mModels.get(position);
    ListAdapterRow row = new ListAdapterRow();
    model.getDisplayValues(row);
    
    if(convertView == null)
      convertView = inflater.inflate(mLayout, null);
    
    TextView textView;
    for(int i = 0; i < mViews.length; i++){
      textView = (TextView) convertView.findViewById(mViews[i]);
      textView.setText(row.getString(mColumns[i]));
    }
    
    return convertView;
  }
  
  private static class ListAdapterRow implements Row{
    private final HashMap<String, String> map = new HashMap<String, String>();

    @Override
    public void put(String key, int value) {
      map.put(key, Integer.toString(value));      
    }

    @Override
    public void put(String key, boolean value) {
      map.put(key, Boolean.toString(value));
      
    }

    @Override
    public void put(String key, long value) {
      map.put(key, Long.toString(value));      
    }

    @Override
    public void put(String key, double value) {
      map.put(key, Double.toString(value));
      
    }

    @Override
    public void put(String key, float value) {
      map.put(key, Float.toString(value));
      
    }

    @Override
    public void put(String key, String value) {
      map.put(key, value);
      
    }

    @Override
    public void put(String key, byte[] value) {
      throw new UnsupportedOperationException();      
    }

    @Override
    public void put(String key, JSONObject jO) {
      throw new UnsupportedOperationException();
      
    }

    @Override
    public void put(String key, JSONArray jA) {
      throw new UnsupportedOperationException();
      
    }

    @Override
    public void put(String key, JSONSerializable jS) throws JSONException {
      throw new UnsupportedOperationException();
      
    }

    @Override
    public void put(String key, BinarySerializable bS) {
      throw new UnsupportedOperationException();
      
    }

    @Override
    public int getInt(String key) {
      throw new UnsupportedOperationException();
    }

    @Override
    public int getInt(String key, int defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean getBool(String key) {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean getBool(String key, boolean defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public long getLong(String key) {
      throw new UnsupportedOperationException();
    }

    @Override
    public long getLong(String key, long defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public double getDouble(String key) {
      throw new UnsupportedOperationException();
    }

    @Override
    public double getDouble(String key, double defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public float getFloat(String key) {
      throw new UnsupportedOperationException();
    }

    @Override
    public float getFloat(String key, float defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public String getString(String key) {
      return map.get(key);
    }

    @Override
    public String getString(String key, String defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public byte[] getByteArr(String key) {
      throw new UnsupportedOperationException();
    }

    @Override
    public byte[] getByteArr(String key, byte[] defaultValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public JSONObject getJSONObject(String key) throws JSONException {
      throw new UnsupportedOperationException();
    }

    @Override
    public JSONArray getJSONArray(String key) throws JSONException {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsKey(String key) {
      return false;
    }
    
  }

}




Java Source Code List

com.logician.abstractModel.AsyncModelFactory.java
com.logician.abstractModel.BinarySerializable.java
com.logician.abstractModel.Column.java
com.logician.abstractModel.JSONSerializable.java
com.logician.abstractModel.ModelListAdapter.java
com.logician.abstractModel.Model.java
com.logician.abstractModel.Util.java
com.logician.abstractModel.examples.MyDatabaseOpenHelper.java
com.logician.abstractModel.examples.User.java
com.logician.abstractModel.examples.UsersActivity.java