2017-04-18 38 views
0

我希望我沒有搞砸了!但是我已經創建了應用程序,並且完成了它,但是當我點擊後退按鈕時,它會關閉應用程序。我現在意識到,我只是改變了我的主要活動的觀點,而不是使用意向發起單獨的活動。該應用程序有3個按鈕來加載3個不同的「意見」(簡單,中等,硬),每個都充滿了一系列圖像。我會發布一些代碼,但不知道需要什麼。我應該重寫後退按鈕以返回到MainActivity視圖嗎?不知道解決這個問題的最佳方法。謝謝後退按鈕與圖像適配器(baseadapter)

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) 



} 

    } 

回答

0

好吧,我發現這個,它似乎工作!我希望這可以幫助別人。

但在我的MainActivity.java我現在添加了下面的代碼當我點擊後退按鈕它將返回到主屏幕!

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

}