【问题标题】:Using getFragmentManager() in BaseExpandableListAdapter在 BaseExpandableListAdapter 中使用 getFragmentManager()
【发布时间】:2014-08-17 16:27:45
【问题描述】:

我正在创建一个自定义的可扩展列表。在标题中,我添加了一个 ImageButton。我想在单击图像按钮时显示一个 AlertDialog。为此,我创建了另一个扩展 DialogFragment 的类 AlerttoB。一切正常,但为了显示警报对话框,我使用了 myAlert.show(getFragmentManager(), "My Alert");这给出了一个错误:方法 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;

}

}

【问题讨论】:

    标签: android fragment expandablelistview android-alertdialog baseadapter


    【解决方案1】:

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

    【讨论】:

    • _context.getFragmentManager() 给出 Context 类型的方法 getFragmentManager() 未定义
    • 试试((Activity) _context).getFragmentManager()
    • 现在它给出了这个错误DialogFragment类型中的方法show(FragmentManager, String)不适用于参数(FragmentManager, String)是什么原因?
    • 啊,您可能需要支持片段管理器。试试((Activity) _context).getSupportFragmentManager()
    • 不,它也不起作用.. Activity 类型的方法 getSupportFragmentManager() 未定义您知道其他方法吗?
    【解决方案2】:

    好的

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

    1- 首先将您的声明更改为这种形式:

        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() 在制作适配器时试试这个(例如):

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

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多