2012-01-12 59 views
0

我正在通過遊標從sqlite檢索數據到基礎適配器。從Sqlite恢復數據時的空指針異常

main.java

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 2012-01-12 04:21:09

+0

發佈堆棧跟蹤。 – 2012-01-12 04:36:13

+0

你從本地數據庫獲得任何價值嗎?意思是你在v0,v01 ...中獲得價值? – 2012-01-12 04:36:45

回答

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

是的...這是很大的答案.. thanx很多傢伙... – vick 2012-01-13 04:08:36

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