我真的不明白support.v4库正在发生什么,但无论如何。我有一个Fragment,我实现在单击导航抽屉中的项目时打开(android studio中的默认实现)。在这个片段中,我想打开一个datePickerDialog,但我无法找到解决方案。这是我的片段与datePicker:
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;
import java.util.Calendar;
public class SearchFragment extends Fragment {
private ListView listView;
private CustomAdapter activityAdapter;
private Button filterButton;
public SearchFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
activityAdapter = new CustomAdapter(getActivity());
listView = (ListView) rootView.findViewById(R.id.lvActivity);
listView.setAdapter(activityAdapter);
activityAdapter.loadObjects();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String activity = activityAdapter.getItem(position).get("activity").toString();
Toast.makeText(getActivity(), activity, Toast.LENGTH_SHORT).show();
}
});
filterButton = (Button) rootView.findViewById(R.id.bFilter);
return rootView;
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
}
我的问题在于这一行:newFragment.show(getSupportFragmentManager(),&#34; datePicker&#34;);
如果活动没有扩展Fragment但我的活动已经在扩展ActionBarActivity,我发现存在兼容性问题。我该怎么办?
答案 0 :(得分:3)
不,你没有。你在这里导入错误
import android.app.DialogFragment;
将其更改为使用支持库
答案 1 :(得分:1)
更改
import android.app.DialogFragment;
到
android.support.v4.app.DialogFragment
这将从支持包
导入DialogFragment类