2012-01-12 54 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); 
}