- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这方面遇到了真正的麻烦,我想知道我能得到一些帮助。我有以下代码。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.AsyncTask;
import android.util.Log;
/**
* Simple notes database access helper class. Defines the basic CRUD operations
* for the notepad example, and gives the ability to list all notes as well as
* retrieve or modify a specific note.
*
* This has been improved from the first version of this tutorial through the
* addition of better error handling and also using returning a Cursor instead
* of using a collection of inner classes (which is less scalable and not
* recommended).
*/
public class recoverAnimalDBHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.recover.foundanimal/databases/";
public static final String KEY_BIRDGROUP = "birdgroup";
public static final String KEY_BIRD_LATINNAME = "birdlatinname";
public static final String KEY_BIRD_COMMONNAME = "birdcommonname";
public static final String KEY_BIRD_REGIONFOUND = "birdregionfound";
public static final String KEY_BIRD_SUBREGION = "birdsubregion";
public static final String KEY_ROWID = "_id";
private static final String TAG = "RECOVERAnimalDBHelper";
private static SQLiteDatabase mDb;
private static final String DATABASE_NAME = "recoveranimals.sqlite";
private static final String DATABASE_BIRD_SPECIES_TABLE = "birds";
private static int DATABASE_VERSION = 1;
/**
* Database creation sql statement
*/
private static final String CREATE_BIRD_SPECIES_TABLE =
"create table "+DATABASE_BIRD_SPECIES_TABLE+" ("+KEY_ROWID+" integer primary key autoincrement, "
+ KEY_BIRDGROUP+" text not null, "
+ KEY_BIRD_LATINNAME+" text not null, "
+KEY_BIRD_COMMONNAME+" text not null, "
+KEY_BIRD_REGIONFOUND+" text not null, "
+KEY_BIRD_SUBREGION +" text not null); ";
protected static Context mCtx = null;
public recoverAnimalDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mCtx = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// }
// if (!db.isReadOnly())
// {
// // Enable foreign key constraints
// db.execSQL("PRAGMA foreign_keys=ON;");
// }
// db.execSQL(CREATE_BIRD_SPECIES_TABLE);
// fillDatabase(db);
}
public void createDatabase() {
boolean dbExist = checkDataBase();
if(!dbExist) {
this.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}else {
this.getWritableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
mDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private static boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void fillDatabase(SQLiteDatabase db) {
db.beginTransaction();
Resources currResource = mCtx.getResources();
Field[] values = R.array.class.getDeclaredFields();
String[] birds = currResource.getStringArray(R.array.birdfamilies);
HashMap<String, String[]> birdFamToKey = new HashMap<String, String[]>(birds.length);
for(String bird : birds) {
String[] brdtyps = bird.split("b1234b");
birdFamToKey.put(brdtyps[brdtyps.length-1], brdtyps);
}
String[] specBird = null;
String birdGroup = null;
for(Field v : values) {
if(birdFamToKey.containsKey(v.getName())) {
try {
specBird = currResource.getStringArray(v.getInt(null));
birdGroup = birdFamToKey.get(v.getName())[0];
} catch (NotFoundException e) {
Log.w(TAG,""+v.getName()+" was not found in keyset with error: "+e.getLocalizedMessage());
continue;
} catch (IllegalArgumentException e) {
Log.w(TAG,""+v.getName()+" was not found in keyset with error: "+e.getLocalizedMessage());
continue;
} catch (IllegalAccessException e) {
Log.w(TAG,""+v.getName()+" was not found in keyset with error: "+e.getLocalizedMessage());
continue;
}
}
if(specBird != null && birdGroup != null) {
for(String brdspc : specBird) {
if(brdspc.contains("Family"))
continue;
String[] brdspcarr = brdspc.split("b1234b");
ContentValues initialValues = new ContentValues();
try {
initialValues.put(KEY_BIRDGROUP,birdGroup);
initialValues.put(KEY_BIRD_LATINNAME, brdspcarr[1]);
initialValues.put(KEY_BIRD_COMMONNAME, brdspcarr[0]);
if(brdspcarr.length>2) {
initialValues.put(KEY_BIRD_REGIONFOUND, brdspcarr[2]);
if(brdspcarr.length >3)
initialValues.put(KEY_BIRD_SUBREGION , brdspcarr[3]);
else
initialValues.put(KEY_BIRD_SUBREGION , "");
}else {
initialValues.put(KEY_BIRD_REGIONFOUND, "");
initialValues.put(KEY_BIRD_SUBREGION , "");
}
} catch (Exception e) {
Log.w(TAG, e.getLocalizedMessage()+"\nCAUSE: "+e.getMessage());
e.printStackTrace();
}
db.insertOrThrow(DATABASE_BIRD_SPECIES_TABLE, null, initialValues);
}
}
specBird = null;
}
db.endTransaction();
}
@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 "+DATABASE_BIRD_SPECIES_TABLE);
// onCreate(db);
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private static void copyDataBase() throws IOException{
//Open your local db as the input stream
AssetManager assetManager = mCtx.getAssets();
InputStream myInput = assetManager.open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
@Override
public synchronized void close() {
if(mDb != null)
mDb.close();
super.close();
}
public void resetTables() {
this.onUpgrade(mDb, 1, 1);
}
/**
* Return a Cursor over the list of all birds in the database
*
* @return Cursor over all notes
*/
public Cursor fetchAllAnimals() {
Cursor mCursor = mDb.query(DATABASE_BIRD_SPECIES_TABLE, new String[] {KEY_ROWID,KEY_BIRDGROUP,KEY_BIRD_LATINNAME,
KEY_BIRD_COMMONNAME, KEY_BIRD_REGIONFOUND,KEY_BIRD_SUBREGION}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Returns all unique macroclasses of animals
*
* @return Cursor to all bird types
*/
public Cursor fetchAllAnimalTypes() {
Cursor mCursor = mDb.query(true, DATABASE_BIRD_SPECIES_TABLE, new String[] {KEY_ROWID,KEY_BIRDGROUP}, null,null,null,null,KEY_BIRDGROUP+" ASC",null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/** Returns all animals species that mach an animal type
*
* @return Cursor pointing to all animal species matching the bird type selected
*/
public Cursor fetchAllAnimalSpecies(String animalType) {
Cursor mCursor = mDb.query(DATABASE_BIRD_SPECIES_TABLE,new String[] {KEY_ROWID,KEY_BIRD_COMMONNAME},KEY_BIRDGROUP+" = ?",new String[] {animalType},null,null,KEY_BIRD_COMMONNAME+" DESC");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/**
* Return a Cursor positioned at the animal that matches the given rowId
*
* @param rowId id of animal to retrieve
* @return Cursor positioned to matching animal, if found
* @throws SQLException if note could not be found/retrieved
*/
public Cursor fetchAnimal(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_BIRD_SPECIES_TABLE, new String[] {KEY_ROWID,KEY_BIRDGROUP,KEY_BIRD_LATINNAME,
KEY_BIRD_COMMONNAME, KEY_BIRD_REGIONFOUND,KEY_BIRD_SUBREGION}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean isOpen() {
if( mDb != null && mDb.isOpen())
return true;
return false;
}
public Cursor fetchAnimalTypePartialMatching(String partialmatch) {
Cursor mCursor = mDb.query(true, DATABASE_BIRD_SPECIES_TABLE, new String[] {KEY_BIRDGROUP,KEY_ROWID}, KEY_BIRDGROUP+" LIKE ?",
new String[] {partialmatch},null,null,KEY_BIRDGROUP+" DESC",null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAnimalSpecificPartialMatching(String animalType, String partialmatch) {
Cursor mCursor = mDb.query(DATABASE_BIRD_SPECIES_TABLE, new String[]{KEY_BIRD_COMMONNAME,KEY_ROWID},
KEY_BIRDGROUP+" = ? AND "+KEY_BIRD_COMMONNAME+" LIKE ?",new String[] {animalType,partialmatch},null,
null,KEY_BIRD_COMMONNAME+" DESC");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAnimalSpeciesParitalMatching(String partialmatch) {
Cursor mCursor = mDb.query(DATABASE_BIRD_SPECIES_TABLE, new String[]{KEY_BIRD_COMMONNAME,KEY_ROWID},
KEY_BIRD_COMMONNAME+" LIKE ?",new String[] {partialmatch},null,
null,KEY_BIRD_COMMONNAME+" DESC");
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor query(String[] m_projection, String selct, String[] args,
String m_sortOrder) {
Cursor mCursor = mDb.query(this.DATABASE_BIRD_SPECIES_TABLE, m_projection, selct, args, null, null, m_sortOrder);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
使用它的代码是:
AutoCompleteTextView actvMainType = (AutoCompleteTextView) findViewById(R.id.animalMainTypeTB);
actvMainType.setMaxLines(1);
SimpleCursorAdapter scaSpecTyp = new SimpleCursorAdapter(cntxt,R.layout.simpleautocompletelayout,mCursorMainType,
new String[] {recoverAnimalDBHelper.KEY_BIRDGROUP}, new int[] {R.id.autocompleteTxt});
scaSpecTyp.setStringConversionColumn(
mCursorMainType.getColumnIndexOrThrow(recoverAnimalDBHelper.KEY_BIRDGROUP));
scaSpecTyp.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
String partialItemName = null;
if (constraint != null) {
partialItemName = constraint.toString();
}
return rdbHelper.fetchAnimalTypePartialMatching(partialItemName);
}
});
actvMainType.setAdapter(scaSpecTyp);
actvMainType.setThreshold(1);
我正在尝试让这个数据库正常工作,但我似乎无法让自动完成功能正常工作。有什么建议吗?
非常感谢!乔恩
最佳答案
使用这个网站:http://www.outofwhatbox.com/blog/2010/11/android-simpler-autocompletetextview-with-simplecursoradapter/我能够非常简单地解决它。我的代码基本上很草率,我没有正确使用 LIKE 术语。谢谢大家!
关于android - Autocompletetextview 和 SimpleCursorAdapter 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5414273/
当用户点击 ACTV 并找到他正在搜索的内容时,他再次选择 ACTV 的唯一原因是搜索其他内容。我正在寻找一种方法,当用户在 ACTV 中进行搜索并且搜索文本位于 ACTV 字段中(建议菜单已关闭)时
当我单击 AutoCompleteTextView 的建议时,我希望光标移动到下一个可用的 EditBox 或 AutoCompleteTextView。 我在下面附上了我的布局和我的应用图像文件。在
我有一个 AppCompatAutoCompleteTextView,其 anchor 比 AppCompatAutoCompleteTextView 更靠下。这是设计使然,无法更改。当我在下拉菜单上
我正在使用 3 个 AutocompleteTextViews 来建议来自数据库的条目。我对 AutocompleteTextView 进行了子类化处理,以便在单击时将默认文本设置为 null,并在移
我正在使用 AutoCompleteTextView 创建下拉菜单。如何删除列表顶部和底部的默认边距? 重新创建: List dropdownItems
我正在尝试让 TextWatcher 与 AutoCompleteTextView 一起工作,但遇到了一些问题。我面临的问题是,当我开始输入文本时,如果数据输入的长度只有一个字符,我在 AutoCom
即使我没有在下拉列表项布局中设置边距,下拉列表也带有左右边距。我希望它与 autocompletetextview 的宽度相匹配。我怎样才能做到这一点? 这是我的 AutoCompleteTextVi
我的应用实现了一个 HashMap,这样当在 AutoCompleteTextView editText 中键入 key 时,并且用户单击下拉项,该值将显示在textView上。 唯一的问题是,单击的
这里是 Android 菜鸟,之前是 iOS、.Net、C 和 Fortran。 如果我有一个带有 1000 多个字符串的 ArrayAdapter,我如何修改 AutoCompleteTextVie
嗨,我刚刚开始学习 android Studio,我想要一个 AutoCompletetextView 和一个按钮,如果用户输入未在数组列表中列出,则添加用户输入。我的 AutoCompleteTex
我正在开发一个使用 AutoCompleteTextView 的应用程序,但遇到了一些问题。请在下面查找问题的详细信息。 数据中存在以下值: 1)曼尼什·洛根·杰恩 2)M.J.(洛根·费恩) 3)洛
我正在尝试根据另一个 View (微调器)中选择的选项来更改 AutoCompleteTextView 的建议列表。不幸的是,这个 TextView 的适配器似乎没有从另一个 View 的 setOn
我有这个布局: 使用这种风格: @color/text_input_layout_outlined_box_stroke @color/green_2 我试图在自动
我正在尝试使用 AutoCompleteTextView 进行过滤,但过滤器出现问题。它返回 ArrayList 中的所有项目,而不是过滤后的项目。下面是我的代码: Filter nameFilte
我想制作一个 AutoCompleteTextview ,它将从内部存储加载以前保存的建议。我成功地将字符串从内部存储加载到字符串数组(我使用日志记录来检查......)。 然后,当我将字符串数组加载
我有一个 AutoCompleteTextView,我想在它上面设置一个过滤器,我遇到的问题是当我去输入一些东西时它只会让我在文本字段中输入 1 个字符。如果我从代码中删除 textView.setF
我有两个自动完成 TextView 例如,如果任何人选择自动完成 TextView “HUB ID”位置#3,则另一个自动完成 TextView “HUBName”必须强制位于位置 3即希望它们都处于
您好 Android 开发人员, 我有一个关于自动完成 TextView 的问题。我在平板电脑上开发,我很好地定制了它。当我在平板电脑上运行它时,它看起来像这样: 这实际上是我想要的 - 所有可见的东
我正在处理一项要求,其中使用自动完成 TextView 来获取匹配的Localities。用户必须输入几个字符,然后从服务器返回与这些字符匹配的 Localities 列表。自动完成 TextView
使用 Bootstrap 的 Web 应用程序,有这样的输入区域。 在 Android 中有没有像这样实现 EditText 的库? 最佳答案 是的,完成的泡泡在Android中被称为“筹码”。工作解
我是一名优秀的程序员,十分优秀!