android中的运行时游标错误

时间:2014-10-01 06:55:21

标签: android cursor

获取游标错误。我是新手到android所以需要一个help.cursor显示请求索引5的CursorIndexOutOfBoundsException的运行时异常,大小为5。 在下面的程序中,我实现了AndroidSlidingUpPanel-Master。光标值显示正常但当我尝试向上滚动滑块然后抛出光标错误。

Context mContext;
  Cursor mCursor;
  boolean movetonext = false;
  private LayoutInflater inflater;

  DownloadedFileListAdapter(Context m, Cursor c) {
      mContext = m;
      mCursor = c;
  }

  @Override
  public int getCount() {
      return mCursor.getCount();
  }

  @Override
  public Object getItem(int position) {
      return mCursor.moveToNext();
  }

  @Override
  public long getItemId(int position) {
      return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      if (inflater == null)
          inflater = (LayoutInflater) mContext
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      if (convertView == null)
          convertView = inflater.inflate(R.layout.display_file_list, null);

      TextView fileName = (TextView) convertView.findViewById(R.id.downloaded_file_name);
      TextView fileCreatedOn = (TextView) convertView.findViewById(R.id.file_path_created_on);

      fileName.setText("" + mCursor.getString(0));
      fileCreatedOn.setText("" + mCursor.getString(2));

      getItem(position);

      return convertView;
  }

P.S:

10-01 12:17:26.268: E/AndroidRuntime(22538): android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
10-01 12:17:26.268: E/AndroidRuntime(22538):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
10-01 12:17:26.268: E/AndroidRuntime(22538):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
10-01 12:17:26.268: E/AndroidRuntime(22538):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
10-01 12:17:26.268: E/AndroidRuntime(22538):    at com.example.socialapp.DownloadedFileListAdapter.getView(DownloadedFileListAdapter.java:56)

1 个答案:

答案 0 :(得分:0)

试试这个

@Override
public long getItemId(int position) {
  return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (inflater == null) {
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }else{
        if (convertView == null){
            convertView = inflater.inflate(R.layout.display_file_list, null);
        }else{

        TextView fileName = (TextView) convertView.findViewById(R.id.downloaded_file_name);
        TextView fileCreatedOn = (TextView) convertView.findViewById(R.id.file_path_created_on);

        fileName.setText("" + mCursor.getString(0));
        fileCreatedOn.setText("" + mCursor.getString(2));

        getItem(position);
        }
    }

    return convertView;
}