Android 带图像适配器(baseadapter)的后退按钮

Android 带图像适配器(baseadapter)的后退按钮,android,gridview,back-button,Android,Gridview,Back Button,我希望我没有搞砸!但我已经创建了一个应用程序,并且已经完成了,但当我点击后退按钮时,它关闭了应用程序。我现在意识到,我只是在改变对我的主要活动的看法,而不是使用intent启动单独的活动。该应用程序有3个按钮,可加载3个不同的“视图”(简单、中等、硬),每个按钮都充满了一系列图像。我会发布一些代码,但不确定需要什么。我是否应该覆盖后退按钮以返回MainActivity视图?不确定解决这个问题的最佳方法。谢谢 public void onClick(View v) { switch (v

我希望我没有搞砸!但我已经创建了一个应用程序,并且已经完成了,但当我点击后退按钮时,它关闭了应用程序。我现在意识到,我只是在改变对我的主要活动的看法,而不是使用intent启动单独的活动。该应用程序有3个按钮,可加载3个不同的“视图”(简单、中等、硬),每个按钮都充满了一系列图像。我会发布一些代码,但不确定需要什么。我是否应该覆盖后退按钮以返回MainActivity视图?不确定解决这个问题的最佳方法。谢谢

 public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnEasy:

            creatArray();
            btnEasy.setVisibility(View.INVISIBLE); //hides button on the second activity
            btnMed.setVisibility(View.INVISIBLE);
            btnHard.setVisibility(View.INVISIBLE);
            btnHome.setVisibility(View.VISIBLE);
            btnReset.setVisibility(View.VISIBLE);
            btnFloat.setVisibility(View.INVISIBLE);

            grid=(GridView)findViewById(R.id.grid);  //grid is @id of grid in MainActivity
            grid.setVisibility(View.VISIBLE);

            EasyGridAdapter adapter = new EasyGridAdapter(MainActivity.this, randomPics, overlay); //have to add all images (arrays here)  references arrays above
            grid.setAdapter(adapter);



            break; 




public class EasyGridAdapter extends BaseAdapter {
private int[] easyPics;
private int[] overlayPic; //used for overlay ...this is just created here

Context myContext;

String pos0,pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8;  //for scoring game




public class Holder
{
    RelativeLayout rlParent;  //main layout (custom_easy.xml)
    ImageView iv;  //for images (grid) main ones
    ImageView ol;  //for overlay
}


public EasyGridAdapter(Context myContext, int easyPics[], int overlayPic[]){
    //sets gridadapter to use pics , if had text under pic would need to add after int[] pics
    // name EasyGridAdapter is name ot the java class
    this.myContext = myContext;
    this.easyPics = easyPics;
    this.overlayPic = overlayPic;
}

@Override
public int getCount() {
    return easyPics.length;

}

@Override
public Object getItem(int position) {
    return easyPics[position]; //worked with just easypics
   // return position;

}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView (final int position, View convertView, ViewGroup parent) {
    View grid = convertView;  //grid (not sure if this is the same) is @id of grid on activity main
    final Holder holder=new Holder();
    if (convertView == null){

        LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //think inflates above public EasyGridAdapter
        grid = inflater.inflate(R.layout.custom_easy, null);

    }

    //main view (i think inializing veiw)
    holder.rlParent = (RelativeLayout) grid.findViewById(R.id.customParent); //customeParent @id set on custom_easy.xml
    holder.iv = (ImageView) grid.findViewById(R.id.easyPics); //EasyPics is android id name of imageviewer in custom easy xml
    holder.ol = (ImageView) grid.findViewById(R.id.overlayPic);



    holder.iv.setImageResource(easyPics[position]); //easypics is the interger from above
    holder.rlParent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (position==0)
            {

    REMOVED TO SAVE SPACE...


    holder.ol.setVisibility(View.VISIBLE);


        }
    });



    return grid;  //returns grid which is name of grid on activity_main.xml (with all stuff in it)



}

    }

好的,我找到了这个,它似乎起作用了!我希望这对其他人有帮助

但是在我的MainActivity.java中,我现在添加了以下代码,当我点击后退按钮时,它将返回到主屏幕

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    Intent in = new Intent(MainActivity.this,MainActivity.class);
    startActivity(in);
   // finish();

}