这是myclass eyehelper,它从文本文件中预先添加数据库
public class EyeHelper extends SQLiteOpenHelper {
private static final String TAG = "EyeDatabase";
//private static final String DB_DIR = "/data/data/test.eyedb/databases/";
private static final String DB_NAME="eyeDB.sqlite";
//private static String DB_PATH = DB_DIR + DB_NAME;
private static final int SCHEMA_VERSION=1;
private final Context myContext = null;
private SQLiteDatabase mDatabase;
public EyeHelper(Context context) {
super(context, DB_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE DiseaseTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, Disease TEXT NOT NULL, Definition TEXT NOT NULL, Remedy TEXT NOT NULL);");
db.execSQL("INSERT INTO DiseaseTable VALUES(1,'Nilsen', 'Johan', 'Bakken 2');");
loadDB();
}
private void loadDB() {
new Thread(new Runnable() {
public void run() {
try {
loadWords();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWords() throws IOException {
Log.d(TAG, "Loading words...");
final Resources resources = myContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.dbtest);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 3) continue;
long id = addWord(strings[0].trim(), strings[1].trim(), strings[2].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
/**
* Add a word to the dictionary.
* @return rowId or -1 if failed
*/
public long addWord(String disease, String definition, String remedy) {
ContentValues cv = new ContentValues();
cv.put("disease", disease);
cv.put("definition", definition);
cv.put("remedy", remedy);
return mDatabase.insert("DiseaseTable", null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS DiseaseTable");
onCreate(db);
}
public Cursor getAll() {
return(getReadableDatabase()
.rawQuery("SELECT * FROM DiseaseTable",
null));
}
public void insert(String disease, String definition,
String remedy) {
ContentValues cv=new ContentValues();
cv.put("disease", disease);
cv.put("definition", definition);
cv.put("remedy", remedy);
getWritableDatabase().insert("Disease", "disease", cv);
}
public String getDisease(Cursor c) {
return(c.getString(1));
}
public String getDefinition(Cursor c) {
return(c.getString(2));
}
public String getRemedy(Cursor c) {
return(c.getString(3));
}
}
这是我的主要活动
public class EyeDBActivity extends TabActivity {
Cursor model=null;
EyeAdapter adapter=null;
TextView disease;
TextView definition;
TextView remedy;
EyeHelper helper=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new EyeHelper(this);
disease=(TextView)findViewById(R.id.disease);
definition=(TextView)findViewById(R.id.definition);
remedy=(TextView)findViewById(R.id.remedy);
//sButton save=(Button)findViewById(R.id.save);
//save.setOnClickListener(onSave);
ListView list=(ListView)findViewById(R.id.list);
model=helper.getAll();
startManagingCursor(model);
adapter=new EyeAdapter(model);
list.setAdapter(adapter);
TabHost.TabSpec spec=getTabHost().newTabSpec("tag1");
spec.setContent(R.id.list);
spec.setIndicator("Search", getResources()
.getDrawable(R.drawable.list));
getTabHost().addTab(spec);
spec=getTabHost().newTabSpec("tag2");
spec.setContent(R.id.details);
spec.setIndicator("Information", getResources()
.getDrawable(R.drawable.restaurant));
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
list.setOnItemClickListener(onListClick);
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
/*private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
type="sit_down";
break;
case R.id.take_out:
type="take_out";
break;
case R.id.delivery:
type="delivery";
break;
}
helper.insert(name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
model.requery();
}
};*/
private AdapterView.OnItemClickListener onListClick=new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position,
long id) {
model.moveToPosition(position);
disease.setText(helper.getDisease(model));
definition.setText(helper.getDefinition(model));
remedy.setText(helper.getRemedy(model));
getTabHost().setCurrentTab(1);
}
};
class EyeAdapter extends CursorAdapter {
EyeAdapter(Cursor c) {
super(EyeDBActivity.this, c);
}
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
EyeHolder holder=(EyeHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
EyeHolder holder=new EyeHolder(row);
row.setTag(holder);
return(row);
}
}
static class EyeHolder {
private TextView disease;
EyeHolder(View row) {
disease=(TextView)row.findViewById(R.id.text);
}
void populateFrom(Cursor c, EyeHelper helper) {
disease.setText(helper.getDisease(c));
}
}
}
我不知道为什么我的程序在创建时不显示我的数据库, 即使我把适配器放到listview
答案 0 :(得分:0)
你这样做有点不同我会这么说我很难说什么不对。看起来你的setText调用不正确。您不应该将settext方法放在持有者中。你的绑定应该没问题。我认为您不需要将光标调用它,因为它应该根据适配器中设置的光标自动执行此操作。如果你还没有看到这个链接,它应该有所帮助。 http://www.vogella.de/articles/AndroidListView/article.html#adapterperformance_standard
这对构建自定义适配器有很大帮助。