更新GridView时出现问题

时间:2013-01-06 20:35:45

标签: java android gridview

我正在尝试开发一款应用程序游戏,其中包含显示12张卡片,当您选择3张卡片时会被替换。我为此建立了两个类,“位置”和“卡片”。 onCreate()我制作72张牌并将它们添加到ArrayList并创建12个位置,这12个位置输入一个int,用于是否选择和卡片。该卡作为其中一个输入R.drawable.image1,并且在我的ImageAdapter中,我将mThumbIdsMainActivity.p1.getCard().getImage()作为我的p1 if (deck.size() > 0) { countSel.get(0).setCard(deck.remove(0)); countSel.get(1).setCard(deck.remove(1)); countSel.get(2).setCard(deck.remove(2)); gridview.invalidateViews(); } 是一个静态的位置实例,所有卡片也都是静态的。当点击一个位置时,它会被选中,当选择其中三个时,我有:

countSel

其中,deck包含所有卡片,package com.example.set; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public Integer[] getmThumbIds() { return mThumbIds; } public void setmThumbIds(Integer[] mThumbIds) { this.mThumbIds = mThumbIds; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(200, 200)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); notifyDataSetChanged(); } else { imageView = (ImageView) convertView; } imageView.setImageResource(mThumbIds[position]); notifyDataSetChanged(); return imageView; } // references to our images public Integer[] mThumbIds = { MainActivity.p1.getCard().getImage() , MainActivity.p2.getCard().getImage(), MainActivity.p3.getCard().getImage(),MainActivity.p4.getCard().getImage() , MainActivity.p5.getCard().getImage(), MainActivity.p6.getCard().getImage(), MainActivity.p7.getCard().getImage() , MainActivity.p8.getCard().getImage(), MainActivity.p9.getCard().getImage(), MainActivity.p10.getCard().getImage() , MainActivity.p11.getCard().getImage(), MainActivity.p12.getCard().getImage(), MainActivity.p13.getCard().getImage() , MainActivity.p14.getCard().getImage(), MainActivity.p15.getCard().getImage() }; 仅包含3张所选卡片。所以基本上,现在我的mThumbIds中的位置有新的卡片,因此新的图片,但即使卡片的图像也不会更新。

感谢您的帮助,以下是我的其余代码:

ImageAdapter:

    <?xml version="1.0" encoding="utf-8"?>
    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="200dp"
    android:numColumns="3"
    android:verticalSpacing="60dp"
    android:horizontalSpacing="20dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

}

activity_main.xml中:

        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            if (position == 0) {
                choose(p1);
            } else if (position == 1) {
                choose(p2);
            } else if (position == 2) {
                choose(p3);
            } else if (position == 3) {
                choose(p4);
            } else if (position == 4) {
                choose(p5);
            } else if (position == 5) {
                choose(p6);
            } else if (position == 6) {
                choose(p7);
            } else if (position == 7) {
                choose(p8);
            } else if (position == 8) {
                choose(p9);
            } else if (position == 9) {
                choose(p10);
            } else if (position == 10) {
                choose(p11);
            } else if (position == 11) {
                choose(p12);
            } else if (position == 12) {
                choose(p13);
            } else if (position == 13) {
                choose(p14);
            } else if (position == 14) {
                choose(p15);
            } else {
            }
            if (countSel.size() == 3) {
                countSel.get(0).setChosen(0);
                countSel.get(1).setChosen(0);
                countSel.get(2).setChosen(0);
        if (set(countSel.get(1), countSel.get(2), countSel.get(0))) {
                    {
        Toast.makeText(MainActivity.this, "Got a Set",
            Toast.LENGTH_LONG).show();
                    }

                    if (deck.size() > 0) {
                    countSel.get(0).setCard(deck.remove(0));
                    countSel.get(1).setCard(deck.remove(1));
                    countSel.get(2).setCard(deck.remove(2));

                    } else {
                        countSel.get(0).setCard(cardnull);
                        countSel.get(1).setCard(cardnull);
                        countSel.get(2).setCard(cardnull);

                    }
                } else {
                    Toast.makeText(MainActivity.this,
            "Not A Set" + "remaining" + countSel.size(),
                            Toast.LENGTH_LONG).show();
                }
                countSel.clear();
            }

            {
        Toast.makeText(MainActivity.this,
                    "chicken" + "remaining" +countSel.size(),
                        Toast.LENGTH_SHORT).show();
            }

        }
    });
    gridview.invalidateViews();
}

public void choose(Position p) {
    if (p.getChosen() == 0) {
        p.setChosen(1);
        countSel.add(p);

    }

    else if (p.getChosen() == 1) {
        p.setChosen(0);
        countSel.remove(p);
        System.out.println(countSel.size());
    } else
        System.out.println("emo");
}

这是我尝试刷新gridview的部分,如果有人认为错误存在,我会发布剩下的代码,我只是很难正确地格式化它:

{{1}}

0 个答案:

没有答案