更改活动并在点击时重置gridview

时间:2016-12-14 20:13:56

标签: java android gridview

我正在制作一个国际象棋应用程序,需要帮助来结束游戏。

点击结束游戏按钮后,我想返回主屏幕并重置持有游戏板的gameActivity。该板是一个网格视图,可以保存棋子的图像。

我尝试过的一些事情:

Intent intent = getIntent()
finish()
startActivity(intent)

我意识到这只会重新启动活动,现在返回主活动。但即使这样,网格视图也会保留,并且不会重置碎片。

pieceGrid.setAdapter(new PieceAdapter());
pieceAdapter = new(PieceAdapter(context));
finish();

这将完成活动并返回主活动,但在开始新游戏时,碎片仍保留在之前所处的位置。我认为我误解了适配器和上下文如何协同工作但不能将它拼凑在一起。下面是我的PieceAdapter类。

PieceAdapter.java

public class PieceAdapter extends BaseAdapter {
private Context mContext;

public PieceAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

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) {
    MyImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new MyImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (MyImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}
// references to our images
public static Integer[] mThumbIds = {
        R.drawable.blackrook, R.drawable.blackknight, R.drawable.blackbishop, R.drawable.blackqueen, R.drawable.blackking, R.drawable.blackbishop, R.drawable.blackknight, R.drawable.blackrook,
        R.drawable.blackpawn, R.drawable.blackpawn, R.drawable.blackpawn, R.drawable.blackpawn, R.drawable.blackpawn, R.drawable.blackpawn, R.drawable.blackpawn, R.drawable.blackpawn,
        R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare,
        R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare,
        R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare,
        R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare, R.drawable.emptysquare,
        R.drawable.whitepawn, R.drawable.whitepawn, R.drawable.whitepawn, R.drawable.whitepawn, R.drawable.whitepawn, R.drawable.whitepawn, R.drawable.whitepawn, R.drawable.whitepawn,
        R.drawable.whiterook, R.drawable.whiteknight, R.drawable.whitebishop, R.drawable.whitequeen, R.drawable.whiteking, R.drawable.whitebishop, R.drawable.whiteknight, R.drawable.whiterook
};

如果我不清楚,请道歉。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

似乎顺序调用finish()然后直接调用startActivity(intent)就什么都不做。

尝试

Intent i = new Intent(YourActivity.class);
i.setFlags(FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);
startActivity(i);