2017-05-03 26 views
-1

現在我嘗試從數據庫[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) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java :1145) at android.widget.AbsListView $ PerformClick.run(AbsListView.java:3066) at android.widget.AbsListView $ 3.run(AbsListView.java:3903) at android.os.Handler.handleCallback(Handler。 java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread。 main(ActivityThread.java:5451) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android .internal.os.ZygoteInit.main(ZygoteInit.java:616)

在SearchActivity在userDbHelper

@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"; 
} 

}

after search if press on listview that's error

我怎樣才能解決這個問題?

編輯:更新userDbHelper

+0

如何創建新的數據庫語句? 請給我一個CREATE語句 – redAllocator

+0

好的,我更新了userDbHelper。 – MyNaMeArT

回答

0
String user_name = cursor.getString(cursor.getColumnIndex("USER_NAME")); 

您正在使用的列名作爲「USER_NAME」,而你已經將它定義爲

 public static final String USER_NAME = "user_name"; 

所以修正用於列名的情況。

+0

它的平均我會固定到 字符串USER_NAME = cursor.getString(cursor.getColumnIndex( 「USER_NAME」));對 ? – MyNaMeArT

+0

ty 4意見,但它仍然錯誤:) – MyNaMeArT

0

我沒有看到你所有的整個代碼,但很明顯,導致崩潰的路線是:

cursor.getString(cursor.getColumnIndex("USER_NAME")); 

我認爲cursor是你SearchActivity的成員。我不知道爲什麼你認爲它會一定指向正確的行(因爲我看到你在其他地方更新它,並且它被用於迭代整個DB)。我覺得你有2種選擇:

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(); 

    }