此方法将来自不同应用的内容提供商的数据填充到列表视图中,我的问题出在onItemClick上,我认为该方法的'long id'返回错误的ID ,有时它会逐渐减少我删除了一个条目,因为在这种情况下,每当我尝试删除或更新一行时,大多数情况下它会产生错误(CursorIndexOutOfBounds),但有些实例会更新或删除不同的行。< / p>
private void populateList(){
arNames = new ArrayList<String>();
String name;
Cursor cursor = getContentResolver().query(PATH_URI, null, null, null, null);
cursor.moveToFirst();
do{
name = cursor.getString(cursor.getColumnIndex("FULL"));
arNames.add(name);
}while(cursor.moveToNext());
cursor.close();
final CustomAdapter customAdapter = new CustomAdapter(inflater, arNames);
listNames.setAdapter(customAdapter);
listNames.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listNames.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = getContentResolver().query(PATH_URI, null, "_id=" + id, null, null);
c.moveToFirst();
view.setBackgroundColor(Color.GRAY);
cNames[coupler] = c.getString(c.getColumnIndex("FULL"));
Toast.makeText(getApplicationContext(),cNames[coupler],Toast.LENGTH_LONG).show();
if (cNames[0].equals(cNames[1]) && coupler == 1) {
showEditor(id);
} else if (coupler++ == 1) {
coupler = 0;
populateList();
Toast.makeText(getApplication(), flameOn(cNames[0], cNames[1]), Toast.LENGTH_LONG).show();
}
}
});
}
12-17 16:31:52.075 3852-3852/com.example.den.flamesclient E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.den.flamesclient, PID: 3852
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at com.example.den.flamesclient.MainActivity$1.onItemClick(MainActivity.java:71)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
at android.widget.AbsListView.onTouchUp(AbsListView.java:3650)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3429)
at android.view.View.dispatchTouchEvent(View.java:7706)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
at android.view.View.dispatchPointerEvent(View.java:7886)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:138)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
检查以下情况并编写代码。
如果(c.moveToFirst()){
//编写代码
}
在列表项中单击您将给出列表项位置,这与数据库ID检查一次不同。
答案 1 :(得分:0)
在继续之前,应始终为光标指定空指针:
....
if( c != null && c.moveToFirst() ){
// code comes here
c.close(); // Make sure to do this..
}
答案 2 :(得分:0)
我找到了我的行ID提供错误ID的原因。 在意识到这个错误之后,我真的不会把它称为'错误'。
您会看到游标在列表视图中查询数据并将其投影时,实际上并不会查询已删除的数据。我的错误是我忘记了如果执行了一个删除语句,dbId将不会被重构,id将保持原样,在我们的情况下将保持固定,而它的'相邻列将被删除。
listNames.setOnItemClickListener(new AdapterView.OnItemClickListener()
Cursor c = getContentResolver().query(PATH_URI, null, "_id=" + id, null, null);
上面的代码段是我出错的地方。我正在比较listview id(更可能是列表视图中的项目位置)和内容提供者db id。
我所做的是比较一个不同的行,更具体地说是我的数据库中的一个字符串和我的ListView上的投影,我首先设置了我的arraylist(arNames)全局。然后这个:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor c = getContentResolver().query(PATH_URI, null, "COLUMN_NAME= '" + arNames.get(position) + "'", null, null);