Skip to content

Commit

Permalink
修复已知的问题,优化代码。
Browse files Browse the repository at this point in the history
修复已知的问题,优化代码。
  • Loading branch information
LiqiNew committed Jun 3, 2018
1 parent e46c021 commit f63851b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import android.widget.EditText;
import android.widget.TextView;

import com.liqi.Logger;
import com.liqi.myutils.demo.R;

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

/**
Expand All @@ -22,7 +24,7 @@
* Created by LiQi on 2017/12/6.
*/

public class TestDataBaseOperateActivity extends AppCompatActivity implements View.OnClickListener,TestDataBaseOperateP.OnTestDataBaseOperateListener<ArrayList<Map<String, String>>>{
public class TestDataBaseOperateActivity extends AppCompatActivity implements View.OnClickListener, TestDataBaseOperateP.OnTestDataBaseOperateListener<ArrayList<Map<String, String>>> {
private TextView content;
private EditText add_key_one, add_key_two, add_key_three, add_key_four,
query_key_one, query_key_two, query_key_three, query_key_four,
Expand All @@ -32,6 +34,7 @@ public class TestDataBaseOperateActivity extends AppCompatActivity implements Vi

private Button query_key_query;
private TestDataBaseOperateP<ArrayList<Map<String, String>>> mPresenter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -58,7 +61,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
query_key_query.setOnClickListener(this);
findViewById(R.id.update_key_update).setOnClickListener(this);
findViewById(R.id.delete_key_delete).setOnClickListener(this);
mPresenter=new TestDataBaseOperateP<>(this);
mPresenter = new TestDataBaseOperateP<>(this);
}

@Override
Expand Down Expand Up @@ -134,6 +137,14 @@ public void onClick(View v) {
break;
}
query_key_query.setText("查询数据库>>" + (queryCode + 1));

List<Map<String, String>> mapList = mPresenter.allFindByIdList();
if (null != mapList && !mapList.isEmpty()) {
for (Map<String, String> map : mapList
) {
Logger.e("TestDataBase>>>全部查询", "one值:" + map.get(OnDatabaseTableHelperListener.TEST_CONTENT_ONE));
}
}
break;
//更新
case R.id.update_key_update:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -64,6 +65,18 @@ public void orPrecisionFindByIdList(String contentOne, String contentTwo, String

}

/**
* 查询全部-->详细请看findByAll()注释
*/
public List<Map<String, String>> allFindByIdList() {
ArrayList<String> strings = new ArrayList<>();
strings.add(TEST_CONTENT_ONE);
strings.add(TEST_CONTENT_TWO);
strings.add(TEST_CONTENT_THREE);
strings.add(TEST_CONTENT_FOUR);
return DBManagerOperation.BaseDBManagerOperation(mBaseOperateListener.getContext()).findByAll(TEST_NAME, strings);
}

/**
* 查询-->详细请看allFuzzyFindByIdList()注释
*/
Expand Down
23 changes: 18 additions & 5 deletions utils/src/main/java/com/liqi/utils/ActivityUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
public class ActivityUtil {
private static ActivityUtil mActivityUtil;
private final String TAG = "ActivityUtil.";
private final String TAG = getClass().getName();
private Intent mIntent;

private ActivityUtil() {
Expand All @@ -36,6 +36,15 @@ public static ActivityUtil getActivityUtil() {
return mActivityUtil = null == mActivityUtil ? new ActivityUtil() : mActivityUtil;
}

/**
* 获取intent
*
* @return Intent
*/
public Intent getIntent() {
return mIntent;
}

/**
* 关闭所有的acitivity
*
Expand Down Expand Up @@ -173,7 +182,8 @@ public void startActivityWithObjectData(Context context, Class clazz, Map<String
if (value instanceof Serializable) {
mIntent.putExtra(en.getKey(), (Serializable) en.getValue());
} else {
Logger.e(TAG + "startActivityWithObjectData()", "传入的类型不符合规定");
mIntent.putExtra(en.getKey(), "");
Logger.e(TAG + "startActivityWithObjectData()", "传入的类型不符合规定>>>>默认传输值:\"\"");
}

}
Expand Down Expand Up @@ -230,7 +240,8 @@ public void startActivityWithObjectData(Activity activity, Class<?> clazz, Map<S
if (value instanceof Serializable) {
mIntent.putExtra(en.getKey(), (Serializable) en.getValue());
} else {
Logger.e(TAG + "startActivityWithObjectData()", "带请求标识的方法传入的类型不符合规定");
mIntent.putExtra(en.getKey(), "");
Logger.e(TAG + "startActivityWithObjectData()", "带请求标识的方法传入的类型不符合规定>>>>默认传输值:\"\"");
}
}
} else {
Expand Down Expand Up @@ -284,7 +295,8 @@ public void startFragmentWithObjectData(Fragment fragment, Activity activity, Cl
if (value instanceof Serializable) {
mIntent.putExtra(en.getKey(), (Serializable) en.getValue());
} else {
Logger.e(TAG + "startFragmentWithObjectData()", "传入的类型不符合规定");
mIntent.putExtra(en.getKey(), "");
Logger.e(TAG + "startFragmentWithObjectData()", "传入的类型不符合规定>>>>默认传输值:\"\"");
}
}
} else {
Expand Down Expand Up @@ -399,7 +411,8 @@ public void startActivityForBundleData(Activity activity, Class clazz, Serializa
if (value instanceof Long) {
mIntent.putExtra(en.getKey(), (long) en.getValue());
} else {
Logger.e(TAG + "startActivityForBundleData()", "传入的类型不符合规定");
mIntent.putExtra(en.getKey(), "");
Logger.e(TAG + "startActivityForBundleData()", "传入的类型不符合规定>>>>默认传输值:\"\"");
}
}
} else {
Expand Down
27 changes: 15 additions & 12 deletions utils/src/main/java/com/liqi/utils/db/BaseDBManagerOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
public final class BaseDBManagerOperation {
private static BaseDBManagerOperation mBaseDBManagerOperation;
private final String TAG = "BaseDBManagerOperation";
private final String TAG = getClass().getName();
private SQLiteOpenHelper mSQLiteOpenHelper;

private BaseDBManagerOperation() {
Expand Down Expand Up @@ -67,17 +67,20 @@ public synchronized long save(String table, ContentValues values) {
* @return -1写入失败
*/
public synchronized long save(String table, ArrayList<ContentValues> valuesList) {
long saveSign = 1;
SQLiteDatabase db = mSQLiteOpenHelper.getWritableDatabase();
for (ContentValues contentValues : valuesList) {
saveSign = db.insert(table, null, contentValues);

if (saveSign < 0) {
break;
long saveSign = -1;
if (null != valuesList && !valuesList.isEmpty()) {
SQLiteDatabase db = mSQLiteOpenHelper.getWritableDatabase();
for (int i = 0; i < valuesList.size(); i++) {
ContentValues contentValues = valuesList.get(i);
saveSign = db.insert(table, null, contentValues);
if (saveSign < 0) {
Logger.e(TAG, "save批量添加:写入失败>>>>索引:" + i);
}
}

db.close();
} else {
Logger.e(TAG, "save批量添加:增加数据集合无数据");
}
db.close();
return saveSign;
}

Expand Down Expand Up @@ -303,7 +306,7 @@ private Map<String, Object> getWhereSq(Map<String, String> whereKeyValuess, Data
*
* @param table 表名
* @param keyToVlsInt 指定键(键对应的值必须为int or String)
* @param valuesList 变动的key 和value值集合
* @param valuesList 变动的key 和value值集合
*/
public synchronized void updateAndInsertAndDele(String table, String keyToVlsInt,
ArrayList<ContentValues> valuesList) {
Expand Down Expand Up @@ -656,9 +659,9 @@ public ArrayList<Map<String, String>> findByAll(String table,
if (index >= 0) {
String tagVal = cursor.getString(index);
hashTable.put(tagString, tagVal == null ? "" : tagVal);
list.add(hashTable);
}
}
list.add(hashTable);
}
// 关闭游标
cursor.close();
Expand Down
26 changes: 17 additions & 9 deletions utils/src/main/java/com/liqi/utils/spf/BaseSharePreference.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
* @author LiQi
*/
public class BaseSharePreference {
private static final String TAG=BaseSharePreference.class.getName();
private static Reference<BaseSharePreference> mBaseSharePreference;
private SharedPreferences spf;

private static String mSpName="LiQi_Utils";
/**
* @param spName SharePreference文件名
*/
private BaseSharePreference(Context context, String spName) {
spf = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
spf = context.getApplicationContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
mSpName=spName;
}

/**
Expand All @@ -31,8 +33,13 @@ private BaseSharePreference(Context context, String spName) {
* @param spName SharePreference文件名
* @return BaseSharePreference对象
*/
public static BaseSharePreference initBaseSharePreference(Context context, String spName) {
return null == mBaseSharePreference ? (mBaseSharePreference = new SoftReference<>(new BaseSharePreference(context, spName))).get() : mBaseSharePreference.get();
public static synchronized BaseSharePreference initBaseSharePreference(Context context, String spName) {
synchronized (BaseSharePreference.class) {
if (null == mBaseSharePreference || null == mBaseSharePreference.get()) {
mBaseSharePreference = new SoftReference<>(new BaseSharePreference(context, spName));
}
}
return mBaseSharePreference.get();
}

/**
Expand All @@ -54,10 +61,10 @@ public static BaseSharePreference getNewBaseSharePreference(Context context, Str
*/
public static BaseSharePreference getBaseSharePreference(Context context) {
if (null == mBaseSharePreference) {
Logger.e("BaseSharePreference", "BaseSharePreference没有初始化,创建一个独立的BaseSharePreference实例。");
return getNewBaseSharePreference(context, "LiQi_Utils");
Logger.e(TAG, "BaseSharePreference没有初始化,创建一个独立的BaseSharePreference实例。");
return getNewBaseSharePreference(context, mSpName);
} else {
return mBaseSharePreference.get();
return initBaseSharePreference(context,mSpName);
}
}

Expand Down Expand Up @@ -119,11 +126,12 @@ public void putObjectKeyValue(String key, boolean value) {
* boolean 类型的缺省值为false。
* 如果使用BaseSharePreferenceTypeEnum.SP_ALL_MAP类型取出,那么key失效
* </hint>
*@param <T> 继承 Object的泛型
*
* @param <T> 继承 Object的泛型
* @param key 键
* @param typeEnum 数据取出类型.通过BaseSharePreferenceTypeEnum枚举设置
* @see BaseSharePreferenceTypeEnum
* @return T
* @see BaseSharePreferenceTypeEnum
*/

public <T extends Object> T getObjectKeyValue(String key, BaseSharePreferenceTypeEnum typeEnum) {
Expand Down

0 comments on commit f63851b

Please sign in to comment.