我想查看SQLite数据库中存在数据的所有以前的用户 滚动网后很多都无法实现任何目标
我为.setThreshold()
Android / java代码:
dbHandler = new MyDBHandler(this, null, null, 1);
mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
String[] previousUsers = dbHandler.getUseremails();
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, previousUsers);
mEmailView.setThreshold(1);
mEmailView.setAdapter(arrayAdapter1);
xml代码:
<AutoCompleteTextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />
myDBHandler.java:
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "LoginDetails.db";
private Context context;
public static final String TABLE_PRODUCTS = "People";
public static final String COLUME_ID = "_id";
public static final String COLUME1_NAME = "_email";
public static final String COLUME2_NAME = "_password";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUME1_NAME + " TEXT Unique, " +COLUME2_NAME + " TEXT " +
");";
db.execSQL(query);
}
public String[] getUseremails()
{
String returnString[] = new String[20];
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT " + COLUME1_NAME +" FROM " + TABLE_PRODUCTS + ";";
Cursor c = db.rawQuery(query, null);
int i=0;
if(c.moveToFirst())
{
do {
dbString = String.format("%s", c.getString(c.getColumnIndex("_email")));
returnString[i] = dbString;
++i;
dbString = "";
} while (c.moveToNext());
}
c.close();
return returnString;
}
加上奇怪的是,当我使用文本观察器填写密码时,只要自动填充条目与数据库中的密码相匹配,它就会从DB填充正确的密码。暗示DB中存在数据。
我希望单击文本框时列表会下拉。我也尝试使用onClickListener
,但无法得到任何东西。
答案 0 :(得分:0)
我认为,您的问题是,您正在将字符串数组传递给可能包含 null&#39; 的ArrayAdapter
以及ArrayAdpater对此例外。除非你在人员表中有正好20行,否则20个定义的元素中的一些将不会被设置,因此为空。< / p>
如果您查看日志,很可能会发现类似的内容(这实际上并未使应用程序崩溃,因此您可能没有注意到它): -
06-03 11:03:53.910 10422-10532/mjt.testvcsuse W/Filter: An exception occured during performFiltering()!
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter$ArrayFilter.performFiltering(ArrayAdapter.java:480)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
其中一个解决方案是将字符串数组 returnString
设置为具有正确数量的元素,即元素数量与光标中的行数匹配。
这是 getUseremails
方法的修改版本,旧代码留在评论中( PS我太懒了,无法包含格式< / em>的)。问题的真正关键在于使用 String returnString[] = new String[c.getCount()];
来解决,它将数组元素的数量设置为行数(请注意,您应该检查一下如何处理行(或者只是采取行数)我的话,它是hanlded OK)): -
public String[] getUseremails() {
//String returnString[] = new String[20]; //MUST BE 20 rows else nulls
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT " + COLUME1_NAME + " FROM " + TABLE_PRODUCTS + ";";
Cursor c = db.rawQuery(query, null);
String returnString[] = new String[c.getCount()]; // # elements = rows
int i = 0;
while (c.moveToNext()) {
returnString[i++] = c.getString(
c.getColumnIndex(COLUME1_NAME)
);
}
/* OLD
int i = 0;
if (c.moveToFirst()) {
do {
dbString = String.format("%s", c.getString(c.getColumnIndex("_email")));
returnString[i] = dbString;
++i;
dbString = "";
} while (c.moveToNext());
}
*/
c.close();
return returnString;
}