Android 使用新ArrayList更新GridView BaseAdapter时仍然引用原始值

Android 使用新ArrayList更新GridView BaseAdapter时仍然引用原始值,android,Android,我现在可以成功地更改GridView显示,以显示来自新ArrayList的一组新图像,但是我得到了一个 IndexOutOfBoundsException。(新的updateContent ArrayList是通过SecondActivity的意图发送的) 原始ID为[69,70,71,72,73,74,75],更新后的ID为[73,74,75],新位置0正在选择69 而不是73,但我(显然不是很成功)修改了BaseAdapter中的GetItem和GetItemId。这个 ArrayList

我现在可以成功地更改GridView显示,以显示来自新ArrayList的一组新图像,但是我得到了一个 IndexOutOfBoundsException。(新的updateContent ArrayList是通过SecondActivity的意图发送的)

原始ID为[69,70,71,72,73,74,75],更新后的ID为[73,74,75],新位置0正在选择69 而不是73,但我(显然不是很成功)修改了BaseAdapter中的GetItem和GetItemId。这个 ArrayList on load(imageCollection)输入非常完美,但updateContent one anyCollection会导致问题。LogCat 及相关代码如下:

public ImageAdapter(Context c, ArrayList<String> anyCollection) {
    super();                                      //--- We do not come here for updateContent --
    this.context = c;
    this.anyCollection = anyCollection;
    this.colCount = anyCollection.size();
    Log.d("DEBUG","Line 200 Size: " + this.anyCollection.size() + " Values: " + this.anyCollection);
}

public void updateContent (ArrayList<String> updates) {
    this.anyCollection.clear();   
    this.anyCollection = updates;           //--This is an ArrayList of 3 numbers, 69,71,72.
    this.colCount = anyCollection.size()
    Log.d("DEBUG","Line 207 Size: " + this.anyCollection.size() + " Values: " + this.anyCollection);  
    this.notifyDataSetChanged();
}
....
public Object getItem(int position) {
    String item = anyCollection.get(position);
    int index = imageCollection.indexOf(item);
    Log.d("DEBUG", "Line224 Index: " + index);    
    getCursor.moveToPosition(index);
    Log.d("DEBUG", "Line 197: String " + " " + position + " " + getCursor.getString(1));
    return getCursor.getString(1);
}
public long getItemId(int position) {
   String item = anyCollection.get(position);   //--Extract value from this position.
   int index   = imageCollection.indexOf(item); //--Find this value in imageCollection.
   Log.d("DEBUG", "Line 230 Position: " + position + " Size: " + imageCollection.size() + " Index: " + index); 
   getCursor.moveToPosition(index); 
   id = getCursor.getLong(0);                   //--- This is line 238
   return id;
}

D/DEBUG﹕ Line 200 Size: 16 Values: [37, 47, 49, 50, 51, 52, 53, 54, 55, 63, 64, 65, 69, 70, 71, 72] //anyCollection onLoad.
D/DEBUG﹕ Line 230 Position: 0 Size: 16 Index: 0    //Correct on loading.
....
D/DEBUG﹕ Line 207 Size: 3 Values: [69, 71, 72]     //anyCollection via Intent from SecondActivity.
D/DEBUG﹕ Line 230 Position: 0 Size: 0 Index: -1    //Should be Position 0 Size 3 Index 12! 
(Exception here).

 java.lang.IllegalStateException: Could not execute method of the    activity at android.view.View$1.onClick(View.java:3823)
 at  abc.qwerty.intents.MainActivity$ImageAdapter.getItemId(MainActivity.java:238)
  Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 16
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main); 

allImages = Images.Media.EXTERNAL_CONTENT_URI;
ArrayList<String> imageCollection = new ArrayList<String>();

String[] projection = {
      MediaStore.Images.Media._ID,
      MediaStore.Images.Media.DATA
};
getCursor = getContentResolver().query(
      allImages,
      projection,
      null,
      null,
      null
);
columnIndex = getCursor.getColumnIndexOrThrow(projection[0]);  //-- ID number.
arrayIndex  = getCursor.getColumnIndexOrThrow(projection[1]);  //-- Filepath.
imageCollection.add(String.valueOf(columnIndex));
02-13 10:05:17.330 1120-1120/?E/AndroidRuntime﹕ 致命异常:主
进程:com.seastar.intents,PID:1120
java.lang.IllegalStateException:无法执行活动的方法
在android.view.view$1.onClick上(view.java:3823)
在android.view.view.performClick上(view.java:4438)
在android.view.view$PerformClick.run(view.java:18422)
位于android.os.Handler.handleCallback(Handler.java:733)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:136)
位于android.app.ActivityThread.main(ActivityThread.java:5017)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
在dalvik.system.NativeStart.main(本机方法)
原因:java.lang.reflect.InvocationTargetException
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在android.view.view$1.onClick上(view.java:3818)
在android.view.view.performClick上(view.java:4438)
在android.view.view$PerformClick.run(view.java:18422)
位于android.os.Handler.handleCallback(Handler.java:733)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:136)
位于android.app.ActivityThread.main(ActivityThread.java:5017)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
在dalvik.system.NativeStart.main(本机方法)
原因:android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为16
在android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)中
位于android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
位于android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
位于android.database.CursorWrapper.getLong(CursorWrapper.java:106)
位于com.seastar.intents.MainActivity$ImageAdapter.getItemId(MainActivity.java:240)
在android.widget.AdapterView.rememberSyncState(AdapterView.java:1195)
位于android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:811)
在android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6280)中
位于android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
位于android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
位于com.seastar.intents.MainActivity$ImageAdapter.updateContent(MainActivity.java:209)
位于com.seastar.intents.MainActivity.onimageslick(MainActivity.java:80)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在android.view.view$1.onClick上(view.java:3818)
在android.view.view.performClick上(view.java:4438)
在android.view.view$PerformClick.run(view.java:18422)
位于android.os.Handler.handleCallback(Handler.java:733)
位于android.os.Handler.dispatchMessage(Handler.java:95)
位于android.os.Looper.loop(Looper.java:136)
位于android.app.ActivityThread.main(ActivityThread.java:5017)
位于java.lang.reflect.Method.Invokenactive(本机方法)
位于java.lang.reflect.Method.invoke(Method.java:515)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)上
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

在dalvik.system.NativeStart.main(本机方法)
问题最终解决了,但非常奇怪!注释掉下面这行可以阻止java崩溃,但我不明白为什么。 剩下的唯一问题是将新位置传递给imagesCollection,因为现在出现了错误的图片 SetoniceMcClicklistener;但这是一个小问题

 public void updateContent (ArrayList<String> updates) {
    this.imageCollection = anyCollection;
    // this.anyCollection.clear();
    this.anyCollection = updates;
 }
public void updateContent(ArrayList更新){
this.imageCollection=anyCollection;
//this.anyCollection.clear();
this.anyCollection=更新;
}

显示imagesCollection的代码,并显示包含错误的整个堆栈跟踪。
 public void updateContent (ArrayList<String> updates) {
    this.imageCollection = anyCollection;
    // this.anyCollection.clear();
    this.anyCollection = updates;
 }