【问题标题】:Error when deleting an item from listview in android从android中的listview中删除项目时出错
【发布时间】:2016-09-24 12:33:24
【问题描述】:

我想学习如何在 android 中使用 sqlite,所以我使用了教程网站上的以下代码,但是当我运行它时,如果我删除 listview 中的第一个条目,则下一个条目会给出

android.database.CursorIndexOutOfBoundsException: 请求索引 -1, DisplayContact 类中的大小为 0。

所以想知道怎么解决,不知道是listview还是arraylist的问题。

提前致谢。

这是显示错误的代码

    Bundle extras = getIntent().getExtras();
        if (extras != null)
{
            int Value = extras.getInt("id");
            if (Value > 0)
{
                Cursor rs = mydb.getData(Value);
                id_To_Update = Value;
                if (rs != null && rs.getCount() > 0)
{
                    rs.moveToFirst();
                }

String dat = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_DATE));
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));
String nam = rs.getString(rs.getColumnIndex(DBHelper.CONTACTS_COLUMN_NAME));
if (!rs.isClosed())
{
rs.close();
}

这里是删除条目的代码

    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)
{ }
}
);
                AlertDialog d =builder.create();
                d.setTitle("Are you sure?");
                d.show();
                return true;
            default:

这是logcat输出05-20

12:59:33.953 E/AndroidRuntime(25513): Process: com.myelse.sqlcontacts, PID: 25513
    05-20 12:59:33.953 E/AndroidRuntime(25513): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myelse.sqlcontacts/com.myelse.sqlcontacts.DisplayContact}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
    05-20 12:59:33.953 E/AndroidRuntime(25513): at com.myelse.sqlcontacts.DisplayContact.onCreate(DisplayContact.java:51)

编辑:这是mainactivity中arraylist和arrayadapter的代码

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mydb = new DBHelper(this);
    ArrayList arraylist = mydb.getAllContacts();
    arrayadapter= new ArrayAdapter (this, android.R.layout.simple_list_item_1 ,arraylist);
    arrayadapter.notifyDataSetChanged();
    obj = (ListView) findViewById(R.id.listView1);
    obj.setAdapter(arrayadapter);
    obj.setOnItemClickListener(new OnItemClickListener() {


            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                int id_to_search=arg2+1;
                Toast.makeText(getApplicationContext(), id_to_search + "" , Toast.LENGTH_SHORT).show();
                Bundle dataBundle = new Bundle();
                dataBundle.putInt("id", id_to_search);
                Intent intent = new Intent(getApplicationContext(), DisplayContact.class);
                intent.putExtras(dataBundle);
                startActivity(intent);
                arrayadapter.notifyDataSetChanged();
            }
        }

    );

}

【问题讨论】:

  • 我假设您需要重新加载数组,然后更新 ListAdapter,因为数据已更改。
  • 我试图调用来自另一个类的数组列表,它给了我 nullpointerexception 我不知道如何同时从数据库和数组列表中删除
  • 我们可以看到ArrayList的代码
  • 请发布正确的代码
  • @andre3wap 我编辑了arraylist的代码。

标签: android sqlite listview indexoutofboundsexception


【解决方案1】:

问题出在一行:

Cursor rs = mydb.getData(Value);

mydb.getData(Value) 将只返回一行,其中 "id" = Value

因此,当您删除这一行时,光标会返回 IndexOutOfBoundsException

一种可能的解决方案是修改您的getData 函数,如下所示:

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

【讨论】:

  • 感谢您的回复,错误已经消失,但现在删除后列表视图中的 id 从 sqldatabase 中减少了一个,所以当我例如按下列表中的一个项目时,我实际上得到了前一个在数据库中注册的条目。
  • 奇怪的是,当我删除 id 为 1 的项目时,它在数据库中从 id 2 开始,但在列表视图中它仍然从 id 1 开始
  • 我可以重置数据库中的id列吗谢谢
【解决方案2】:

感谢@parth mehta 的回复,但我发现在删除条目后保留顺序的唯一解决方案是将所有记录复制到没有 id 列的临时表中,然后在每次之后将它们移回原始表删除。 使用 insert into table 1 () 从 table2 中选择。

谢谢。

【讨论】:

    【解决方案3】:

    请在删除事件后添加 Notifydatsetchange()

    【讨论】:

    • 谢谢,我试过了,但是由于适配器是在另一个类中声明的,所以它给出了空指针异常,但是当我回答时,我发现唯一有效的方法是使用 sql 命令
    猜你喜欢
    • 2018-01-31
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多