简体   繁体   中英

Hide items in GridView if two items have same images using Android

i am developing a project using GridView where i need to hide items if a user click such two items having same images then both item should be hide.
First time when project will load it will have one Question Marks image on all item. when user click item the background image will show then if he click another item having the same picture then both item should be hide if not, it again show the Question Marks image.
How this is possible?
here is my MainActivity.java

public class MainActivity extends Activity {
    Context ctx;
    int imagesArray[];
    GridViewContent adapter;
    List<Integer> pictures;
    boolean flage=false;
    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);
        shuffleArray();

        adapter= new GridViewContent(this);

        final GridView grid = (GridView) findViewById(R.id.gv_memory);
        grid.setAdapter(new GridViewContent(this));
    }
    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);

    }
    public class GridViewContent extends BaseAdapter {
        private Context context;

        //abstract change();

        public int pictureArray[]={
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,
                R.drawable.question,        
        };
        public GridViewContent(Context c){
            context=c;

        }

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

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return pictureArray[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup arg2) {
            // TODO Auto-generated method stub
            convertView=LayoutInflater.from(context).inflate(R.layout.main, null);
            final ImageView myimage=new ImageView(context);
            myimage.setImageResource(pictureArray[position]);
            //myimage.setImageResource(pictures.get(pictureArray[position]));
            myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
            myimage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                //  int post1,post2;

                    myimage.setImageResource(pictures.get(position));

                if(flage==false)
                {

                img1=pictures.get(position);

                flage=true;

                }else if(flage==true){
                    //post2=position;
                img2=pictures.get(position);
                checkResult();
                //notifyDataSetChanged();
                flage=false;
                }
                //else if(f)
                }
            });

            return myimage;
        }

    }
    public void checkResult() {
        if(img1==img2)
        {
            //pictures.remove(post2);

            adapter.notifyDataSetChanged();

            Toast.makeText(MainActivity.this, "Congratualatin !!!!", Toast.LENGTH_LONG).show();
        }
        else{
            Toast.makeText(MainActivity.this, "Sorry!!!!", Toast.LENGTH_LONG).show();
            final GridView grid = (GridView) findViewById(R.id.gv_memory);
            grid.setAdapter(new GridViewContent(this));
        }

    }
}

You could try and remember the index of a GridView item you clicked. When a second click occurs, check the remembered index. If it is populated (meaning the user already clicked on an arbitrary image once), extract both drawables from the adapter and check if they are equal. If they are, remove them from the adapter and update it. Do not forget to reset the remembered index.

In code, this might look something like this:

int firstClickIndex = -1;

grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
        if (-1 == firstClickIndex) {
            firstClickIndex = position;
        } else {
            final int fstDrawable = adapter.get(firstClickIndex);
            final int sndDrawable = adapter.get(position);

            if (fstDrawable == sndDrawable) {
                // Remove stuff, then reset firstClickIndex.
            } else {
                // Reset firstClickIndex.
            }

        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM