2015-04-04 34 views
-1

我有一個片段和另一個我從ArrayAdapter類擴展的類。從ArrayAdapter擴展的類中的按鈕內使用片段的方法

現在我有一個按鈕裏面的類,我想使用片段的方法。

public class ProfileAdapter extends ArrayAdapter<Profile> { 
    private LayoutInflater inflater=null; 
    private Context mContext; 
    public ProfileAdapter(Context context) { 
     super(context, 0); 
     this.mContext=context; 
     inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view=convertView; 
     if(view==null){ 
      view = inflater.inflate(R.layout.profile_item, null); 
     } 

     ImageButton btnDel=(ImageButton) view.findViewById(R.id.btnDell); 
     final Profile currentObject = getItem(position); 
     final String type=currentObject.getType(); 

     btnDel.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       MainActivity.mViewPager.setCurrentItem(1); 
       AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
        builder.setMessage("do you want to delete profile") 
        .setCancelable(false) 
        .setTitle("** Delete Profile **") 
        .setPositiveButton("yes", 
         new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
         DbManager db=new DbManager(mContext); 
         int idd=Integer.parseInt(currentObject.getId()); 
          db.deleteProfile(idd); 
          DisplayLocation dL=new DisplayLocation() //this is Fragment 
;       dL.removeProximity(idd);// i want to access this method 



         } 
         }) 
        .setNegativeButton("Cancel", 
         new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
         } 
         }); 
        AlertDialog alert = builder.create(); 
        alert.show(); 
      } 
     }); 
     return view; 
    } 

回答

0

我在我的一個應用程序中遇到了類似的問題。以下是我使用的陣列適配器類中的代碼。露營類是一個片段。

您是否記得創建片段的靜態實例?

final Camping camping = new Camping(); 
btn = (ImageButton) otherView.findViewById(R.id.delete); 

      btn.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        camping.list = db.getAllItemsCamping(); 
        camping.adapt = new CustomAdapter1(getActivity(), R.layout.item_layout, camping.list); 
        camping.listItem = (ListView) rootView.findViewById(R.id.listview_camp); 
        camping.listItem.setAdapter(camping.adapt); 
        camping.nothingtext = (TextView)rootView.findViewById(R.id.nothing_here_camp); 
        camping.count = camping.listItem.getCount(); 
        if (camping.count > 0){ 
         camping.nothingtext.setVisibility(View.GONE); 
        } 
        else if (camping.count == 0){ 
         camping.nothingtext.setVisibility(View.VISIBLE); 
        } 
        Log.i("UpDateDataBase()", "Was Called"); 
       } 
      }); 

而且,你似乎在這裏失去了分號DisplayLocation dL=new DisplayLocation()