滑动删除+数据库增删改查(WEB DAO+Service+Entity+事务写法)

This commit is contained in:
zengfantian 2015-03-04 11:44:18 +08:00
parent 999f03054a
commit 1dde258fed
12 changed files with 518 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 351 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/fav_go_nomal" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/fav_go_pressed" />
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/fav_go_nomal" />
<item android:state_pressed="true" android:drawable="@drawable/fav_go_pressed" />
</selector>

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 没有焦点时的背景颜色 -->
<item android:drawable="@drawable/list_n" android:state_window_focused="false"/>
<!-- 非触摸模式下获得焦点并单击时的背景颜色 -->
<item android:drawable="@drawable/list_p" android:state_focused="true" android:state_pressed="true"/>
<!-- 触摸模式下单击时的背景颜色 -->
<item android:drawable="@drawable/list_p" android:state_focused="false" android:state_pressed="true"/>
<!-- 选中时的背景颜色 -->
<item android:drawable="@drawable/list_p" android:state_selected="true"/>
<!-- 获得焦点时的背景 颜色 -->
<item android:drawable="@drawable/list_p" android:state_focused="true"/>
</selector>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android_custom="http://schemas.android.com/apk/res/com.zftlive.android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/aliceblue"
android:orientation="vertical" >
<com.zftlive.android.view.SwipeListView
android:id="@+id/fav_listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="@android:color/transparent"
android:divider="@color/lightgray"
android:dividerHeight="1.5dp"
android:focusable="true"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true"
android:scrollbars="none"
android_custom:right_width="100dp" />
</LinearLayout>

View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/my_favorite_list_selector"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/item_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/tv_publish_date"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="2dp"
android:text="@string/list_publish_date"
android:textColor="@color/whiteGray"
android:textSize="@dimen/fav_list_item_date" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:layout_toLeftOf="@+id/btn_go"
android:layout_toRightOf="@+id/tv_publish_date"
android:gravity="left|center_vertical"
android:orientation="vertical"
android:padding="2dp" >
<TextView
android:id="@+id/tv_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/list_category"
android:textColor="@color/grey"
android:textSize="@dimen/fav_list_item_category" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:singleLine="false"
android:lines="2"
android:text="@string/list_title"
android:textColor="@color/black"
android:textSize="@dimen/fav_list_item_title" />
</LinearLayout>
<ImageView
android:id="@+id/btn_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="5dp"
android:layout_centerVertical="true"
android:src="@drawable/my_favorite_list_go_selector"
android:contentDescription="@string/app_name" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/item_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/right_bg"
android:gravity="center" >
<TextView
android:id="@+id/item_right_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/del_icon_normal"
android:gravity="center"
android:text="@string/menu_delete"
android:textColor="@color/blur_bg"
android:textSize="@dimen/fav_list_item_delete"
/>
</RelativeLayout>
</LinearLayout>

View File

@ -0,0 +1,11 @@
package com.zftlive.android.sample;
public interface IContant {
/**数据库名称**/
public static final String DB_NAME = "zftlive";
/**数据库版本号**/
public static final int DB_VERSION = 1;
}

View File

@ -0,0 +1,53 @@
package com.zftlive.android.sample.db;
import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import com.zftlive.android.R;
import com.zftlive.android.base.BaseActivity;
import com.zftlive.android.view.SwipeListView;
public class DBOpreationActivity extends BaseActivity {
private SwipeListView mListView;
private MyFavDataAdapter mAdapter;
@Override
public int bindLayout() {
return R.layout.activity_db_fav_list;
}
@Override
public void initView(View view) {
mListView = (SwipeListView) findViewById(R.id.fav_listview);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// //没有显示删除时进行跳转
// if(!mListView.isShowRight()){
// Intent intent = new Intent(getActivity(),EssayDetailActivity.class);
// intent.putExtra("url", ((Favorite)mAdapter.getItem(position)).getLinkURL());
// getActivity().startActivity(intent);
// }
}
});
}
@Override
public void doBusiness(Context mContext) {
}
@Override
public void resume() {
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}

View File

@ -0,0 +1,84 @@
package com.zftlive.android.sample.db;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
import com.zftlive.android.base.BaseEntity;
/**
* [我的收藏]实体
* @author 曾繁添
*/
@DatabaseTable(tableName="Favorite")
public class Favorite extends BaseEntity {
/**
* 主键ID
*/
@DatabaseField(id=true)
private String id;
/**
* 发布日期yyyy-MM-dd
*/
@DatabaseField
private String publishDate;
/**
* 频道类别
*/
@DatabaseField
private String category;
/**
* 文章标题
*/
@DatabaseField
private String title;
/**
* 链接URL
*/
@DatabaseField
private String linkURL ;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLinkURL() {
return linkURL;
}
public void setLinkURL(String linkURL) {
this.linkURL = linkURL;
}
}

View File

@ -0,0 +1,92 @@
package com.zftlive.android.sample.db;
import java.sql.Savepoint;
import java.util.List;
import android.content.Context;
import android.content.ServiceConnection;
import android.util.Log;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.DatabaseConnection;
import com.zftlive.android.sample.IContant;
import com.zftlive.android.tools.db.DatabaseHelper;
/**
* 信息搜藏业务控制
*
* @author 曾繁添
* @version 1.0
*
*/
public class FavoriteManager {
private DatabaseHelper dbHelper;
private String TAG = "FavoriteManager";
/**
* 初始化收藏业务控制
* @param mContext
*/
public FavoriteManager(Context mContext) {
dbHelper = DatabaseHelper.gainInstance(mContext, IContant.DB_NAME, IContant.DB_VERSION);
}
public int insert(Favorite model) {
//返回受影响行数
int rows = 0;
String savePointName = "FavoriteInsert";
Savepoint savePoint = null;
DatabaseConnection connection = null;
try {
//获取数据库连接
connection = dbHelper.getConnection();
connection.setAutoCommit(false);
savePoint = connection.setSavePoint(savePointName);
//插入数据
Dao<Favorite, String> dao = dbHelper.getDao(Favorite.class);
rows = dao.create(model);
//提交事务
connection.commit(savePoint);
} catch (Exception e) {
//回滚事务
dbHelper.rollback(connection,savePoint);
Log.e(TAG, "插入数据失败,原因:"+e.getMessage());
}finally{
//释放数据库连接
dbHelper.closeConnection(connection);
}
return rows;
}
public void insertBatch() {
}
public void delete(Favorite model) {
}
public void update(Favorite model) {
}
public Favorite findById(String strPK) {
return null;
}
public List<Favorite> findByCondtion() {
return null;
}
public List<Favorite> findAllPagenation(final long offset, final long limit){
return null;
}
}

View File

@ -0,0 +1,144 @@
package com.zftlive.android.sample.db;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import com.zftlive.android.R;
import com.zftlive.android.base.BaseAdapter;
/**
* 我的收藏列表适配器
* @author 曾繁添
* @version 1.0
*
*/
public class MyFavDataAdapter extends BaseAdapter {
private final static String TAG = "MyFavDataAdapter";
private int mRightWidth = 0;
public MyFavDataAdapter(Activity activity,int rightWidth) {
super(activity);
mRightWidth = rightWidth;
}
@SuppressLint("ResourceAsColor")
@Override
public View getView(final int position, View itemView, ViewGroup parent) {
// 查找控件
Holder holder = null;
if (null == itemView) {
itemView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_db_fav_list_item, null);
holder = new Holder();
//左右两侧
holder.item_left = (RelativeLayout)itemView.findViewById(R.id.item_left);
holder.item_right = (RelativeLayout)itemView.findViewById(R.id.item_right);
holder.tv_publish_date = (TextView) itemView.findViewById(R.id.tv_publish_date);
holder.tv_category = (TextView) itemView.findViewById(R.id.tv_category);
holder.tv_title = (TextView) itemView.findViewById(R.id.tv_title);
holder.btn_go = (ImageView) itemView.findViewById(R.id.btn_go);
//删除按钮
holder.item_right_txt = (TextView)itemView.findViewById(R.id.item_right_txt);
itemView.setTag(holder);
} else {
holder = (Holder) itemView.getTag();
}
LinearLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
holder.item_left.setLayoutParams(lp1);
LinearLayout.LayoutParams lp2 = new LayoutParams(mRightWidth, LayoutParams.MATCH_PARENT);
holder.item_right.setLayoutParams(lp2);
// 装填数据
Favorite data = (Favorite) getItem(position);
try {
//发布日期
String favDate = data.getPublishDate();
if(favDate.length() > 10){
favDate = favDate.substring(0, 10);
}
holder.tv_publish_date.setText(favDate);
holder.tv_category.setText(data.getCategory());
holder.tv_title.setText(data.getTitle());
holder.btn_go.setTag(position);
holder.btn_go.setOnClickListener(new MyFavClickListener(data.getLinkURL()));
} catch (Exception e) {
Log.e(TAG, e.getMessage().toString());
}
//滑动删除按钮点击监听事件
holder.item_right.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onRightItemClick(v,position);
}
}
});
return itemView;
}
/**
* 单击事件监听器
*/
private onRightItemClickListener mListener = null;
public void setOnRightItemClickListener(onRightItemClickListener listener){
mListener = listener;
}
/***控件保持Holder**/
public class Holder {
RelativeLayout item_left;
RelativeLayout item_right;
TextView tv_publish_date;
TextView tv_category;
TextView tv_title;
ImageView btn_go;
TextView item_right_txt;
}
/**
* 我的收藏列表点击事件监听器
*
*/
public class MyFavClickListener implements View.OnClickListener{
private String linkURL = "" ;
public MyFavClickListener(String linkURL) {
super();
this.linkURL = linkURL;
}
@Override
public void onClick(View v) {
// Intent intent = new Intent(getActivity(),EssayDetailActivity.class);
// intent.putExtra("url", linkURL);
// getActivity().startActivity(intent);
}
}
public interface onRightItemClickListener {
void onRightItemClick(View v, int position);
}
}