2012-01-12 3 views
0

커서를 기준으로 sqlite에서 기본 어댑터로 데이터를 가져옵니다. 그런 다음 main.javaSqlite에서 데이터를 Retriving하는 동안 Null 포인터 예외

SQLiteDatabase db = dbHelper.getReadableDatabase(); 
Cursor c=db.rawQuery("select * from budget",null); 
     while (c.moveToNext()) { 



     String tes0 = Integer.toString(c.getInt(c.getColumnIndex("_id"))); 
     String tes1 = Double.toString(c.getDouble(c.getColumnIndex("material_actual"))); 
     tes2 = c.getString(c.getColumnIndex("start_date")); 
     tes3 = c.getString(c.getColumnIndex("end_date")); 

     String[] v0 = new String[] { tes0 }; 
     String[] v01 = new String[] { tes1 }; 
     String[] v02 = new String[] { tes2 }; 
     String[] v03 = new String[] { tes3 }; 

      Adapter_ListView adapter = new Adapter_ListView(getBaseContext(), 
       v01, v02 , v03, theTotal); //string[] 
      TaskList.setAdapter(adapter); 

} 

, Adapter_listView.java이 코드는 항상 오류에서

public class Adapter_ListView extends BaseAdapter { 
private int count; 
private Context context; 

private String[] string1; 
private String[] string2; 
private String[] string3; 

private String con1; 
private String con2; 
private String con3; 


public Adapter_ListView(Context baseContext, String[] v01, String[] v02, String[] v03, int theTotal) { 
     this.count = theTotal; 
     this.context = baseContext; 
     this.string1 = v01; 
     this.string2 = v02; 
     this.string3 = v03; 
} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return count; 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int position, View contentView, ViewGroup arg2) { 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    contentView = inflater.inflate(R.layout.layout_inflate_list2, null); 

    TextView title = (TextView)contentView.findViewById(R.id.inflate_title); 
    TextView body = (TextView)contentView.findViewById(R.id.inflate_body); 
    TextView sub = (TextView)contentView.findViewById(R.id.inflate_sub); 


    title.setText(string1[position]); 
    body.setText(string2[position]); 
    sub.setText(string3[position]); 


    return contentView; 
} 
} 

-> ArrayOuOfBound 내가 해결할 수있는 방법이 코드

title.setText(string1[position]); 

을 실행할 경우 그것?

+3

기다립니다, 그래서 당신은 널 포인터 예외 또는 ArrayOutOfBounds 예외를해야합니까? – UIAdam

+0

스택 추적을 게시하십시오. –

+0

로컬 DB에서 값을 얻었습니까? v0, v01 ...에서 가치를 얻는다는 뜻인가요? –

답변

1

커서에서 데이터를 검색하려면 먼저 커서의 첫 번째 행 데이터로 이동해야합니다. 이 같은

일부 코드 :

SQLiteDatabase db = dbHelper.getReadableDatabase(); 
Cursor c=db.rawQuery("select * from budget",null); 
if (c!=null) c.moveToFirst(); 
     while (c.moveToNext()) { 
... 
} 

행운을 빕니다! : D

0

을 당신이 그렇게 루프의 외부 어댑터를 설정하는 코드를 변경하시기 바랍니다 경우, 목록에없는 결과를로드 할 것 같다

당신은 검색 활동에 데이터베이스를 열거 나 데이터를 삽입 할 필요가
SQLiteDatabase db = dbHelper.getReadableDatabase(); 
Cursor c=db.rawQuery("select * from budget",null); 
int theTotal=c.getCount(); 
String[] v0 = new String[theTotal]; 
     String[] v01 = new String[theTotal]; 
     String[] v02 = new String[theTotal]; 
     String[] v03 = new String[theTotal]; 

     int i=0; 
     if (c!=null) c.moveToFirst(); 
     while (c.moveToNext()) { 



     String tes0 = Integer.toString(c.getInt(c.getColumnIndex("_id"))); 
     String tes1 = Double.toString(c.getDouble(c.getColumnIndex("material_actual"))); 
     tes2 = c.getString(c.getColumnIndex("start_date")); 
     tes3 = c.getString(c.getColumnIndex("end_date")); 

     v0[i] =tes0 ; 
     v01[i] = tes1 ; 
     v02[i] = tes2 ; 
     v03[i] = tes3 ; 



} 

Adapter_ListView adapter = new Adapter_ListView(getBaseContext(), 
       v01, v02 , v03, theTotal); //string[] 
      TaskList.setAdapter(adapter); 
+0

예 ... greate 답변입니다. 고맙습니다. – vick

2

SQLite database에 또한 작업이 끝나면 닫으십시오.

final DatabaseHelper m = new DatabaseHelper(this); 
m.open(); 

귀하의 onDestroy에서 또는 일시 중지하십시오. 전화

m.close(); 

코드에 데이터베이스를 열어 본 곳이 없기 때문에 문제가 될 수 있습니다.

편집 : 당신의 DatabaseHelper 클래스에서

. 메서드를 만듭니다.

public void open(){ 
db.open(); 
} 

public void close(){ 
db.close() 
} 

그런 다음 정보를 삽입하고 검색해야하는 활동에서 데이터베이스를 닫고 열 수있는 방법이 있습니다.

0

이 시도 :

main.java

SQLiteDatabase db = dbHelper.getReadableDatabase(); 
Cursor c=db.rawQuery("select * from budget",null); 
if(c.getCount()>0) 
{ 
    int i=0; 
    int clength=c.getCount(); 
    String[] v0=new String[clength]; 
    String[] v1=new String[clength]; 
    String[] v2=new String[clength]; 
    String[] v3=new String[clength]; 

    while (c.moveToNext()) 
    {   
     String tes0 = Integer.toString(c.getInt(c.getColumnIndex("_id"))); 
     String tes1 = Double.toString(c.getDouble(c.getColumnIndex("material_actual"))); 
     tes2 = c.getString(c.getColumnIndex("start_date")); 
     tes3 = c.getString(c.getColumnIndex("end_date")); 

     v0[i]=tes0; 
     v01[i]=tes1; 
     v01[i]=tes2; 
     v01[i]=tes3; 
     i++;  
    } 
    Adapter_ListView adapter = new Adapter_ListView(getBaseContext(),v01, v02 , v03, theTotal); //string[] 
    TaskList.setAdapter(adapter); 
}