2014-03-07 66 views
2

我正在開發具有導航抽屜的應用程序。我的基本活動是儀表板活動。有一個導航列表。當我點擊導航列表中的項目時,它顯示了一些使用片段的接口(替換儀表板活動)。在AddPatient片段中,我顯示了form.I添加了一個Date_of_birth字段,當我單擊該日期時,文本)查看日期選擇器對話框應彈出(使背景透明但黑暗),採取輸入,顯示在片段上。使用警報對話框構建器的片段上的日期選擇器對話框

問題是我沒有找到如何顯示片段上的日期選擇器,基本活動(儀表板活動)擴展了片段活動和AddPatient片段擴展DailogFragment。代碼如下。我不知道我哪裏錯了。

對我的儀表盤活動的代碼是

public class DashboardActivity extends FragmentActivity { 
    Fragment fragment; 
private DrawerLayout mDrawerLayout; 
private ExpandableListView mDrawerList; 
private ArrayList<NavDrawerItem> navDrawerItems; 
private int groupP, childP; 
private SwipeListView swipeListView; 
List<CalenderDrawable> list; 
List<String> menu_items; 
Map<String, List<String>> childCollection; 
public static List<String> childList; 
public static List<String> childListForConnectedService; 

private ActionBarDrawerToggle mDrawerToggle; 
Session session; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 
    setContentView(R.layout.activity_dashboard); 
    session = new Session(this); 

    fragment = new DashboardFragment(); 
    getFragmentManager().beginTransaction() 
      .replace(R.id.frame_container, fragment).commit(); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ExpandableListView) findViewById(R.id.list_slidermenu); 

    createMenuItemList(); 
    createCollection(); 
    final ExpandableListAdapterNew adapter = new ExpandableListAdapterNew(
      DashboardActivity.this, menu_items, childCollection); 
    mDrawerList.setAdapter(adapter); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    // getActionBar().setHomeButtonEnabled(true); 
    getActionBar().setDisplayShowTitleEnabled(false); 
    getActionBar().setBackgroundDrawable(
      new ColorDrawable(Color.parseColor("#33ffffff"))); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, R.string.empty, R.string.empty) { 

     public void onDrawerClosed(View view) { 

      invalidateOptionsMenu(); // Setting, Refresh and Rate App 
     } 

     public void onDrawerOpened(View drawerView) { 

      invalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 
    mDrawerList.setOnGroupExpandListener(new OnGroupExpandListener() { 

     int previousGroup = -1; 

     @Override 
     public void onGroupExpand(int groupPosition) { 
      Toast.makeText(getApplicationContext(), 
        menu_items.get(groupPosition) + " Expanded", 
        Toast.LENGTH_SHORT).show(); 

      if (groupPosition != previousGroup) 
       mDrawerList.collapseGroup(previousGroup); 
      previousGroup = groupPosition; 

     } 
    }); 

    mDrawerList.setOnChildClickListener(new OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 
      // TODO Auto-generated method stub 
      Toast.makeText(
        getApplicationContext(), 
        menu_items.get(groupPosition) 
          + " : " 
          + childCollection.get(
            menu_items.get(groupPosition)).get(
            childPosition), Toast.LENGTH_SHORT) 
        .show(); 
      groupP = groupPosition; 
      childP = childPosition; 
      switch (groupPosition) { 
      case 0: 
       switch (childPosition) { 
       case 0: 
        Log.d("CASE Patient ", "Add Patient"); 
        fragment = new AddPatient(); 
        getFragmentManager().beginTransaction() 
          .replace(R.id.frame_container, fragment) 
          .commit(); 
        break; 
       case 1: 
        Log.d("CASE Patient ", "View Patient"); 
        fragment = new DashboardFragment(); 
        getFragmentManager().beginTransaction() 
          .replace(R.id.frame_container, fragment) 
          .commit(); 
        break; 
    default: 
       break; 
      } 

      mDrawerLayout.closeDrawer(mDrawerList); 
      return false; 
     } 
    }); 
} 

,代碼爲病人添加在Frament該日期選擇器是要顯示的這個..

public class AddPatient extends Fragment implements OnClickListener { 
View rootView; 
private EditText edtGender, edtUsername, edtMobNo, edtFname, edtLname; 
private static TextView edtDOB; 
private TextView Heading, textWelcm; 
private Button btnSubmit; 
Session session; 

public AddPatient() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    rootView = inflater.inflate(R.layout.addpatient, container, false); 
    init(); 
    Heading = (TextView) rootView.findViewById(R.id.textView1); 
    Heading.setText("Add Patient"); 
    session = new Session(getActivity()); 
    String userType = session.restoreName(Session.USERTPYE); 
    String userN = session.restoreName(Session.FULLNAME); 
    textWelcm = (TextView) rootView.findViewById(R.id.textWelcm); 
    if (userType.equals("D")) { 
     textWelcm 
       .setText(Html.fromHtml("<b>Welcome, </b>" + "Dr." + userN)); 
    } else if (userType.equals("O")) { 
     textWelcm.setText(Html 
       .fromHtml("<b>Welcome, </b>" + "Opt." + userN)); 
    } 

    return rootView; 
} 

private void init() { 
    // TODO Auto-generated method stub 
    edtUsername = (EditText) rootView.findViewById(R.id.edtUsername); 
    edtMobNo = (EditText) rootView.findViewById(R.id.edtMobNo); 
    edtFname = (EditText) rootView.findViewById(R.id.edtFname); 
    edtLname = (EditText) rootView.findViewById(R.id.edtLname); 
    edtDOB = (TextView) rootView.findViewById(R.id.edtDOB); 
    edtDOB.setOnClickListener(this); 
    edtGender = (EditText) rootView.findViewById(R.id.edtGender); 
    btnSubmit = (Button) rootView.findViewById(R.id.btnSubmit); 
    btnSubmit.setOnClickListener(this); 
} 
@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
     case R.id.edtDOB: 
     Log.d("showDatePickerDialogBox", "showDatePickerDialogBox"); 
     LayoutInflater inflater = (LayoutInflater) getActivity() 
       .getLayoutInflater(); 
     final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
       getActivity()); 
     View customView = inflater.inflate(R.layout.datepicker, null); 
     alertDialog.setView(customView); 
     final Calendar now = Calendar.getInstance(); 
     final DatePicker datePicker = (DatePicker) customView 
       .findViewById(R.id.dialog_datepicker); 
     final TextView dateTextView = (TextView) customView 
       .findViewById(R.id.dialog_dateview); 
     final SimpleDateFormat dateViewFormatter = new SimpleDateFormat(
       "EEEE, dd.MM.yyyy", Locale.ENGLISH); 
     final SimpleDateFormat formatter = new SimpleDateFormat(
       "dd.MM.yyyy", Locale.ENGLISH); 
     // Minimum date 
     Calendar minDate = Calendar.getInstance(); 
     try { 
      minDate.setTime(formatter.parse("12.12.2010")); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     datePicker.setMinDate(minDate.getTimeInMillis()); 
     // View settings 
     alertDialog.setTitle("Choose a date"); 
     Calendar choosenDate = Calendar.getInstance(); 
     int year = choosenDate.get(Calendar.YEAR); 
     int month = choosenDate.get(Calendar.MONTH); 
     int day = choosenDate.get(Calendar.DAY_OF_MONTH); 
     try { 
      Date choosenDateFromUI = formatter 
        .parse(edtDOB.getText().toString()); 
      choosenDate.setTime(choosenDateFromUI); 
      year = choosenDate.get(Calendar.YEAR); 
      month = choosenDate.get(Calendar.MONTH); 
      day = choosenDate.get(Calendar.DAY_OF_MONTH); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     Calendar dateToDisplay = Calendar.getInstance(); 
     dateToDisplay.set(year, month, day); 
     dateTextView.setText(dateViewFormatter.format(dateToDisplay 
       .getTime())); 
     // Buttons 
     alertDialog.setNegativeButton("Go to today", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         edtDOB.setText(formatter 
           .format(now.getTime())); 
         dialog.dismiss(); 
        } 
       }); 
     alertDialog.setPositiveButton("Choose", 
       new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Calendar choosen = Calendar.getInstance(); 
         choosen.set(datePicker.getYear(), 
           datePicker.getMonth(), 
           datePicker.getDayOfMonth()); 
         edtDOB.setText(dateViewFormatter.format(choosen 
             .getTime())); 
         dialog.dismiss(); 
        } 
       }); 
     final AlertDialog dialog = alertDialog.create(); 
     // Initialize datepicker in dialog datepicker 
     datePicker.init(year, month, day, 
       new DatePicker.OnDateChangedListener() { 
        public void onDateChanged(DatePicker view, int year, 
          int monthOfYear, int dayOfMonth) { 
         Calendar choosenDate = Calendar.getInstance(); 
         choosenDate.set(year, monthOfYear, dayOfMonth); 
         dateTextView.setText(dateViewFormatter 
           .format(choosenDate.getTime())); 
         if (choosenDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY 
           || now.compareTo(choosenDate) < 0) { 
          dateTextView.setTextColor(Color 
            .parseColor("#ff0000")); 
          ((Button) dialog 
            .getButton(AlertDialog.BUTTON_POSITIVE)) 
            .setEnabled(false); 
         } else { 
          dateTextView.setTextColor(Color 
            .parseColor("#000000")); 
          ((Button) dialog 
            .getButton(AlertDialog.BUTTON_POSITIVE)) 
            .setEnabled(true); 
         } 
        } 
       }); 
     // Finish 
     dialog.show(); 
    default: 
     break; 
    } 
} 

}

編輯: 找到解決方案www.open-sourced.de/show_article.php

回答

1

使用

DialogFragment picker = new AddPatient(); 
picker.show(getFragmentManager(), "datePicker"); 

你可以找到更多信息@

http://developer.android.com/reference/android/app/DialogFragment.html

例子:

How to transfer the formatted date string from my DatePickerFragment?

+0

它給錯誤...的方法秀(FragmentManager,字符串)在DialogFragment類型中不適用於參數(FragmentManager,String)。 – user2833621

+0

@ user2833621顯示您的導入DialogFragment – Raghunandan

+0

我已經添加了導入我編輯..當我使用FragmentTransaction ft = getSupportFragmentManager()。beginTransaction(); DialogFragment picker = new AddPatient(); picker.show(ft,「datePicker」); .........它顯示我儀表板活動上的dailog ..我想從儀表板活動中創建一個Add Patient Fragment ...然後顯示它的dailog .. – user2833621