创建游标两次的问题

时间:2012-09-17 16:02:12

标签: android cursor textview indexoutofboundsexception

关于光标的使用真的很困惑。

我有一项活动正常,代码如下:

public class AreaActivity extends ListActivity {

private TextView secondaryTitle;
private Button newArea;
private static final int ACTIVITY_CREATE=0;

private RMDbAdapter rmDbHelper;
private AlertDialog clickOptionsDialog;
private long inspectionId;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_area);
    rmDbHelper = new RMDbAdapter(this);
    rmDbHelper.open();
    Intent i = getIntent();
    inspectionId = i.getLongExtra("Intent_InspectionID", -1);
    setUpViews();
    setLongClick();

    // Get a Cursor for the list items
    Cursor listCursor = rmDbHelper.fetchAllAreasForInspection(inspectionId);
    startManagingCursor(listCursor);           

    // set the custom list adapter     
    setListAdapter(new MyListAdapter(this, listCursor));



}

private void setUpViews() {
    secondaryTitle = (TextView)findViewById(R.id.secondary_title);
    final Cursor cursor = (Cursor) rmDbHelper.fetchInspection(inspectionId);
    String inspectionRef = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
            RMDbAdapter.INSPECTION_REF)), "Reference unknown"); 
    String companyName = RMUtilities.notEmpty(cursor.getString(cursor.getColumnIndex( 
            RMDbAdapter.INSPECTION_COMPANY)), "company unknown"); 
    cursor.close();
    final String secondaryTitleText = inspectionRef + ", " + companyName;
    secondaryTitle.setText(secondaryTitleText);
    newArea = (Button)findViewById(R.id.new_area);
    newArea.setOnClickListener(new View.OnClickListener() {                     
        public void onClick(View v) {
            createArea();
        }
    }); 

}

游标从数据库中获取信息,然后检查它是否为空(使用本网站上推荐给我的单独类中的代码),并在TextView中设置文本。

然而,当我在下一个活动中使用这个确切的代码做同样的事情时,我得到错误:

  

CursorIndexOutOfBoundsException:请求索引0,大小为0 ..

代码是一样的,所以我想这是一个复制光标或不使用moveToFirst的问题,但我没做什么似乎解决了这个问题。

1 个答案:

答案 0 :(得分:0)

感谢njzk2发现问题 - 当我将inspectId传递给Intent时出现了愚蠢的错误..