在运行时更新和删除GridView项

时间:2014-03-14 10:32:16

标签: java android gridview android-listview arraylist

我正在开发一个项目,我比较两个项目的图像,所以如果两个项目在点击这些项目后将具有相同的图像应该隐藏。我的代码如下所示,此代码遇到问题。这是一个逻辑错误还是任何其他问题?我试着解决这个问题,但没有解决..请指导我......这是我的主要活动 MainActivity.java

public class MainActivity extends Activity {
    Context ctx;
    int imagesArray[];
    ImageAdapter adapter;
    List<Integer> pictures;
    boolean flage = false;
    GridView gridView;
    int save1, save2;
    int img1 = -1, img2 = -1;
    public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_0,
            R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3 };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView myimage = new ImageView(ctx);
        gridView = (GridView) findViewById(R.id.gv_memory);
        gridView.setAdapter(adapter);
         shuffleArray();

        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                myimage.setImageResource(pictures.get(position));

                if (flage == false) {

                    img1 = pictures.get(position);
                    flage = true;

                } else if (flage == true) {

                    img2 = pictures.get(position);

                    checkResult();

                    flage = false;
                }
        }
            private void checkResult() {
                // TODO Auto-generated method stub

                if (img1 == img2) {
                    adapter.pictureList.remove(img1);
                    adapter.pictureList.remove(img2);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(MainActivity.this, "Congratualatin !!!!",
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(MainActivity.this, "Sorry!!!!",
                            Toast.LENGTH_LONG).show();

                }
            }
        });
    }

    private void shuffleArray() {
        // TODO Auto-generated method stub
        pictures = new ArrayList<Integer>();
        for (int index = 0; index < OriginalArray.length; index++) {
            pictures.add(OriginalArray[index]);
        }
        Collections.shuffle(pictures);

    }


}       

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

    private Context context;
    List<Integer> pictureList = new ArrayList<Integer>();

    public ImageAdapter(Context c) {
        context = c;
         for (int i = 0; i < 8; i++) {
             pictureList.add(R.drawable.question);
         }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return (pictureList.size());
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return pictureList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView myimage = new ImageView(context);
        myimage.setImageResource(pictureList.get(position));
        myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
        return myimage;
    }

}       

logcat的。

03-14 06:09:03.793: D/dalvikvm(2877): GC_FOR_ALLOC freed 54K, 8% free 2771K/2996K, paused 111ms, total 117ms
03-14 06:09:03.802: I/dalvikvm-heap(2877): Grow heap (frag case) to 3.943MB for 1127536-byte allocation
03-14 06:09:03.922: D/dalvikvm(2877): GC_FOR_ALLOC freed 2K, 6% free 3870K/4100K, paused 118ms, total 118ms
03-14 06:09:03.962: D/AndroidRuntime(2877): Shutting down VM
03-14 06:09:03.962: W/dalvikvm(2877): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-14 06:09:03.972: E/AndroidRuntime(2877): FATAL EXCEPTION: main
03-14 06:09:03.972: E/AndroidRuntime(2877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memory/com.example.memory.MainActivity}: java.lang.NullPointerException
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.os.Looper.loop(Looper.java:137)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at java.lang.reflect.Method.invokeNative(Native Method)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at java.lang.reflect.Method.invoke(Method.java:525)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at dalvik.system.NativeStart.main(Native Method)
03-14 06:09:03.972: E/AndroidRuntime(2877): Caused by: java.lang.NullPointerException
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.view.View.<init>(View.java:3264)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.widget.ImageView.<init>(ImageView.java:112)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at com.example.memory.MainActivity.onCreate(MainActivity.java:33)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.Activity.performCreate(Activity.java:5133)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-14 06:09:03.972: E/AndroidRuntime(2877):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-14 06:09:03.972: E/AndroidRuntime(2877):     ... 11 more
03-14 06:14:04.503: I/Process(2877): Sending signal. PID: 2877 SIG: 9

1 个答案:

答案 0 :(得分:1)

你有

final ImageView myimage = new ImageView(ctx);

ctz未初始化。它只被声明为Context ctx;

你有这个

gridView.setAdapter(adapter);

但是在使用相同的

之前,你需要初始化adapter

所以改为

setContentView(R.layout.main);
final ImageView myimage = new ImageView(this); //this refers to Activity context
gridView = (GridView) findViewById(R.id.gv_memory);
adapter = new ImageAdapter(this)
gridView.setAdapter(adapter);