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

时间:2015-02-13 11:52:26

标签: android

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

原始身份证号码是[69,70,71,72,73,74,75],更新的身份证号码是[73,74,75],新的位置0是选择69 而不是73,但我(明显不太成功)修改了BaseAdapter中的 GetItem GetItemId 。该 加载时的ArrayList( 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

由于某种原因,imagesCollection.size()在这里没有正确到达第230行。我又错过了一些简单的东西吗? imageCollection代码如下:

 @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));

再次运行并在下面发布另一个LogCat输出:

02-13 10:04:54.300    1120-1120/? D/DEBUG﹕ Line 200 Size: (0) 16 Values: [37, 47, 49, 50, 51, 52, 53, 54, 55, 63, 64, 65, 69, 70, 71, 72]
02-13 10:04:54.300    1120-1120/? D/DEBUG﹕ Line 219 ColCount: 16
02-13 10:04:54.300    1120-1120/? D/DEBUG﹕ Line 219 ColCount: 16
02-13 10:04:54.340    1120-1120/? D/DEBUG﹕ Line 234 Index: 37
02-13 10:04:54.340    1120-1120/? D/DEBUG﹕ Line 236 Position: 0 Size: 16 Index: 0 (0)
02-13 10:04:54.340    1120-1120/? D/DEBUG﹕ Line 234 Index: 37
02-13 10:04:54.340    1120-1120/? D/DEBUG﹕ Line 236 Position: 0 Size: 16 Index: 0 (0)
02-13 10:04:54.890    1120-1120/? D/DEBUG﹕ Line 219 ColCount: 16
02-13 10:04:54.890    1120-1120/? D/DEBUG﹕ Line 248 position: 0
02-13 10:04:55.530    1120-1120/? D/OpenGLRenderer﹕ Enabling debug mode 0

02-13 10:05:12.460    1120-1120/? D/DEBUG﹕ Line 248 position: 0
02-13 10:05:17.040    1120-1120/? D/DEBUG﹕ Line 63 Images Size: 16 Set: 16
02-13 10:05:17.050    1120-1120/? D/DEBUG﹕ Line 294 Array Size: 3
02-13 10:05:17.050    1120-1120/? D/DEBUG﹕ Line 207 Size: 3 Values: [69, 71, 72]
02-13 10:05:17.050    1120-1120/? D/DEBUG﹕ Line 208: Number of images: 3
02-13 10:05:17.050    1120-1120/? D/DEBUG﹕ Line 219 ColCount: 3
02-13 10:05:17.050    1120-1120/? D/DEBUG﹕ Line 219 ColCount: 3
02-13 10:05:17.060    1120-1120/? D/DEBUG﹕ Line 234 Index: 69
02-13 10:05:17.060    1120-1120/? D/DEBUG﹕ Line 236 Position: 0 Size: 0 Index: -1 (0)

02-13 10:05:17.330    1120-1120/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.seastar.intents, PID: 1120
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3823)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            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)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            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)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 16
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
            at android.database.CursorWrapper.getLong(CursorWrapper.java:106)
            at com.seastar.intents.MainActivity$ImageAdapter.getItemId(MainActivity.java:240)
            at android.widget.AdapterView.rememberSyncState(AdapterView.java:1195)
            at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:811)
            at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:6280)
            at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
            at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
            at com.seastar.intents.MainActivity$ImageAdapter.updateContent(MainActivity.java:209)
            at com.seastar.intents.MainActivity.onImagesClick(MainActivity.java:80)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            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)

1 个答案:

答案 0 :(得分:0)

问题最终解决了,但非常奇怪!评论下面的行阻止了java崩溃,但我不明白为什么。 唯一剩下的问题是将新位置传递给 imagesCollection ,因为错误的图片现在出现了 SetOnItemClickListener ;但这是一个小问题。

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