前段時(shí)間項(xiàng)目中遇到的問題贮尉,來跟大家來分享一下伴嗡。
開發(fā)環(huán)境:
Android Studio
項(xiàng)目框架:
litepal
最終效果:
result.gif
需要了解的sql語句
1.select name from sqlite_master where type='table' order by name 獲取app中所有表名
2.pragma table_info(" + name + ") 通過表名獲取字段名
服務(wù)端中主要的四個(gè)方法:
1.獲取全部表名:
public List<String> getAllTableName() throws RemoteException {
List<String> result = new ArrayList<>();
Cursor cursor = findBySQL("SELECT name FROM sqlite_master WHERE type='table' order by name");
while (cursor.moveToNext()) {
if (!cursor.getString(cursor.getColumnIndex("name")).trim().equals("android_metadata"))
result.add(cursor.getString(cursor.getColumnIndex("name")));
}
return result;
}
2.通過表名獲取表中所有字段
@Override
public List<String> getFieldByName(String name) throws RemoteException {
List<String> result = new ArrayList<>();
Cursor cursor = findBySQL("pragma table_info(" + name + ")");
while (cursor.moveToNext()) {
result.add(cursor.getString(cursor.getColumnIndex("name")));
}
return result;
}
3.通過表名來獲取數(shù)據(jù)
@Override
public Modern getDataByTableName(String tableName) throws RemoteException {
Modern modern = new Modern();
Map<Integer, List<String>> dataMap = new HashMap<>();
List<String> field = new ArrayList<>();
Cursor cursor = DataSupport.findBySQL("pragma table_info(" + tableName + ")");
while (cursor.moveToNext()) {
field.add(cursor.getString(cursor.getColumnIndex("name")));
}
cursor = DataSupport.findBySQL("select *from " + tableName);
int i = 0;
while (cursor.moveToNext()) {
List<String> data = new ArrayList<>();
for (int itme = 0; itme < field.size(); itme++) {
data.add(cursor.getString(cursor.getColumnIndex(field.get(itme))));
}
dataMap.put(i, data);
Log.d("zw", " i = " + i);
i++;
}
modern.setMap(dataMap);
return modern;
}
4.通過表名更新活插入一條新數(shù)據(jù)
@Override
public void updateData(String tableName, Modern modern) throws RemoteException {
Log.d("zw", "updatedata");
Map<Integer, List<String>> data = modern.getMap();
List<String> field = new ArrayList<>();
Cursor cursor = DataSupport.findBySQL("pragma table_info(" + tableName + ")");
while (cursor.moveToNext()) {
field.add(cursor.getString(cursor.getColumnIndex("name")));//獲取表中所有字段名
}
for (int i = 0; i < data.size(); i++) {
cursor = DataSupport.findBySQL("select * from " + tableName + " where id = " + data.get(i).get(0));
if (cursor.moveToNext()) //通過id來判斷表中是否已經(jīng)存在該條數(shù)據(jù) 如果是 更新 否則插入一條新數(shù)據(jù)
{
ContentValues values = new ContentValues();
for (int s = 0; s < field.size(); s++) {
values.put(field.get(s), data.get(i).get(s));
}
DataSupport.updateAll(tableName, values, field.get(0) + " = ?", data.get(i).get(0) + "");
} else {
Log.d("zw", "insert into ");
SQLiteDatabase sqLiteDatabase = LitePal.getDatabase();//litepal框架獲取database方法
ContentValues value = new ContentValues();
for (int c = 0; c < field.size(); c++) {
value.put(field.get(c), data.get(i).get(c));
}
sqLiteDatabase.insert(tableName, null, value);
}
}
}
5.通過表名和id刪除數(shù)據(jù)
@Override
public void deleteData(String tableName, List<String> id) throws RemoteException {
Log.d("zw", "start deleteData");
for (int i = 0; i < id.size(); i++) {
Log.d("zw", " id " + id.get(i));
DataSupport.deleteAll(tableName, "id = ?", id.get(i));
}
}
客戶端主要方法:
1.獲取表名在spinner上輸出
private void initTableInfo() {
try {
tabNames = mlService.getAllTableName();
if (tabNames != null) {
tabNameAdapter = new StringAdapter(tabNames, getActivity());
spDataTyle.setAdapter(tabNameAdapter);//這里的spDataTyle是一個(gè)Spinner控件
spDataTyle.setSelection(0);
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
2.加載表中數(shù)據(jù)
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (progressDialog != null)
progressDialog.dismiss();
switch (msg.what) {
case SUCESS:
initFiledInfo();
Map<Integer, List<String>> data = modern.getMap();
if (tableAdapter == null) {
tableAdapter = new TableAdapter(getActivity(), data);
} else {
tableAdapter.setDataToView(data);
}
rvData.setAdapter(tableAdapter);
break;
case FAILURE:
showToast("數(shù)據(jù)加載失敗");
break;
case DELETEFAILURE:
break;
case DELETESUCESS:
initTableData();
break;
}
}
};
if (progressDialog == null)
progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle("數(shù)據(jù)加載中......");
progressDialog.show();
new Thread() {
@Override
public void run() {
super.run();
try {
filedNames = mlService.getFieldByName(tableName);
modern = mlService.getDataByTableName(tableName);
handler.sendEmptyMessage(SUCESS);
} catch (RemoteException e) {
e.printStackTrace();
handler.sendEmptyMessage(FAILURE);
}
}
}.start();
3.適配器
public class TableAdapter extends RecyclerView.Adapter<TableAdapter.MyViewHolder> {
private Context context;
private Map<Integer, List<String>> data;
private List<String> pos;
private List<Boolean> checks;
public List<String> getPos() {
return pos;
}
public TableAdapter(Context context, Map<Integer, List<String>> data) {
pos = new ArrayList<>();
checks = new ArrayList<>();
this.context = context;
this.data = data;
for (int i = 0; i < data.size(); i++) {
checks.add(false);
}
}
public void setAllChecked(boolean checked) {
pos.clear();
for (int i = 0; i < checks.size(); i++)
checks.set(i, checked);
if (checked) {
for (int c = 0; c < data.size(); c++) {
pos.add(data.get(c).get(0));
}
}
notifyDataSetChanged();
}
public void addNewDataToView() {
List<String> newData = new ArrayList<>();
for (int i = 0; i < data.get(data.size() - 1).size(); i++) {
if (i == 0) {
String id = (Integer.parseInt(data.get(data.size() - 1).get(0)) + 1) + "";
newData.add(id);
} else {
newData.add("");
}
}
data.put(data.size(), newData);
checks.clear();
for (int i = 0; i < data.size(); i++) {
checks.add(false);
}
notifyDataSetChanged();
}
public void setDataToView(Map<Integer, List<String>> data) {
Log.d("zw", "setDataToView");
this.data.clear();
pos.clear();
this.data.putAll(data);
checks.clear();
for (int i = 0; i < data.size(); i++) {
checks.add(false);
}
notifyDataSetChanged();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.dataitmelayout, parent, false);
return new MyViewHolder(view);
}
public Map<Integer, List<String>> getData() {
return data;//輸出數(shù)據(jù)
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.linearLayout.removeAllViews();
final CheckBox checkBox = new CheckBox(getActivity());
checkBox.setBackgroundResource(R.color.colorheadLine);
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
checkBox.setLayoutParams(checkParams);
checkBox.setChecked(checks.get(position));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checks.set(position, isChecked);
if (isChecked) {
pos.add(data.get(position).get(0));
} else {
pos.remove(data.get(position).get(0));
}
}
});
holder.linearLayout.addView(checkBox);
final List<String> datas = data.get(position);
for (int i = 0; i < datas.size(); i++) {
String value = datas.get(i);
DataItmeView dataItmeView = new DataItmeView(getActivity(), null);
dataItmeView.setContentValue(value);
if (i == 0)
dataItmeView.setContentEdit(false);
dataItmeView.setBackGroudColor(R.color.colorWhite);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
dataItmeView.setLayoutParams(layoutParams);
holder.linearLayout.addView(dataItmeView);
EditText content = dataItmeView.getContentView();
final int finalI = i;
//用來監(jiān)聽表中數(shù)據(jù)的變化達(dá)到修改之后通過更新按鈕更新的效果
content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
data.get(position).set(finalI, s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
/**
* @return
*/
@Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
public MyViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
}
}
}
還有不明白的可以 v樓主 微信號(hào) a21544182123