我使用外部源代码导入导出我的SQlite数据库。我有我的ListAdapter和SQliteHelper类。我不明白我应该在错误中替换什么。 在这里,我已经展示了我在工作室中找到错误或红字的地方,然后我附加了我的ListAdapter和SQLiteHelper类。
DbAdapter dbAdapter = new DbAdapter(ctx);
dbAdapter.open();
这里DbAdapter和开放的单词在android工作室中显示为红色
while(cursor.moveToNext()){
dbAdapter.createQuote(
cursor.getString(titleColumn),
cursor.getString(timestampColumn)
);
}
sqlDb.close();
cursor.close();
dbAdapter.close();
这里createQuote和close以红色显示
这是我从外部源复制的主要代码 我的DbExportImport类
/** Imports the file at IMPORT_FILE **/
protected static boolean importIntoDb(Context ctx){
if( ! SdIsPresent() ) return false;
File importFile = IMPORT_FILE;
if( ! checkDbIsValid(importFile) ) return false;
try{
SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
(importFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
null, null, null, null, null, null, null
);
DbAdapter dbAdapter = new DbAdapter(ctx);
dbAdapter.open();
final int titleColumn = cursor.getColumnIndexOrThrow("title");
final int timestampColumn = cursor.getColumnIndexOrThrow("timestamp");
// Adds all items in cursor to current database
cursor.moveToPosition(-1);
while(cursor.moveToNext()){
dbAdapter.createQuote(
cursor.getString(titleColumn),
cursor.getString(timestampColumn)
);
}
sqlDb.close();
cursor.close();
dbAdapter.close();
} catch( Exception e ){
e.printStackTrace();
return false;
}
return true;
}
/** Given an SQLite database file, this checks if the file
* is a valid SQLite database and that it contains all the
* columns represented by DbAdapter.ALL_COLUMN_KEYS **/
protected static boolean checkDbIsValid( File db ){
try{
SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase
(db.getPath(), null, SQLiteDatabase.OPEN_READONLY);
Cursor cursor = sqlDb.query(true, DATABASE_TABLE,
null, null, null, null, null, null, null
);
// ALL_COLUMN_KEYS should be an array of keys of essential columns.
// Throws exception if any column is missing
for( String s : DbAdapter.ALL_COLUMN_KEYS ){
cursor.getColumnIndexOrThrow(s);
}
sqlDb.close();
cursor.close();
} catch( IllegalArgumentException e ) {
Log.d(TAG, "Database valid but not the right type");
e.printStackTrace();
return false;
} catch( SQLiteException e ) {
Log.d(TAG, "Database file is invalid.");
e.printStackTrace();
return false;
} catch( Exception e){
Log.d(TAG, "checkDbIsValid encountered an exception");
e.printStackTrace();
return false;
}
return true;
}
MY ListAdapter Class
public class ListAdapter extends BaseAdapter {
Context context;
ArrayList<String> ID;
ArrayList<String> Name;
ArrayList<String> PhoneNumber;
ArrayList<String> Date;
public ListAdapter(
Context context2,
ArrayList<String> id,
ArrayList<String> name,
ArrayList<String> phone,
ArrayList<String> date
)
{
this.context = context2;
this.ID = id;
this.Name = name;
this.PhoneNumber = phone;
this.Date = date;
}
public int getCount() {
// TODO Auto-generated method stub
return ID.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View child, ViewGroup parent) {
Holder holder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.items, null);
holder = new Holder();
holder.ID_TextView = (TextView) child.findViewById(R.id.textViewID);
holder.Name_TextView = (TextView) child.findViewById(R.id.textViewNAME);
holder.PhoneNumberTextView = (TextView) child.findViewById(R.id.textViewPHONE_NUMBER);
holder.Date_TextView = (TextView) child.findViewById(R.id.textViewDATE);
child.setTag(holder);
} else {
holder = (Holder) child.getTag();
}
holder.ID_TextView.setText(ID.get(position));
holder.Name_TextView.setText(Name.get(position));
holder.PhoneNumberTextView.setText(PhoneNumber.get(position));
holder.Date_TextView.setText(Date.get(position));
return child;
}
public class Holder {
TextView ID_TextView;
TextView Name_TextView;
TextView PhoneNumberTextView;
TextView Date_TextView;
}
}
我的SQLiteHelper类
public class SQLiteHelper extends SQLiteOpenHelper {
public static String DATABASE_NAME="AndroidJSonDataBase";
public static final String TABLE_NAME="AndroidJSonTable";
public static final String Table_Column_ID="id";
public static final String Table_Column_1_Name="name";
public static final String Table_Column_2_PhoneNumber="phone_number"; //info table
public static final String Table_Column_3_Date="date";
public static final String Table_Column_4_ImageUri="image_uri";
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
String CREATE_TABLE="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("
+Table_Column_ID+" INTEGER PRIMARY KEY, "
+Table_Column_1_Name+" VARCHAR, "
+Table_Column_2_PhoneNumber +" VARCHAR, "
+Table_Column_3_Date+" VARCHAR, "
+Table_Column_4_ImageUri+" VARCHAR)";
database.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
答案 0 :(得分:0)
我认为答案是,既不使用,但也许适应DbAdapter。
看起来DbAdapter不是用于显示目的的适配器,而是从导入的数据库中调整/转换/提取数据以适合本地使用。
ListAdapter是一个适用于显示目的的适配器,不适用于转换
简而言之,方法importIntoDb
: -
checkDbIsValid
SQLiteDatabase sqlDb = SQLiteDatabase.openDatabase(db.getPath(), null, SQLiteDatabase.OPEN_READONLY);
- 将文件作为SQLite数据库打开,如果该文件不是SQLite数据库,则SQLite可以理解该异常被捕获并且该方法返回false 。这是好的/适合的,因为它是通用的。Cursor cursor = sqlDb.query(true, DATABASE_TABLE, null, null, null, null, null, null, null);
- 根据DATABASE_TABLE从表中提取包含所有列和所有行(重复项除外)的Cursor,无论是什么。这要求DATABASE_TABLE(无论何处设置)设置为导入数据库中存在的表。for( String s : DbAdapter.ALL_COLUMN_KEYS ){
cursor.getColumnIndexOrThrow(s);
}
- 检查光标中的列是否全部位于String数组中,该数组是 DbAdapter
<的类变量/ strong>上课。 这需要DbAdapter类。所以这需要省略或调整。true
或false
后者,表示数据库无效。title
和timestamp
。 DbAdapter
和createQuote
列中提取的数据,使用title
的{{1}}方法循环光标。 因此,只有在源中存在所述列并且存在DbAdapter类的实例时,这才会起作用。 ...
要调整DbAdapter,您可能需要继续查看已复制的代码,并确定它是否适合您的目的。