在运行时更新并删除GridView项[英] Update and remove GridView items at runtime

本文是小编为大家收集整理的关于在运行时更新并删除GridView项的处理方法,想解了在运行时更新并删除GridView项的问题怎么解决?在运行时更新并删除GridView项问题的解决办法?在运行时更新并删除GridView项问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我正在开发一个项目,其中我比较了两个项目的图像,因此如果两个项目单击这些项目后,如果两个项目将具有相同的图像应该是隐藏的.下面给出了我的代码,这个代码遇到了一个问题.这是一个逻辑错误还是任何其他问题?我试图解决这个问题,但并不解决..请指导我......这是我的主要活动.

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

    }


}       

imageapapter.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

推荐答案

你有

final ImageView myimage = new ImageView(ctx);

ctz未初始化.它唯一宣称为Context ctx;

你有这个

gridView.setAdapter(adapter);

但是在使用相同的

之前,需要intialize 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);

本文地址:https://www.itbaoku.cn/post/157747.html

问题描述

i am developing a project where i compare the images of two items,So if two items will have same image after clicking these items should be hide. my code is given below and this code encounter a problem. is this a logical error or any other issue? i try to solve the issue but did't resolve.. Please guide me... here is my main Activity.
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

推荐答案

You have

final ImageView myimage = new ImageView(ctx);

ctz is not initialized. Its only declared as Context ctx;

You have this

gridView.setAdapter(adapter);

But you need to intialize adapter before using the same

So change to

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);
查看更多