gpt4 book ai didi

java - 我无法删除 sqlite 中的日期

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:48 27 4
gpt4 key购买 nike

我在排除日期时遇到问题,并且仅当它不是 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() 是否返回 false。

如果为 false,则意味着光标为空,因此当您尝试获取该值时,会出现 android.database.CursorIndexOutOfBoundsException。

你的 getData(int) 返回一个空游标,因为你给他的 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);

关于java - 我无法删除 sqlite 中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35750634/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com