diff --git a/README.md b/README.md index 99223f1..27650ae 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,12 @@ Android 开源项目 Demo ### 添加规范 各项目需要新建一个属于自己的文件夹,用于后续上传该开源库使用示例工程代码 -- 该文件夹以`开源库名-demo`命名,全小写,单词间用`-`连接。如果已有该文件夹,则以`开源库名-demo-${GitHub 用户名}`命名,如`event-bus-demo-trinea`; +- 该文件夹以`开源库名-demo`命名,全小写,单词间用`-`连接。 +如果已有该文件夹,则以`开源库名-demo-${GitHub 用户名}`命名,如`event-bus-demo-trinea`; - 示例工程要求覆盖到该开源项目所有功能,不允许拷贝官方 Demo; -- 若没有自己的 Code Format 文件,使用 [common](https://github.com/android-cn/android-open-project-demo/tree/master/common) 文件夹下 code format,code template 文件; +- 若没有自己的 Code Format 文件,使用 [common](common) 文件夹下 code format,code template 文件; - 文件夹下需要有名为 apk 的子文件夹,用于存放可运行 APK 文件; - 文件夹下需要有名为 README.md 的介绍文件,其中包含以下内容。 (1). Demo Download (2). Screenshot 截图可使用 [licecap](http://www.cockos.com/licecap/) -具体可参考:[EventBus Demo ReadMe](https://github.com/android-cn/android-open-project-demo/tree/master/event-bus-demo) +具体可参考:[EventBus Demo ReadMe](event-bus-demo) diff --git a/active-android-demo/.gitignore b/active-android-demo/.gitignore new file mode 100644 index 0000000..afbdab3 --- /dev/null +++ b/active-android-demo/.gitignore @@ -0,0 +1,6 @@ +.gradle +/local.properties +/.idea/workspace.xml +/.idea/libraries +.DS_Store +/build diff --git a/active-android-demo/active-android-demo.iml b/active-android-demo/active-android-demo.iml new file mode 100644 index 0000000..2a02201 --- /dev/null +++ b/active-android-demo/active-android-demo.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/active-android-demo/app/.gitignore b/active-android-demo/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/active-android-demo/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/active-android-demo/app/app.iml b/active-android-demo/app/app.iml new file mode 100644 index 0000000..8d27a9e --- /dev/null +++ b/active-android-demo/app/app.iml @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/active-android-demo/app/build.gradle b/active-android-demo/app/build.gradle new file mode 100644 index 0000000..074d092 --- /dev/null +++ b/active-android-demo/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' +repositories { + mavenCentral() + maven { url "https://oss.sonatype.org/content/repositories/snapshots/" } +} + + +android { + compileSdkVersion 21 + buildToolsVersion "21.1.1" + + defaultConfig { + applicationId "ouyang.activeandroid" + minSdkVersion 15 + targetSdkVersion 21 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:support-v4:21.+' + compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT' + compile 'com.google.code.gson:gson:2.3.1' + compile project(':library') +} diff --git a/active-android-demo/app/proguard-rules.pro b/active-android-demo/app/proguard-rules.pro new file mode 100644 index 0000000..2b76c9d --- /dev/null +++ b/active-android-demo/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in D:\android-sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/active-android-demo/app/src/androidTest/java/ouyang/activeandroid/ApplicationTest.java b/active-android-demo/app/src/androidTest/java/ouyang/activeandroid/ApplicationTest.java new file mode 100644 index 0000000..61ed873 --- /dev/null +++ b/active-android-demo/app/src/androidTest/java/ouyang/activeandroid/ApplicationTest.java @@ -0,0 +1,13 @@ +package ouyang.activeandroid; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/active-android-demo/app/src/main/AndroidManifest.xml b/active-android-demo/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..2a001e5 --- /dev/null +++ b/active-android-demo/app/src/main/AndroidManifest.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodActivity.java b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodActivity.java new file mode 100644 index 0000000..9ea9e31 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodActivity.java @@ -0,0 +1,84 @@ +package ouyang.activeandroid; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.Toast; + +import com.activeandroid.query.Select; +import com.tjerkw.slideexpandable.library.ActionSlideExpandableListView; + +import java.util.List; + +import ouyang.adapter.FoodAdapter; +import ouyang.model.Food; + +/** + * Created by zuxiang on 2015/2/8. + */ +public class FoodActivity extends Activity { + ActionSlideExpandableListView mListView; + List mFoods; + FoodAdapter mAdapter; + + private long mId; + public static final int FOOD_RESULT = 0 << 1; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_food); + getActionBar().setDisplayHomeAsUpEnabled(true); + + mListView = (ActionSlideExpandableListView) findViewById(R.id.listview); + mId = getIntent().getLongExtra("id", 0L); + mFoods = new Select().from(Food.class).where("cid=?", mId).execute(); + mAdapter = new FoodAdapter(this, mFoods, mId); + mListView.setAdapter(mAdapter); + mListView.setItemActionListener(new ActionSlideExpandableListView.OnActionClickListener() { + @Override + public void onClick(View listView, View buttonview, int position) { + Food food = (Food) mAdapter.getItem(position); + if (buttonview.getId() == R.id.btn_update) { + Intent intent = new Intent(FoodActivity.this, UpdateFoodActivity.class); + intent.putExtra("food", food); + startActivityForResult(intent, FOOD_RESULT); + } + + if (buttonview.getId() == R.id.btn_delete) { + mAdapter.delete(food.pk_id); + } + } + }, R.id.btn_update, R.id.btn_delete); + } + + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == FOOD_RESULT) { + mFoods = new Select().from(Food.class).where("cid=?", mId).execute(); + mAdapter.replaceAll(mFoods); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } +} + + + + + diff --git a/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCategoryActivity.java b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCategoryActivity.java new file mode 100644 index 0000000..81be450 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCategoryActivity.java @@ -0,0 +1,65 @@ +package ouyang.activeandroid; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.app.NavUtils; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; + +import com.activeandroid.ActiveAndroid; +import com.activeandroid.query.Select; + +import java.util.ArrayList; +import java.util.List; + +import ouyang.adapter.FoodCategoryAdapter; +import ouyang.model.Category; +import ouyang.model.Food; + +/** + * Created by zuxiang on 2015/2/8. + */ +public class FoodCategoryActivity extends Activity { + ListView mListView; + FoodCategoryAdapter mAdapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_activeandroid); + getActionBar().setDisplayHomeAsUpEnabled(true); + + mListView = (ListView) findViewById(R.id.listview); + List foodCatogroys = new Select().from(Category.class).execute(); + mAdapter = new FoodCategoryAdapter(this, foodCatogroys); + mListView.setAdapter(mAdapter); + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Intent intent = new Intent(FoodCategoryActivity.this, FoodActivity.class); + intent.putExtra("id", id); + startActivity(intent); + } + }); + + + } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } + + + + + + +} diff --git a/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCategoryCpActivity.java b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCategoryCpActivity.java new file mode 100644 index 0000000..f5835c0 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCategoryCpActivity.java @@ -0,0 +1,82 @@ +package ouyang.activeandroid; + +import android.app.Activity; +import android.app.LoaderManager; +import android.content.CursorLoader; +import android.content.Intent; +import android.content.Loader; +import android.database.Cursor; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; +import android.widget.SimpleCursorAdapter; + +import com.activeandroid.content.ContentProvider; + +import java.util.ArrayList; +import java.util.List; + +import ouyang.adapter.FoodAdapter; +import ouyang.adapter.FoodCategoryAdapter; +import ouyang.adapter.FoodCategoryCursorAdapter; +import ouyang.model.Category; + +/** + * Created by zuxiang on 2015/2/11. + */ +public class FoodCategoryCpActivity extends Activity implements LoaderManager.LoaderCallbacks{ + ListView mListView; + private FoodCategoryCursorAdapter mAdapter; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_activeandroid); + getActionBar().setDisplayHomeAsUpEnabled(true); + + + mListView = (ListView)findViewById(R.id.listview); + mAdapter = new FoodCategoryCursorAdapter(this); + mListView.setAdapter(mAdapter); + + + getLoaderManager().initLoader(0, null,this); + mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Intent intent = new Intent(FoodCategoryCpActivity.this, FoodCpActivity.class); + intent.putExtra("id", parent.getAdapter().getItemId(position)); + startActivity(intent); + } + }); + } + + @Override + public Loader onCreateLoader(int id, Bundle args) { + return new CursorLoader(this, + ContentProvider.createUri(Category.class, null), + null, null, null, null + ); + } + + @Override + public void onLoadFinished(Loader loader, Cursor data) { + mAdapter.swapCursor(data); + } + + @Override + public void onLoaderReset(Loader loader) { + mAdapter.swapCursor(null); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCpActivity.java b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCpActivity.java new file mode 100644 index 0000000..bdb1254 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/activeandroid/FoodCpActivity.java @@ -0,0 +1,79 @@ +package ouyang.activeandroid; + +import android.app.Activity; +import android.app.LoaderManager; +import android.content.CursorLoader; +import android.content.Intent; +import android.content.Loader; +import android.database.Cursor; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.View; +import android.widget.ListView; + +import com.activeandroid.content.ContentProvider; +import com.activeandroid.query.Delete; +import com.tjerkw.slideexpandable.library.ActionSlideExpandableListView; + +import java.util.List; + +import ouyang.adapter.FoodAdapter; +import ouyang.adapter.FoodCategoryCursorAdapter; +import ouyang.adapter.FoodCursorAdapter; +import ouyang.model.Category; +import ouyang.model.Food; + +/** + * Created by zuxiang on 2015/2/12. + */ +public class FoodCpActivity extends Activity implements LoaderManager.LoaderCallbacks { + ListView mListView; + List mFoods; + FoodCursorAdapter mAdapter; + + private long mId; + public static final int FOODCP_RESULT = 0 << 1; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_activeandroid); + getActionBar().setDisplayHomeAsUpEnabled(true); + + + mListView = (ListView) findViewById(R.id.listview); + mId = getIntent().getLongExtra("id", 0L); + mAdapter = new FoodCursorAdapter(this); + mListView.setAdapter(mAdapter); + getLoaderManager().initLoader(0, null, this); + } + + @Override + public Loader onCreateLoader(int id, Bundle args) { + return new CursorLoader(this, + ContentProvider.createUri(Food.class, null), + null, "cid=?", new String[]{String.valueOf(mId)}, null + ); + } + + @Override + public void onLoadFinished(Loader loader, Cursor data) { + mAdapter.swapCursor(data); + } + + @Override + public void onLoaderReset(Loader loader) { + mAdapter.swapCursor(null); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/activeandroid/MainActivity.java b/active-android-demo/app/src/main/java/ouyang/activeandroid/MainActivity.java new file mode 100644 index 0000000..f1fa000 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/activeandroid/MainActivity.java @@ -0,0 +1,130 @@ +package ouyang.activeandroid; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; + +import com.activeandroid.ActiveAndroid; +import com.activeandroid.query.Delete; + +import java.util.List; + +import ouyang.model.Category; +import ouyang.model.CategoryResult; +import ouyang.model.Food; +import ouyang.model.FoodResult; + + +public class MainActivity extends Activity{ + Button mBtnActive; + Button mBtnActiveContentProvider; + + private static final int ACTIVE = 1<<0; + private static final int CONTENTPROVIDER = 1<<1; + private Handler mHandler = new Handler() { + @Override + public void handleMessage(Message msg) { + super.handleMessage(msg); + if(msg.what==ACTIVE) { + startActivity(new Intent(MainActivity.this, FoodCategoryActivity.class)); + } + if(msg.what==CONTENTPROVIDER){ + startActivity(new Intent(MainActivity.this, FoodCategoryCpActivity.class)); + } + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + + mBtnActive = (Button) findViewById(R.id.btn_activesimple); + mBtnActive.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new Thread(new Runnable() { + @Override + public void run() { + initData(); + mHandler.sendEmptyMessage(ACTIVE); + } + }).start(); + } + }); + + mBtnActiveContentProvider= (Button)findViewById(R.id.btn_activecontentprovdier); + mBtnActiveContentProvider.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + new Thread(new Runnable() { + @Override + public void run() { + initData(); + mHandler.sendEmptyMessage(CONTENTPROVIDER); + } + }).start(); + } + }); + + } + + private void initData(){ + try { + new Delete().from(Category.class).execute(); + new Delete().from(Food.class).execute(); + ActiveAndroid.beginTransaction(); + List categoryList = CategoryResult.getCategorys(CategoryResult.CATEGORY_JSON); + for (Category category : categoryList) { + category.save(); + } + List foodList1 = FoodResult.getFoods(FoodResult.F1_JSON); + for (Food food : foodList1) { + food.save(); + } + + List foodList2 = FoodResult.getFoods(FoodResult.F2_JSON); + for (Food food : foodList2) { + food.save(); + } + List foodList3 = FoodResult.getFoods(FoodResult.F3_JSON); + for (Food food : foodList3) { + food.save(); + } + ActiveAndroid.setTransactionSuccessful(); + } finally { + ActiveAndroid.endTransaction(); + } + } + + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } + +} diff --git a/active-android-demo/app/src/main/java/ouyang/activeandroid/UpdateFoodActivity.java b/active-android-demo/app/src/main/java/ouyang/activeandroid/UpdateFoodActivity.java new file mode 100644 index 0000000..2ab5f40 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/activeandroid/UpdateFoodActivity.java @@ -0,0 +1,58 @@ +package ouyang.activeandroid; + +import android.app.Activity; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; + +import com.activeandroid.query.Update; + +import ouyang.model.Food; + +/** + * Created by zuxiang on 2015/2/11. + */ +public class UpdateFoodActivity extends Activity { + EditText mEtName; + EditText mEtPrice; + Button mBtnUpdate; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_updatefood); + getActionBar().setDisplayHomeAsUpEnabled(true); + mEtName = (EditText) findViewById(R.id.et_name); + mEtPrice = (EditText) findViewById(R.id.et_price); + mBtnUpdate = (Button) findViewById(R.id.btn_update); + + final Food food = (Food) getIntent().getSerializableExtra("food"); + mEtName.setText(food.name); + mEtPrice.setText(String.valueOf(food.price)); + mBtnUpdate.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String name = mEtName.getText().toString(); + String price = mEtPrice.getText().toString(); + if (name.equals("") || price.equals("")) { + return; + } + new Update(Food.class).set("Name=?,Price=?", name, price).where("pk_id=?",food.pk_id).execute(); + setResult(FoodActivity.FOOD_RESULT); + finish(); + } + }); + + } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + finish(); + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/adapter/FoodAdapter.java b/active-android-demo/app/src/main/java/ouyang/adapter/FoodAdapter.java new file mode 100644 index 0000000..1aede7f --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/adapter/FoodAdapter.java @@ -0,0 +1,47 @@ +package ouyang.adapter; + +import android.content.Context; +import android.view.View; +import android.widget.TextView; + +import com.activeandroid.query.Delete; +import com.activeandroid.query.Select; + +import java.util.List; + +import ouyang.activeandroid.R; +import ouyang.model.Food; + +/** + * Created by zuxiang on 2015/2/8. + */ +public class FoodAdapter extends SimpleBaseAdapter { + private long cid; + + public FoodAdapter(Context context, List data, long cid) { + super(context, data); + this.cid = cid; + } + + @Override + public int getItemResource() { + return R.layout.item_food; + } + + @Override + public View getItemView(int position, View convertView, ViewHolder holder) { + Food food = data.get(position); + TextView tvName = holder.getView(R.id.tv_name); + TextView tvPrice = holder.getView(R.id.tv_price); + tvName.setText(food.name); + tvPrice.setText("¥" + food.price); + return convertView; + } + + public void delete(int id) { + new Delete().from(Food.class).where("pk_id = ? ", id).execute(); + data = new Select().from(Food.class).where("cid=?", cid).execute(); + notifyDataSetChanged(); + } + +} diff --git a/active-android-demo/app/src/main/java/ouyang/adapter/FoodCategoryAdapter.java b/active-android-demo/app/src/main/java/ouyang/adapter/FoodCategoryAdapter.java new file mode 100644 index 0000000..5bec149 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/adapter/FoodCategoryAdapter.java @@ -0,0 +1,37 @@ +package ouyang.adapter; + +import android.content.Context; +import android.view.View; +import android.widget.TextView; + +import java.util.List; + +import ouyang.activeandroid.R; +import ouyang.model.Category; + +/** + * Created by zuxiang on 2015/2/8. + */ +public class FoodCategoryAdapter extends SimpleBaseAdapter { + + public FoodCategoryAdapter(Context context, List data) { + super(context, data); + } + + @Override + public int getItemResource() { + return R.layout.item_category; + } + + @Override + public long getItemId(int position) { + return data.get(position).id; + } + + @Override + public View getItemView(int position, View convertView, ViewHolder holder) { + TextView tvName= holder.getView(R.id.tv_name); + tvName.setText(data.get(position).name); + return convertView; + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/adapter/FoodCategoryCursorAdapter.java b/active-android-demo/app/src/main/java/ouyang/adapter/FoodCategoryCursorAdapter.java new file mode 100644 index 0000000..277449b --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/adapter/FoodCategoryCursorAdapter.java @@ -0,0 +1,50 @@ +package ouyang.adapter; + +import android.content.Context; +import android.database.Cursor; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; + +import ouyang.activeandroid.R; +import ouyang.model.Category; + +/** + * Created by zuxiang on 2015/2/11. + */ +public class FoodCategoryCursorAdapter extends CursorAdapter { + private Cursor mCursor; + + public FoodCategoryCursorAdapter(Context context) { + super(context, null, false); + } + + + @Override + public Object getItem(int position) { + mCursor.moveToPosition(position); + return Category.getCategory(mCursor); + } + + @Override + public long getItemId(int position) { + mCursor.moveToPosition(position); + return Category.getCategory(mCursor).id; + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + mCursor = cursor; + LayoutInflater layoutInflater = LayoutInflater.from(context); + return layoutInflater.inflate(R.layout.item_category,null); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + Category category = Category.getCategory(cursor); + TextView tvName = (TextView)view.findViewById(R.id.tv_name); + tvName.setText(category.name); + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/adapter/FoodCursorAdapter.java b/active-android-demo/app/src/main/java/ouyang/adapter/FoodCursorAdapter.java new file mode 100644 index 0000000..21fd114 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/adapter/FoodCursorAdapter.java @@ -0,0 +1,46 @@ +package ouyang.adapter; + +import android.content.Context; +import android.database.Cursor; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; + +import ouyang.activeandroid.R; +import ouyang.model.Category; +import ouyang.model.Food; + +/** + * Created by zuxiang on 2015/2/12. + */ +public class FoodCursorAdapter extends CursorAdapter { + private Cursor mCursor; + + public FoodCursorAdapter(Context context) { + super(context, null, false); + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + mCursor = cursor; + LayoutInflater layoutInflater = LayoutInflater.from(context); + return layoutInflater.inflate(R.layout.item_cpfood, null); + } + + @Override + public Object getItem(int position) { + mCursor.moveToPosition(position); + return Food.getFood(mCursor); + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + Food food = Food.getFood(cursor); + TextView tvName = (TextView) view.findViewById(R.id.tv_name); + TextView tvPrice = (TextView) view.findViewById(R.id.tv_price); + tvName.setText(food.name); + tvPrice.setText("¥" + food.price); + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/adapter/SimpleBaseAdapter.java b/active-android-demo/app/src/main/java/ouyang/adapter/SimpleBaseAdapter.java new file mode 100644 index 0000000..65cff49 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/adapter/SimpleBaseAdapter.java @@ -0,0 +1,113 @@ +package ouyang.adapter; + +import android.content.Context; +import android.util.SparseArray; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by zuxiang on 2015/2/8. + */ +public abstract class SimpleBaseAdapter extends BaseAdapter { + + protected Context context; + protected List data; + + public SimpleBaseAdapter(Context context, List data) { + this.context = context; + this.data = data == null ? new ArrayList() : new ArrayList(data); + } + + @Override + public int getCount() { + return data.size(); + } + + @Override + public Object getItem(int position) { + if (position >= data.size()) + return null; + return data.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + /** + * 该方法需要子类实现,需要返回item布局的resource id + * + * @return + */ + public abstract int getItemResource(); + + /** + * 使用该getItemView方法替换原来的getView方法,需要子类实现 + * + * @param position + * @param convertView + * @param parent + * @param holder + * @return + */ + public abstract View getItemView(int position, View convertView, ViewHolder holder); + + @SuppressWarnings("unchecked") + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder holder; + if (null == convertView) { + convertView = View.inflate(context, getItemResource(), null); + holder = new ViewHolder(convertView); + convertView.setTag(holder); + } else { + holder = (ViewHolder) convertView.getTag(); + } + return getItemView(position, convertView, holder); + } + + public class ViewHolder { + private SparseArray views = new SparseArray(); + private View convertView; + + public ViewHolder(View convertView) { + this.convertView = convertView; + } + + @SuppressWarnings("unchecked") + public T getView(int resId) { + View v = views.get(resId); + if (null == v) { + v = convertView.findViewById(resId); + views.put(resId, v); + } + return (T) v; + } + } + + public void addAll(List elem) { + data.addAll(elem); + notifyDataSetChanged(); + } + + public void remove(T elem) { + data.remove(elem); + notifyDataSetChanged(); + } + + public void remove(int index) { + data.remove(index); + notifyDataSetChanged(); + } + + public void replaceAll(List elem) { + data.clear(); + data.addAll(elem); + notifyDataSetChanged(); + } +} \ No newline at end of file diff --git a/active-android-demo/app/src/main/java/ouyang/model/Category.java b/active-android-demo/app/src/main/java/ouyang/model/Category.java new file mode 100644 index 0000000..7330317 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/model/Category.java @@ -0,0 +1,36 @@ +package ouyang.model; + +import android.database.Cursor; +import android.provider.BaseColumns; + +import com.activeandroid.Model; +import com.activeandroid.annotation.Column; +import com.activeandroid.annotation.Table; + +/** + * Created by zuxiang on 2015/2/8. + */ +@Table(name = "categorys") +public class Category extends Model { + @Column(name = "Name") + public String name; + @Column(name="cid") + public int id; + + @Column(name=BaseColumns._ID) + public int ID; + public Category(String name) { + this.name = name; + } + + public Category(){ + + } + + public static Category getCategory(Cursor cursor){ + Category category = new Category(); + category.name = cursor.getString(cursor.getColumnIndex("Name")); + category.id = cursor.getInt(cursor.getColumnIndex("cid")); + return category; + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/model/CategoryResult.java b/active-android-demo/app/src/main/java/ouyang/model/CategoryResult.java new file mode 100644 index 0000000..54fd99e --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/model/CategoryResult.java @@ -0,0 +1,19 @@ +package ouyang.model; + +import com.google.gson.Gson; + +import java.util.List; + +/** + * Created by zuxiang on 2015/2/9. + */ +public class CategoryResult { + public static final String CATEGORY_JSON = "{\"rows\": [ {\"id\":1, \"name\": \"IT一族\"},{\"id\":2,\"name\": \"高富帅\" },{ \"id\":3,\"name\": \"白富美\" }]}"; + public List rows; + + public static List getCategorys(String json) { + Gson gson = new Gson(); + CategoryResult categoryResult = gson.fromJson(json, CategoryResult.class); + return categoryResult.rows; + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/model/Food.java b/active-android-demo/app/src/main/java/ouyang/model/Food.java new file mode 100644 index 0000000..1853291 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/model/Food.java @@ -0,0 +1,46 @@ +package ouyang.model; + +import android.database.Cursor; +import android.provider.BaseColumns; + +import com.activeandroid.Model; +import com.activeandroid.annotation.Column; +import com.activeandroid.annotation.Table; + +import java.io.Serializable; + +/** + * Created by zuxiang on 2015/2/8. + */ +@Table(name = "foods") +public class Food extends Model implements Serializable { + @Column(name = "Name") + public String name; + @Column(name = "Price") + public float price; + @Column(name = BaseColumns._ID) + public int ID; + @Column(name = "pk_id") + public int pk_id; + + @Column(name = "cid") + public int cid; + + public Food(String foodName, float price) { + this.name = foodName; + this.price = price; + } + + public Food() { + + } + + public static Food getFood(Cursor cursor) { + Food food = new Food(); + food.cid = cursor.getInt(cursor.getColumnIndex("cid")); + food.name = cursor.getString(cursor.getColumnIndex("Name")); + food.price = cursor.getFloat(cursor.getColumnIndex("Price")); + food.pk_id = cursor.getInt(cursor.getColumnIndex("pk_id")); + return food; + } +} diff --git a/active-android-demo/app/src/main/java/ouyang/model/FoodResult.java b/active-android-demo/app/src/main/java/ouyang/model/FoodResult.java new file mode 100644 index 0000000..2745b53 --- /dev/null +++ b/active-android-demo/app/src/main/java/ouyang/model/FoodResult.java @@ -0,0 +1,21 @@ +package ouyang.model; + +import com.google.gson.Gson; + +import java.util.List; + +/** + * Created by zuxiang on 2015/2/9. + */ +public class FoodResult { + public static final String F1_JSON = "{\"rows\": [{\"pk_id\":1, \"cid\": 1,\"name\": \"海西青虾\",\"price\": 150.12},{\"pk_id\":2, \"cid\": 1,\"name\": \"海鲜套餐\",\"price\": 102.12}, {\"pk_id\":3,\"cid\": 1,\"name\": \"叉烧鸭肉套餐\",\"price\": 110.12},{\"pk_id\":4,\"cid\": 1,\"name\": \"鲜虾一盘\",\"price\": 10.12},{\"pk_id\":5,\"cid\": 1,\"name\": \"白切鸡套餐\",\"price\": 10.12},{\"pk_id\":6,\"cid\": 1,\"name\": \"红烧酱鸭\", \"price\": 160.12}, {\"pk_id\":7,\"cid\": 1,\"name\": \"叉烧鸭肉套餐\", \"price\": 110.12},{\"pk_id\":8,\"cid\": 1,\"name\": \"珍珠贝套餐\",\"price\": 107.12}]}"; + public static final String F2_JSON ="{\"rows\": [{\"cid\":2,\t\"pk_id\":11,\"name\": \"青椒肉丝\", \"price\": 150.12},{\"cid\":2,\"pk_id\":12,\"name\": \"荔枝肉\",\"price\": 102.12}, {\"cid\":2,\"pk_id\":13,\"name\": \"排骨套餐\",\"price\": 110.12},{\"cid\":2,\t\"pk_id\":14, \"name\": \"鸡排套餐\", \"price\": 10.12}, {\"cid\":2,\"pk_id\":15, \"name\": \"鸭腿套餐\",\"price\": 10.12 }, {\"cid\":2,\"pk_id\":16,\"name\": \"鸡翅套餐\",\"price\": 160.12} ]}"; + public static final String F3_JSON = "{\"rows\": [{\"cid\": 3,\"pk_id\":20,\"name\": \"兔肉套餐\", \"price\": 130.12},{\"cid\": 3,\"pk_id\":21,\"name\": \"带鱼套餐\", \"price\": 150.12 }, {\"cid\": 3,\"pk_id\":22,\"name\": \"筒骨套餐\",\"price\": 102.12 }, { \"cid\": 3,\"pk_id\":23,\"name\": \"叉烧鸡套餐\",\"price\": 110.12},{ \"name\": \"白切鸡套餐\",\"price\": 10.12}]}"; + private List rows; + + public static List getFoods(String json) { + Gson gson = new Gson(); + FoodResult foodResult = gson.fromJson(json, FoodResult.class); + return foodResult.rows; + } +} diff --git a/active-android-demo/app/src/main/res/drawable-hdpi/ic_launcher.png b/active-android-demo/app/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..96a442e Binary files /dev/null and b/active-android-demo/app/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/active-android-demo/app/src/main/res/drawable-mdpi/ic_launcher.png b/active-android-demo/app/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..359047d Binary files /dev/null and b/active-android-demo/app/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/active-android-demo/app/src/main/res/drawable-xhdpi/ic_launcher.png b/active-android-demo/app/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..71c6d76 Binary files /dev/null and b/active-android-demo/app/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/active-android-demo/app/src/main/res/drawable-xxhdpi/ic_launcher.png b/active-android-demo/app/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..4df1894 Binary files /dev/null and b/active-android-demo/app/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/active-android-demo/app/src/main/res/layout/activity_activeandroid.xml b/active-android-demo/app/src/main/res/layout/activity_activeandroid.xml new file mode 100644 index 0000000..3706fb0 --- /dev/null +++ b/active-android-demo/app/src/main/res/layout/activity_activeandroid.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/active-android-demo/app/src/main/res/layout/activity_food.xml b/active-android-demo/app/src/main/res/layout/activity_food.xml new file mode 100644 index 0000000..3fd2396 --- /dev/null +++ b/active-android-demo/app/src/main/res/layout/activity_food.xml @@ -0,0 +1,9 @@ + + + + \ No newline at end of file diff --git a/active-android-demo/app/src/main/res/layout/activity_main.xml b/active-android-demo/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..abada51 --- /dev/null +++ b/active-android-demo/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,23 @@ + + +