2012-09-05 2 views
0

데이터베이스에서 해당 정보를 가져 오는 스피너에서 문자열을 가져 오려고합니다. 사용시안드로이드 : MySQL 데이터베이스 커서로 인해 IllegalStateException이 발생 함 (CursorWindow에서 행 *, 열 *을 읽지 못했습니다.)

spinner.getSelectedItem().toString(); 

나는 "[email protected]"과 같은 문자열을 얻습니다. 해결 된 문자열을 얻으려면 커서를 사용하여 데이터베이스를 쿼리하려고했지만 LogCat의 메시지에 "3 행 2 열을 가진 CursorWindow에서 행 1, 열 2를 읽지 못했습니다."라는 메시지가 나타나는 동안 IllegalStatException이 발생합니다.

행과 열의 숫자는 정확한 셀이어야하며 조회 할 값이 들어 있습니다.

저는 여기에 사용되는 코드는 다음과 같습니다

int selection = (int) spinner.getSelectedItemId(); //I get the selected item id. 
     Cursor selectioncursor = db.getAllSubjects(); //This is a query getting all the contents from the table 
     selectioncursor.moveToPosition(selection); //The id I got earlier equals the number of the row, the data is stored in, so I move the cursor to the correct row. 
     String subject = selectioncursor.getString(2); //Now the cursor should get the string from column 2 which is the one containing all the values (first column is "_id" of course) 

당신의 도움에 대한 감사합니다.

+0

나는 당신에게 한 가지 제안을 할 수 있습니다. 2 열의 값을 원하면 selectioncursor.getString (1);을 쿼리해야합니다. selectioncursor.getString (2)가 아닙니다. 커서 인덱스가 0.부터 시작하기 때문에 logcat을 게시하십시오. –

+0

@SharathG 이미 내 문제를 해결했습니다. 고마워요 :) – BlueHazard

+0

아, 그게 이미 자신이 해결 한 것을 듣기 좋다. 건배 –

답변

0
static void llenaMiSpinner(Cursor cursor, String dato, Spinner spinner, Context context) { 
     cursorsimpleAdapter = new SimpleCursorAdapter(context, 
       android.R.layout.simple_spinner_item, 
       cursor, 
       new String[]{dato}, 
       new int[]{android.R.id.text1}, 
       SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER 
     ); 
     spinner.setAdapter(cursorsimpleAdapter); 
    } 

// aqui puedes obtener tu informacion :) 
    @Override 
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) { 
     int vista = adapterView.getId(); 
     switch (vista) { 
      case R.id.spinner: 
       c3 = (Cursor) adapterView.getItemAtPosition(position); 
       objetoid = c3.getString(c3.getColumnIndex(recursoBD.Objeto.OBJ_ID)); 
       break; 
      case R.id.spinner2: 
       Cursor c4 = (Cursor) adapterView.getItemAtPosition(position); 
       marcaId = c4.getString(c4.getColumnIndex(recursoBD.Marca.MAR_ID)); 
       break; 

      case R.id.spinner3: 
       //obteiendo ID del Eje que fue Selecionado 
       Cursor c1 = (Cursor) adapterView.getItemAtPosition(position); 
       ejeSelection = c1.getString(
         c1.getColumnIndex(recursoBD.Eje.EJE_ID)); 
       activeTramoSpinner(ejeSelection); 

       break; 
      case R.id.spinner4: 
       Cursor c2 = (Cursor) adapterView.getItemAtPosition(position); 
       tramoSelection = c2.getString(c2.getColumnIndex(recursoBD.Tramo.TRAMO_ID)); 

       break; 
      case R.id.spinner5: 
       Cursor c5 = (Cursor) adapterView.getItemAtPosition(position); 
       id_usuario = c5.getString(c5.getColumnIndex(recursoBD.Usuario.USU_ID)); 
       break; 
     } 
    } 


llenaMiSpinner(cursorDispo, DISPO_NAME, objetos, this); 
+0

자세한 설명 필요 –

관련 문제