【发布时间】: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