2014-10-29 2 views
0

다른 페이지를 호출하는 버튼이있는 pageradapter를 사용합니다. 세컨더 액티비티를 호출 한 후 페이저 액티비티를 완료 할 수 있습니까? 내 pageradapter 코드는 아래와 같습니다. 메서드 finish()는 new 유형에 대해 정의되지 않았습니다. View.OnClickListener() {} 감사합니다.pageradpter에서 어떻게 활동을 끝낼 수 있습니까?

public class ImageAdapterFromRes extends PagerAdapter { 


    Context context; 
    ArrayList<String> textArray; 
    ArrayList<String> urlArray; 
    ArrayList<Bitmap> bitmapArray; 
    ImageView imageView; 
    TextView textreklama1; 
    Button btnZoznam; 
    public Activity activity; 

    private int[] GalImages = new int[] { 
      //Images from resource folder. 
     R.drawable.one, 
     R.drawable.two, 
     R.drawable.three 
    }; 

    ImageAdapterFromRes(Context context, ArrayList<String> textArray, ArrayList<String> urlArray, ArrayList<Bitmap> bitmapArray){ 
     this.context=context; 
     this.textArray=textArray; 
     this.urlArray=urlArray; 
     this.bitmapArray=bitmapArray; 
    } 

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

    public Object instantiateItem(ViewGroup collection, int position) { 
     LayoutInflater inflater = (LayoutInflater) collection.getContext() 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View view = inflater.inflate(R.layout.reklamator_new, null); 

     imageView = (ImageView) view.findViewById(R.id.imgreklama1); 
     imageView.setImageBitmap(bitmapArray.get(position)); 

     textreklama1 = (TextView) view.findViewById(R.id.textreklama1); 
     textreklama1.setText(textArray.get(position).toString()); 

     btnZoznam = (Button) view.findViewById(R.id.btnZoznam); 
     btnZoznam.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(v.getContext(), SecondActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("cat", "1"); 
       i.putExtras(extras); 
       context.startActivity(i); 
       //finish(); ???????? HOW FINISH ? 

      } 
     }); 

     ((ViewGroup) collection).addView(view, 0); 
     return view; 
     } 

     @Override 
     public void destroyItem(ViewGroup arg0, int arg1, Object arg2) { 
     ((ViewGroup) arg0).removeView((View) arg2); 
     } 

     @Override 
     public boolean isViewFromObject(View arg0, Object arg1) { 
     return arg0 == ((View) arg1); 
     } 


} 

답변

1

내가 활동이 무엇을 처리 제안 (당신은 나중에 조각에 배치 할 수 있음).

간단히 ((Activity) context).finish() 수 있지만 favour composition over aggregation 경향이 있습니다.

여기에 빠른 조각입니다 :

public class ImageAdapterFromRes extends PagerAdapter { 

    OnPagerItemSelected mListener; 
    Context context; 

    ImageAdapterFromRes(Context context, ArrayList<String> textArray, ArrayList<String> urlArray, ArrayList<Bitmap> bitmapArray, 
     OnPagerItemSelected listener 
     ){ 

     this.context=context; 
     this.textArray=textArray; 
     this.urlArray=urlArray; 
     this.bitmapArray=bitmapArray; 

     this.mListener = listener; 
    } 

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

    public Object instantiateItem(ViewGroup collection, int position) { 

     btnZoznam.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent i = new Intent(v.getContext(), SecondActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putString("cat", "1"); 
       i.putExtras(extras); 
       context.startActivity(i); 
       //finish(); ???????? HOW FINISH ? 

       mListener.pagerItemSelected(); 
      } 
     }); 
    } 

    public interface OnPagerItemSelected { 
     void pagerItemSelected(); 
    } 


} 

공지 사항 청취자가있다 및 항목을 클릭 할 때 pagerItemSelected() 메서드가 호출됩니다.

public class MyActivity extends Activity implements OnPagerItemSelected { 

    void setUpPager() { 
     new ImageAdapterFromRes(this, ..., ..., ..., this); 
    } 

    void pagerItemSelected() { 
     finish(); 
    } 

} 
+0

괜찮습니다. – eurosecom

+1

MyActivity에서이 줄을 편집해야합니다. \t public void pagerItemSelected() { \t \t finish(); \t} – eurosecom

1

당신은 캐스트 활동을 완료 할 수 있습니다 :

((Activity) context).finish(); 
+0

감사가 잘 실행하지만 난 아마 다음 대답을 제안 :

이것은 당신의 활동 (또는 조각, 또는 다른 구성 요소)에이 작업을 수행 할 수 있다는 것을 의미합니다. – eurosecom