有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

更新GridView的java问题

我正在尝试开发一款应用程序游戏,它包含12张显示卡,当你选择3张时,这些卡就会被替换。我为此构建了两个类,“positions”和“cards”onCreate()我制作了72张卡片,并将它们添加到一个ArrayList上,并创建了12个位置,这12个位置有一个int作为输入,表示是否选中,以及一张卡片。这张卡的一个输入是R.drawable.image1,在我的ImageAdapter中,我把mThumbIds作为我的MainActivity.p1.getCard().getImage()p1是position的静态实例,所有卡也是静态的。单击某个位置时,该位置会被选中,选择其中3个位置时,我有:

    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仅包含3张选定的卡片。所以基本上,现在我的mThumbIds中的位置有了新的卡片,因此有了新的图片,但是即使卡片更新了,图像也不会更新

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

图像适配器:

package com.example.set;

import 安卓.content.Context;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.BaseAdapter;
import 安卓.widget.GridView;
import 安卓.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()
};

}

主要活动。xml:

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

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

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

共 (0) 个答案