Android Open Source - AndroidModelView Flexible Model






From Project

Back to project page AndroidModelView.

License

The source code is released under:

MIT License

If you think the Android project AndroidModelView 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.khar.isframework;
/*from  ww  w  . j  a va 2  s. co  m*/
import java.util.HashMap;
import java.util.Map;

import com.khar.isframework.models.ibu.Ibu;
import com.khar.isframework.models.rs.RumahSakit;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * a flexible model that uses map to store attributes, already implements setAttr and getAttr method
 * just like Plain Old PHP Object :P
 * @author kharisma
 *
 */
public abstract class FlexibleModel extends Model {
  Map<String,String> _attr;
  public FlexibleModel(DataAccess db) {
    super(db);
    _attr = new HashMap<String, String>();
  }  

  @Override
  public ContentValues toContentValue() {
    ContentValues cv = new ContentValues();
    String[] attr = getAttributes();
    int start = 0;
    if(this.getScenario()==Model.CREATE)
      start=1;
    for(int i=start;i<attr.length;i++){
      cv.put(attr[i], (String)getAttribute(attr[i]));
    }
    return cv;
  }

  @Override
  public Object getAttribute(String attributeName) {
    return _attr.get(attributeName);
  }

  public void setAttribute(String name,String value){
    setAttribute(name, (Object)value);
  }
  @Override
  /**
   * set attribute
   * @param attributeName
   * @param value (string only please)
   */
  public void setAttribute(String attributeName, Object value) {
    try{
      _attr.put(attributeName, (String)value);
    }
    catch(ClassCastException ex){
      _attr.put(attributeName, String.valueOf(value));
    }
  }
  
  public FlexibleModel(Parcel p) {
    super(p);
    _attr = new HashMap<String, String>();
    String[] attr = getAttributes();
    for(int i=0;i<attr.length;i++){
      String h = p.readString();
      setAttribute(attr[i], h);
    }
    setScenario(p.readString());
  }
  
  @Override
  public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
  }
  
  @Override
  public void writeToParcel(Parcel dest, int flags) {
    String[] attr = getAttributes();
    dest.writeString(getTableName());
    for(int i=0;i<attr.length;i++){
      String j = (String)getAttribute(attr[i]);
      dest.writeString(j);
    }
    dest.writeString(getScenario());
  }
  
  
  public abstract FlexibleModel fromParcel(Parcel in);

  @Override
  public  abstract void fillFieldFromCursor(Cursor c);

  @Override
  public abstract Model fromCursor(Cursor c);
  
  public abstract String[] getTags();
  public static Parcelable.Creator<FlexibleModel> CREATOR = new Parcelable.Creator<FlexibleModel>() {
        public FlexibleModel createFromParcel(Parcel in) {
            String tableName = in.readString();
            if(tableName.equals("ibu"))
          return new Ibu(in);
            else if(tableName.equals("rumah_sakit"))
              return new RumahSakit(in);
            else return null;
        }

        public FlexibleModel[] newArray(int size) {
            return new FlexibleModel[size];
        }
    };  

}




Java Source Code List

com.khar.isframework.DataAccess.java
com.khar.isframework.FlexibleModel.java
com.khar.isframework.ModelAdapter.java
com.khar.isframework.ModelListFragment.java
com.khar.isframework.Model.java
com.khar.isframework.MySqliteHelper.java
com.khar.isframework.Query.java
com.khar.isframework.SqliteDataAccess.java
com.khar.isframework.formbuilder.BasicTheme.java
com.khar.isframework.formbuilder.FormBuilder.java
com.khar.isframework.formbuilder.Theme.java
com.khar.isframework.models.ibu.IbuListFragment.java
com.khar.isframework.models.ibu.Ibu.java
com.khar.isframework.models.rs.RSDetailFragment.java
com.khar.isframework.models.rs.RSListFragment.java
com.khar.isframework.models.rs.RumahSakit.java
com.khar.isframework.validator.BasicValidator.java
com.khar.isframework.validator.DateValidator.java
com.khar.isframework.validator.Validator.java
com.kharcustom.testframework.DummyDetailActivity.java
com.kharcustom.testframework.DummyDetailFragment.java
com.kharcustom.testframework.DummyEditForm.java
com.kharcustom.testframework.EmptyDetilFragment.java
com.kharcustom.testframework.FormTester.java
com.kharcustom.testframework.GenericForm.java
com.kharcustom.testframework.IbuDetailActivity.java
com.kharcustom.testframework.IbuDetailFragment.java
com.kharcustom.testframework.IbuListActivity.java
com.kharcustom.testframework.ModuleMenuHandler.java
com.kharcustom.testframework.dummy.DummyContent.java