2013-08-08 2 views
5

내가는 Cursor는 등 을 String, Int 얻기위한 방법을 가지고 있지만 내가 객체를 반환하는 동적 방법을 만들려고하고 난 단지 그것을 던져 Android 커서? 객체 데이터를 읽는 방법?

mycursor.GetObject(index) 

거기 일도하지 않는 것을 알고있다.

또는 어떤 유형의 경우에도 mycursor.GetString(index)을 사용할 수 있습니까? String,Float,Double,Long,Short, Blob,Int

예를 들어 Float 또는 Int 또는 모든 유형을 사용하여 캐스트 할 수 있습니까?

(Blob) newblob=mycursor.GetString(i_return_BloB); 
(Int) newint=mycursor.GetString(i_return_Int); 
(Float) newfloat=mycursor.GetString(i_return_Float); 
(Double) newdouble=mycursor.GetString(i_return_Double); 
(Long) newlong=mycursor.GetString(i_return_long); 
(Short) newshort=mycursor.GetString(i_return_short); 

작동 할 예

에 대한

? 어떤 유형에도 mycursor.GetString()을 사용할 수 있습니까?

답변

4

get the type of a column을 입력 한 다음 switch 문에서 적합한 방법을 호출하고 직접 요청하는 메서드를 빌드하십시오.

결과와 열 값이 널 또는 열 타입 인 경우,이 메소드가 예외를 발생 여부 아닌 문자열 타입 구현은 : 워드 프로세서 지정 상이한 타입

사용 getString은 안전하지 않다 -한정된.

+0

android.database.Cursor에는 swich 또는 if를 사용하여 만든 아이디어 인 getType()이 없지만 GetType (columnindex)이 없습니다. – angel

+0

apilevel> = 11에서 사용할 수 있습니다. 적합한 메소드가없는 것 같습니다. 이전 Androids (http://stackoverflow.com/questions/11658239/cursor-gettype-for-api-level-11 – dst

+0

참조) 해결 방법 : http://stackoverflow.com/a/6145969/2636001 – dst

2

당신은 같은 것을 시도하려는 :

DataBaseHelper myDbHelper = new DataBaseHelper(this); 

ArrayList<String> mArrayList = new ArrayList<String>(); 

String[] array; 

Cursor c = myDbHelper.query(query); 

int id = c.getColumnIndex("COLUMN_NAME"); 

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
    mArrayList.add(c.getString(id)); 
} 

array = mArrayList.toArray(new String[0]); 


System.out.println(array[0]); 

c.close(); 

myDbHelper.close(); 

을 그리고처럼 query() 보일 것입니다 무엇인가 : 당신은 당신의 요구에 맞게 SQL 코드를 변경해야

public Cursor query(String query){ 

    String selectAll = null; 

    if(query.length() > 1){ 

    selectAll = "SELECT TABLENAME.COLUMNAME FROM TABLENAME WHERE TABLENAME.COLUMNNAME LIKE" + query + ";"; 

    return myDataBase.rawQuery(selectAll, null); 
} 

. 이 경우 쿼리와 일치하는 열의 모든 값을 검색하지만 SELECT 문을 수정할 수 있습니다. 희망이 도움이됩니다.