我有一个片段和另一个我从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 :(得分: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()