冲突support.v4.app.Fragment VS app.Fragment使用DatePickerDialog时 [英] Conflict support.v4.app.Fragment vs app.Fragment when using DatePickerDialog
问题描述
我真的不明白是怎么回事的support.v4库,但无论如何。我有一个片段,我实现单击导航抽屉中的项目(从机器人工作室的默认实现)时被打开。在这个片段,我想开一个datePickerDialog,但我找不到它的工作解决方案。下面是我的日期选择器片段:
I don't really understand what's going on with the support.v4 library but anyway. I have a Fragment that I implemented to be opened when clicking an item in the navigation drawer (default implementation from android studio). In this fragment, i'd like to open a datePickerDialog but I can't find the solution for it to work. Here is my fragment with the 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(),日期选择器);
My problem is in this line : newFragment.show(getSupportFragmentManager(), "datePicker");
我见过有一个兼容性的问题,如果该活动没有扩展片段,但我的活动已经扩展ActionBarActivity。我应该怎么办?
I've seen there is a compatibility problem if the activity doesn't extend Fragment but my activity is already extending ActionBarActivity. What should I do ?
推荐答案
没有你没有。你打错进口此处
No you have not. you have the wrong import here
import android.app.DialogFragment;
将其更改为使用支持库
change it to use the support library
这篇关于冲突support.v4.app.Fragment VS app.Fragment使用DatePickerDialog时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!