从Custom Aler Dialog类android获取片段中的Context

时间:2017-07-20 13:00:08

标签: android android-activity alertdialog android-alertdialog android-context

我正在编写一个Alert对话框,我希望在Fragment Class中使用它。我在这个类上实现了AdapterView.OnitemClickListener。我已经为标题,上下文,项目,请求代码,监听器声明了私有字段。我创建了一个构造函数。现在我想调用这个类,并希望为不同的目的设置不同类型的字符串。但是我收到了声明构造函数的错误。我想知道如何在片段类中调用此构造函数。我的Alert Dialog Class代码是

public class Alert extends Dialog implements AdapterView.OnItemClickListener{

public interface OnDialogItemClickListener {
    void onDialogItemClick(int requestCode, int position,String item);
}

private String title;
private Context context;
private String[] items;
private int requestcode;
private OnDialogItemClickListener listener;

public Alert(Context context, String title, String[] items, int requestcode, OnDialogItemClickListener listener) {
    super(context,R.style.DialogTheme);
    this.title = title;
    this.items = items;
    this.requestcode = requestcode;
    this.listener = listener;

}
private TextView textView;
private ListView listView;
private Button cancel;
private ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_alertlist_contact);
    textView = (TextView) findViewById(R.id.title);
    listView = (ListView) findViewById(R.id.listView1);
    cancel=(Button)findViewById(R.id.cancel_button);
    adapter = new ArrayAdapter<String>(context, R.layout.first_alertlist_textstyle,android.R.id.text1, items);
    textView.setText(title);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    listener.onDialogItemClick(requestcode,i, adapter.getItem(i));
    dismiss();
}

}

我的MainFragment类是

 public class MainFragment extends DialogFragment implements View.OnClickListener {
 private static final int REQUEST_CODE_SECOND = 2;
 private static final int REQUEST_CODE_FIRST = 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.fragment_main, container, false);

    Button button1 = (Button) view.findViewById(R.id.button1);
    button1.setOnClickListener(this);
    //registerForContextMenu(btn);
    Button button2 = (Button) view.findViewById(R.id.button2);
    button2.setOnClickListener(this);
    return view;

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button1:
            showFirstDialogwithList();
            break;
           .............
            break;
    }

}

private void showFirstDialogwithList() {
    String[] companyName = getResources().getStringArray(R.array.company_name);

 // I am getting error in this line
    Alert alert=new Alert(getActivity().getApplicationContext(),"Contact", companyName,REQUEST_CODE_FIRST,getActivity());
    alert.show();

4 个答案:

答案 0 :(得分:0)

使用片段活动上下文而不是getActivity()。getApplicationContext()。

@Override
    public void onAttach(Context context) {
        super.onAttach(context);

        Activity a;

        if (context instanceof Activity){
            a=(Activity) context;
        }

    }

答案 1 :(得分:0)

Alert alert=new Alert(getActivity().getApplicationContext(),"Contact", companyName,REQUEST_CODE_FIRST,**getActivity()**);

您将活动上下文作为侦听器的最后一个参数传递。

只需在此处传递this关键字,然后让您的片段实现该回调。

Alert alert=new Alert(getActivity().getApplicationContext(),"Contact", companyName,REQUEST_CODE_FIRST,this);

答案 2 :(得分:0)

试试这一行

Alert alert=new Alert(getView().getContext(),"Contact",companyName,REQUEST_CODE_FIRST,getActivity());
alert.show();

答案 3 :(得分:0)

在您的CoreApplication.java中的代码下编写

第1步) 公共类CoreApplication扩展了Application {

私有静态CoreApplication实例; .. }

第2步) onCreate(){ .... instance = this;

}

step3)添加此方法()

公共静态CoreApplication getGlobalApplicationContext(){

    if (instance == null) {
        throw new IllegalStateException("this application does not 

继承GlobalApplication”);         }

    return instance;

}

Step4) 在您的Fragment.class中调用此应用程序方法

AlertDialog.Builder警报=新的AlertDialog.Builder(getGlobalApplicationContext());