編輯:關於Android編程
android orm映射框架,可像hibernate一樣操作數據庫。 以下代碼是我從網上摘錄下來的,僅供參考.
package com.cng.utils;
import java.sql.SQLException;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.cng.modal.Hello;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
public class DataHelper extends OrmLiteSqliteOpenHelper
{
private static final String DATABASE_NAME = "HelloOrmlite.db";
private static final int DATABASE_VERSION = 1;
private Dao<Hello, Integer> helloDao = null;
public DataHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)
{
try
{
TableUtils.createTable(connectionSource, Hello.class);
} catch (SQLException e)
{
Log.e(DataHelper.class.getName(), "創建數據庫失敗", e);
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
int arg2, int arg3)
{
try
{
TableUtils.dropTable(connectionSource, Hello.class, true);
onCreate(db, connectionSource);
} catch (SQLException e)
{
Log.e(DataHelper.class.getName(), "更新數據庫失敗", e);
e.printStackTrace();
}
}
@Override
public void close()
{
super.close();
helloDao = null;
}
public Dao<Hello, Integer> getHelloDataDao() throws SQLException
{
if (helloDao == null)
{
helloDao = getDao(Hello.class);
}
return helloDao;
}
}
package com.cng;
import java.sql.SQLException;
import java.util.List;
import com.cng.modal.Hello;
import com.cng.utils.DataHelper;
import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;
import com.j256.ormlite.dao.Dao;
import android.os.Bundle;
import android.widget.TextView;
public class OrmliteLoginActivity extends OrmLiteBaseActivity<DataHelper>
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.output);
try
{
Dao<Hello, Integer> helloDao = getHelper().getHelloDataDao();
// 添加數據
for (int i = 0; i < 2; i++)
{
Hello hello = new Hello("Hello" + i);
helloDao.create(hello);
}
tv.setText(tv.getText() + "\n" + "添加數據完成");
// 查詢添加的數據
List<Hello> hellos = helloDao.queryForAll();
for (Hello h : hellos)
{
tv.setText(tv.getText() + "\n" + h.toString());
}
// 刪除數據第一條數據
helloDao.delete(hellos.get(0));
tv.setText(tv.getText() + "\n" + "刪除數據完成");
// 重新查詢數據
hellos = helloDao.queryForAll();
for (Hello h : hellos)
{
tv.setText(tv.getText() + "\n" + h.toString());
}
// 修改數據
Hello h1 = hellos.get(0);
h1.setWord("這是修改過的數據");
tv.setText(tv.getText() + "\n" + "修改數據完成");
helloDao.update(h1);
// 重新查詢數據
hellos = helloDao.queryForAll();
for (Hello h : hellos)
{
tv.setText(tv.getText() + "\n" + h.toString());
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.cng.modal;
import android.R.integer;
import com.j256.ormlite.field.DatabaseField;
public class Hello
{
@DatabaseField(generatedId = true,unique=true)
int id;
@DatabaseField
String word;
//這是必須加的,否則會出錯
public Hello(){}
public int getId()
{
return id;
}
public Hello(String word)
{
super();
this.word = word;
}
public void setId(int id)
{
this.id = id;
}
public String getWord()
{
return word;
}
public void setWord(String word)
{
this.word = word;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("id=").append(id);
sb.append(" ,word=").append(word);
return sb.toString();
}
www.2cto.com
}
就這三個類,datahelper是操作數據庫的類,可新建,更新表,Hello是一個映射到數據庫表的類,具體的看api 文檔的下載地址是http://ormlite.com/releases/(其中的jar包也在這裡下載) OrmliteLoginActivity就是activity類,它沒繼承activity,而是繼承了OrmLiteBaseActivity類。
RecyclerView多個item布局的寫法(頭布局+腳布局)上圖github下載源碼Initial commit第一次提交的代碼,為本文內容以下的為主要代碼,看注釋即
1、下載進入官網(http://opencv.org/)下載OpenCV4Android並解壓。目錄結構如下圖所示。其中,sdk目錄即是我們開發opencv所需要的類庫;
Android 5.0之後Android新增加的兩個UI控件RecyclerView,CardView。RecyclerView可以看出是ListView的加強版,能夠更