Majid
Majid

Reputation: 230

Unknown error in txtview.settext in android

I want to set txt of an text view from a query but I get "index -1 requested with size 1 error..Can some one help me?

    Cursor information = db.rawQuery("select id as _id,description from poet where _id="+Peot_ID_for_db,null);
    TextView poet_info=(TextView)findViewById(R.id.Poet_info);
    poet_info.setText(information.getString(information.getColumnIndex("_id")));
//  poet_info.append(information.getString(information.getColumnIndex("description")));

I searched this error but nothing found in google..I hope you can help me.. I have just one row in my table that matches with this query so I think I dint need any loop or sth like that to get all rows

Upvotes: 1

Views: 90

Answers (2)

Pragnani
Pragnani

Reputation: 20155

You haven't move the cursor to the row..It will throw that error for you

Try using this

if(information.moveToFirst())
{
do{


//Write your code here
}
while(information.moveToNext())

}

Upvotes: 4

Knossos
Knossos

Reputation: 16058

Make sure you start off by using a moveTo function.

It will not get a row for you until you do.

Example:

http://developer.android.com/reference/android/database/Cursor.html#moveToNext()

Snippet:

StringBuilder sb = new StringBuilder();

while(information.moveToNext()) {
    sb.append(information.getString(information.getColumnIndex("_id"))+" ");
}

poet_info.setText(sb.toString());

Upvotes: 3

Related Questions