开发者

Help with the Android Cursor

开发者 https://www.devze.com 2023-02-05 09:32 出处:网络
So I have a Cursor that is getting a result from a query to the application datab开发者_JAVA百科ase. I know that there is at least one correct entry in the database since I was able to retrieve the st

So I have a Cursor that is getting a result from a query to the application datab开发者_JAVA百科ase. I know that there is at least one correct entry in the database since I was able to retrieve the string from the row previously.

I then changed some of the logic to accommodate the result and am suddenly getting null returned when calling c.getString(0). The relevant code is posted below.

I'm new to Android and Java, so I might be missing some subtlety that's causing the problem.

   Cursor c = context.getContentResolver().query(tempJobName.build(), 
           null, null, null, null);

   for (c.moveToFirst(); c.isAfterLast() == false; c.moveToNext())
   {
       Log.w(TAG, c.getString(0));
       if (c.getString(0).equalsIgnoreCase(jobName))
       {
           existed = true;
           break;
       }
    }


You might want to add more details. But since you did find some records, the solution for your problem is easy. Could you please replace this line with this? ( First column of table might change right :))

c.getString(0);

with

int columnIndex = c.getColumnIndex(COLUMN_NAME);// You could also use getcolumnIndexorThrow variant.
c.getString(columnIndex);

if this does not work, then your table does not have the column. You are simply barking up the wrong tree.

0

精彩评论

暂无评论...
验证码 换一张
取 消