0

I am retrieving data from sqlite to base adapter by cursor.

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

}

Then, 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;
}
}

from this code always error --> ArrayOuOfBound if execute this code

title.setText(string1[position]);

how i can solve it?

2
  • 3
    Wait, so do you have a null pointer exception or an ArrayOutOfBounds exception? Commented Jan 12, 2012 at 4:21
  • Did you get any of value from local DB . Mean you get value in v0,v01... ? Commented Jan 12, 2012 at 4:36

4 Answers 4

2

You need to open your database in the activity you retrieve or insert data into your SQLite database. Also make sure you close it when your finished.

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

In your onDestroy or pause. Call

m.close();

This may be the issue, because i dont see where you open your database in the code.

EDIT :

In your DatabaseHelper class. Create a method.

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

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

Then you will have the method to close and open the database in your activities where you need to insert and retrieve information.

Sign up to request clarification or add additional context in comments.

Comments

1

To retrive data from cursor, first you must go to first row data in cursor.

Some code like this:

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

good luck!! :D

Comments

0

It seems you want to load resultset into list, if so please change your code to set adapter outside of loop:

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

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.