我无法删除sqlite中的日期

I can not delete a date in sqlite

我在排除日期时遇到问题,只有当它不是我的 ArrayList 中第一个元素的最后一个元素时才会出现这种情况。

Why does this happen and how to solve?
Example:
| ID |
  :
 Gil <- problem deleting this line
 Rui <- problem deleting this line
 Ana <- no problem

I'm implementing the example of this site: http://www.tutorialspoint.com/android/android_sqlite_database.htm

error: Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

代码

public Integer deleteContact (Integer id, ArrayList<Integer> array_List)
{
    SQLiteDatabase db = this.getWritableDatabase();

    db.delete("contacts", "id = ? ", new String[]{Integer.toString(id)});

    db.close();
    return  id;
}


public ArrayList<String> getAllCotacts()
{
    ArrayList<String> array_list = new ArrayList<String>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from contacts", null );
    res.moveToFirst();

    while(res.isAfterLast() == false){
        array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
        res.moveToNext();
    }
    return array_list;
}

public Cursor getData(int id){
  SQLiteDatabase db = this.getReadableDatabase();
  Cursor res =  db.rawQuery( "select * from contacts where id="+id+"", null       );
  return res;
   }


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_contact);
    name = (TextView) findViewById(R.id.editTextName);
    phone = (TextView) findViewById(R.id.editTextPhone);
    email = (TextView) findViewById(R.id.editTextStreet);
    street = (TextView) findViewById(R.id.editTextEmail);
    place = (TextView) findViewById(R.id.editTextCity);

    mydb = new DBHelper(this);

    Bundle extras = getIntent().getExtras();
    if(extras !=null)
    {
        int Value = extras.getInt("id");

        if(Value>0){
            //means this is the view part not the add contact part.
            Cursor rs = mydb.getData(Value);
            id_To_Update = Value;
                if ( rs.moveToFirst()) {
                    do {
                        if (rs != null  && rs.getCount()!= 0 ){
                            id_To_Update = Value;
String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));
String phon=rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_PHONE));
String emai=rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_EMAIL));
String Stree=rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_STREET));
String  plac = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_CITY));

                            Button b = (Button) findViewById(R.id.button1);
                            b.setVisibility(View.INVISIBLE);

                            name.setText((CharSequence) nam);
                            name.setFocusable(false);
                            name.setClickable(false);

                            phone.setText((CharSequence) phon);
                            phone.setFocusable(false);
                            phone.setClickable(false);

                            email.setText((CharSequence) emai);
                            email.setFocusable(false);
                            email.setClickable(false);

                            street.setText((CharSequence) stree);
                            street.setFocusable(false);
                            street.setClickable(false);

                            place.setText((CharSequence) plac);
                            place.setFocusable(false);
                            place.setClickable(false);

                        }
                    } while (rs.moveToNext());

                        if (!rs.isClosed()) {
                            rs.close();
                        }
                }
            if (!rs.isClosed()) {
                rs.close();
            }

        }
    }
}

public boolean onOptionsItemSelected(MenuItem item)
{
    super.onOptionsItemSelected(item);
    switch(item.getItemId())
    {
        case R.id.Edit_Contact:
            Button b = (Button)findViewById(R.id.button1);
            b.setVisibility(View.VISIBLE);
            name.setEnabled(true);
            name.setFocusableInTouchMode(true);
            name.setClickable(true);

            phone.setEnabled(true);
            phone.setFocusableInTouchMode(true);
            phone.setClickable(true);

            email.setEnabled(true);
            email.setFocusableInTouchMode(true);
            email.setClickable(true);

            street.setEnabled(true);
            street.setFocusableInTouchMode(true);
            street.setClickable(true);

            place.setEnabled(true);
            place.setFocusableInTouchMode(true);
            place.setClickable(true);

            return true;
        case R.id.Delete_Contact:

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.deleteContact)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            mydb.deleteContact(id_To_Update);
                            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                            startActivity(intent);
                        }


                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            AlertDialog d = builder.create();
            d.setTitle("Are you sure");
            d.show();

            return true;
        default:
            return super.onOptionsItemSelected(item);

    }
}


public boolean onOptionsItemSelected(MenuItem item)
{
    super.onOptionsItemSelected(item);
    switch(item.getItemId())
    {
        case R.id.Edit_Contact:
            Button b = (Button)findViewById(R.id.button1);
            b.setVisibility(View.VISIBLE);
            name.setEnabled(true);
            name.setFocusableInTouchMode(true);
            name.setClickable(true);

            phone.setEnabled(true);
            phone.setFocusableInTouchMode(true);
            phone.setClickable(true);

            email.setEnabled(true);
            email.setFocusableInTouchMode(true);
            email.setClickable(true);

            street.setEnabled(true);
            street.setFocusableInTouchMode(true);
            street.setClickable(true);

            place.setEnabled(true);
            place.setFocusableInTouchMode(true);
            place.setClickable(true);

            return true;
        case R.id.Delete_Contact:

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.deleteContact)
                    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            mydb.deleteContact(id_To_Update);
                            Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                            startActivity(intent);
                        }


                    })
                    .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            AlertDialog d = builder.create();
            d.setTitle("Are you sure");
            d.show();

            return true;
        default:
            return super.onOptionsItemSelected(item);

    }
}

public Integer deleteContact (Integer id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("contacts",
            "id = ? ",
            new String[] { Integer.toString(id) });
}

错误导致我开始这一行:

String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));

您应该检查 rs.moveToFist() return 是否为假。

如果为 false,则表示游标为空,因此当您尝试获取值时,您有一个 android.database.CursorIndexOutOfBoundsException.

你的getData(int) return一个空游标,因为你给他的id没有记录。

要删除 arrayList 中的数据,请更改:

array_list.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));

作者:

int idContacts =res.getInt(res.getColumnIndex(CONTACTS_ID)); 
array_list.add(idContacts, res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));

然后当您要删除联系人时使用:

 array_list.remove(idDelete);