在BaseExpandableListAdapter中使用getFragmentManager()

时间:2014-07-23 23:01:04

标签: android fragment expandablelistview alertdialog baseadapter

我正在创建一个自定义可扩展列表。在标题中我添加了一个ImageButton。我想在单击图像按钮时显示AlertDialog。为此,我创建了另一个扩展DialogFragment的类AlerttoB。一切正常,但是为了显示警报对话框,我使用了myAlert.show(getFragmentManager(),“我的警报”);这给出了一个错误:方法getFragmentManager()未定义类型new View.OnClickListener(){}

下面给出了代码,请有人告诉我们如何在此场景中显示警报对话框或在此处显示getfragmentManager()的任何方法。感谢。

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;


public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}



@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.lblListItem);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public Object getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);


    ImageButton buttonAdd = (ImageButton) convertView.findViewById(R.id.imageButtonAdd);
    buttonAdd.setFocusable(false);
    buttonAdd.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

            AlerttoB myAlert = new AlerttoB();
            myAlert.show(getFragmentManager(), "My Alert");






        }  
    });  

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

AlertoB类代码:

public class SmsBlackListAlertInput extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.blocksendersms, null);
builder.setView(view);
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(), "Negative Button was clicked", Toast.LENGTH_SHORT).show();
    }

});

builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        Toast.makeText(getActivity(), "Positive Button was clicked", Toast.LENGTH_SHORT).show();
    }

});

Dialog dialog = builder.create();
return dialog;

}

}

2 个答案:

答案 0 :(得分:0)

您已经在构造函数中存储了上下文,所以只需改为_context.getFragmentManager()

答案 1 :(得分:0)

这也是问题!所以我试试这个,它对我有用:

1-首先将yor声明更改为此表单:

    private FragmentManager FmBase;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
    HashMap<String, List<String>> listChildData , FragmentManager FmMain) 
    {
       this._context = context;
       this._listDataHeader = listDataHeader;
       this._listDataChild = listChildData;

       this.FmBase= FmMain;
    }

并在您的ImageButton onclick事件中使用此

    {
       myAlert.show(FtBase, "My Alert");
    }

2- for pass getFragmentManager()在制作适配器时尝试这个(For Exapmle):

    ExpandableList_Adapter listAdapter = new ExpandableList_Adapter(getActivity(), listDataHeader, listDataChild,getFragmentManager());