现在我尝试从数据库[SQLite]查询数据以通过搜索显示 接下来我想按下在搜索后发现的列表视图,但该错误
android.database.CursorIndexOutOfBoundsException:请求索引2,大小为2 在android.database.AbstractCursor.checkPosition(AbstractCursor.java:460) 在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) at th.kusrc.devbeginner.sqlite4.SearchActivity $ 1.onItemClick(SearchActivity.java:50) 在android.widget.AdapterView.performItemClick(AdapterView.java:310) 在android.widget.AbsListView.performItemClick(AbsListView.java:1145) 在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3066) 在android.widget.AbsListView $ 3.run(AbsListView.java:3903) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
在SearchActivity中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Search_name = (EditText) findViewById(R.id.search_name);
listView = (ListView) findViewById(R.id.listView);
listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(listDataAdapter);
// OnClick Item
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
String user_name = cursor.getString(cursor.getColumnIndex("USER_NAME"));
Toast.makeText(getApplicationContext(),
"Selected User : " + user_name, Toast.LENGTH_SHORT).show();
}
});
searchBtn = (Button) findViewById(R.id.searchContact);
searchBtn.setOnClickListener(this);
}
public void searchContact() {
search_name = Search_name.getText().toString();
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getContact(search_name, sqLiteDatabase);
if(cursor.moveToFirst()) {
do {
String name, mobile, email;
name = cursor.getString(0);
mobile = cursor.getString(1);
email = cursor.getString(2);
DataProvider dataProvider = new DataProvider(name, mobile, email);
listDataAdapter.add(dataProvider);
}
while (cursor.moveToNext());
}
}
public class UserDbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "USERINFO.DB";
private static final int DATABASE_VERSION = 4;
private static final String CREATE_QUERY =
"CREATE TABLE " + UserContact.NewUserInfo.TABLE_NAME + "(" + UserContact.NewUserInfo.USER_NAME + " TEXT," +
UserContact.NewUserInfo.USER_MOB + " TEXT," + UserContact.NewUserInfo.USER_EMAIL + " TEXT" + ");";
public UserDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("Database Operation", "Database created......");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.d("Database Operation", "Table created......");
}
public Cursor getContact(String user_name, SQLiteDatabase db) {
String[] projections = {UserContact.NewUserInfo.USER_NAME, UserContact.NewUserInfo.USER_MOB, UserContact.NewUserInfo.USER_EMAIL};
String selection = UserContact.NewUserInfo.USER_NAME + " LIKE ?";
String[] selection_args = {user_name};
Cursor cursor = db.query(UserContact.NewUserInfo.TABLE_NAME, projections, selection, selection_args, null, null, null);
return cursor;
}
和UserContact.NewUserInfo
public class UserContact {
public static abstract class NewUserInfo {
public static final String USER_NAME = "user_name";
public static final String USER_MOB = "user_mob";
public static final String USER_EMAIL = "user_email";
public static final String TABLE_NAME = "user_info";
}
}
我该如何解决?
编辑:更新userDbHelper
答案 0 :(得分:0)
String user_name = cursor.getString(cursor.getColumnIndex("USER_NAME"));
您正在使用列名称作为&#34; USER_NAME&#34;而你已将其定义为
public static final String USER_NAME = "user_name";
因此请更正用于列名的大小写。
答案 1 :(得分:0)
我没有看到你所有的代码,但很明显导致崩溃的行是:
cursor.getString(cursor.getColumnIndex("USER_NAME"));
我认为cursor
是您SearchActivity
的成员。我不知道为什么你认为它必然指向正确的行(因为我看到你在其他地方更新它,它用于迭代整个数据库)。我认为你有两个选择:
1)创建列表项时,将用户名保存在持有者视图中(请参阅此example)。因此,您可以直接访问单击侦听器上的数据,而无需使用数据库。
2)更新点击监听器,并从每个位置的数据库中获取正确的行。类似的东西:
public void onItemClick(AdapterView<?> myAdapter, View myView, int position, long mylng) {
// You need to write initTheCursorToPointIndex() on your own (I am not sure how you organized your data so I can't implement it)
cursor = initTheCursorToPointIndex(position);
String user_name = cursor.getString(cursor.getColumnIndex("USER_NAME"));
Toast.makeText(getApplicationContext(),
"Selected User : " + user_name, Toast.LENGTH_SHORT).show();
}