2015-09-12 83 views
3

改變佈局我的菜單知名度這是我的菜單佈局之際...我需要顯示此刪除在一定條件下的圖標....我想根據條件

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="ringee.app.com.ringeeapp.Occasion"> 

    <item 
     android:id="@+id/action_settings" 
     android:title="@string/action_settings" 
     android:orderInCategory="100" 
     app:showAsAction="never" /> 

    <item 
     android:id="@+id/menu_cancel_event" 
     android:icon="@drawable/delete_icon" 
     android:title="Cancel event" 
     app:showAsAction="ifRoom"/> 
</menu> 

是否有可能請在此處更改此delete_icon的可見性...請幫助我找出代碼

這是我的場合活動代碼....此處刪除圖標顯示時添加事件以及更新事件..在這裏我需要只有當事件被更新可見的刪除圖標...我不需要顯示,雖然添加事件....

public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present.  
      this.menu=menu; 
      getMenuInflater().inflate(R.menu.menu_occasion, menu); 
      hide(); 
      show(); 
      return true; 
     } 
public boolean show() 
{ 
    menu.findItem(R.id.menu_cancel_event).setVisible(true); 
     return true; 
    } 
public boolean hide(){ 
     menu.findItem(R.id.menu_cancel_event).setVisible(false); 
     return false; 
    } 

     // delete the selected event from event list added here 
     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 

      switch (item.getItemId()) { 
       case R.id.menu_cancel_event: 
        //Setting the value for deleted event 
        eventMO.setIsDelete(1); 
        del(); 
        //Triggering the Create event 
        doOnCLickAndOnOptionSelected(); 
        return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 

     private void del() { 
      //Deleting the event from SQLLite 
      SQLiteDatabase db = dbHelper.getWritableDatabase(); 
      final long Id = eventMO.getEventId(); 
      db.delete("event", "Event_ID" + " = ?", new String[]{String.valueOf(Id)}); 
      db.close(); 
     } 

     // Common for delete event and save event(Create button click listener) 
     private void doOnCLickAndOnOptionSelected() { 
      //We need create button click listener for both delete as well as save the event.so we have this common method.we call this method from delete event and save event 
      prgDialog = new ProgressDialog(OccasionActivity.this); 
      // Set Progress Dialog Text 
      prgDialog.setMessage("Please wait..."); 
      // Set Cancelable as False 
      prgDialog.setCancelable(false); 

      prgDialog.show(); 
      UserMO userMO = dbHelper.getRingeeUserData(1); 
      eventMO.setText(custom_Text.getText().toString()); 
      eventMO.setPlace(event_Place.getText().toString()); 
      eventMO.setEndTime(end_Time); 
      eventMO.setStartTime(start_Time); 

      try { 
       //date and time format changed here 
       String eventDate = btn_Date.getText().toString(); 
       DateFormat date = new SimpleDateFormat("dd-M-yyyy"); 
       Date date1 = date.parse(eventDate); 
       DateFormat convertDate = new SimpleDateFormat(" yyyy-MM-dd hh:mm:ss"); 
       eventDate = convertDate.format(date1); 
       eventMO.setEventDate(eventDate); 

      } catch (ParseException e) { 

       e.printStackTrace(); 
      } 
      //eventMO.setEventDate(btn_Date.getText().toString()); 

      eventMO.setRingeeUserId(userMO.getRingeeUserId()); 
      //event update functionality added here 


      final Long hidden_Text2 = eventId2; 
      if ((eventMO.getText() != null) && (eventMO.getPlace() != null) && (eventMO.getEventDate() != null)) { 
       if (hidden_Text2 != null) { 
        eventMO.setEventId(hidden_Text2); 
        new AsyncTask<Void, Void, String>() { 
         @Override 
         protected String doInBackground(Void... arg0) { 
          return eventDelegates.updateEvent(eventMO, context); 
         } 

         @Override 
         protected void onPostExecute(String arg0) { 
          prgDialog.dismiss(); 
          Intent contactAct = new Intent(getApplicationContext(), UserDashBoardActivity.class); 
          // Clears History of Activity 
          contactAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(contactAct); 

         } 

        }.execute(null, null, null); 
        Toast.makeText(getApplicationContext(), "Details updated successfully", Toast.LENGTH_LONG).show(); 

       } else { 

        new AsyncTask<Void, Void, String>() { 
         @Override 
         protected String doInBackground(Void... arg0) { 
          return eventDelegates.addEvent(eventMO, context); 
         } 

         @Override 
         protected void onPostExecute(String eventId) { 
          prgDialog.dismiss(); 
          SharedPreferences.Editor editor = sharedpreferences.edit(); 
          editor.putString("eventId", eventId); 
          editor.commit(); 
          Intent contactAct = new Intent(getApplicationContext(), ContactActivity.class); 
          // Clears History of Activity 
          contactAct.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(contactAct); 
         } 
        }.execute(null, null, null); 
        Toast.makeText(getApplicationContext(), "Details added successfully", Toast.LENGTH_LONG).show(); 

       } 
      } else { 
       prgDialog.dismiss(); 
       Toast.makeText(context, "Please check Event custom text or Event place or Event date field", Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_occasion); 

      context = getApplicationContext(); 

      seekBar_startTime = (SeekBar) findViewById(R.id.seekBar1); 
      seekBar_startTime.setProgress(0); 
      seekBar_startTime.setMax(1440); //minutes in 24 hours(24*60) 
      seekBar_startTime.setProgress(1); 
      textView_startTime = (TextView) findViewById(R.id.textView1); 

      seekBar_endTime = (SeekBar) findViewById(R.id.seekBar2); 
      seekBar_endTime.setProgress(0); 
      seekBar_endTime.setMax(1440); //minutes in 24 hours(24*60) 
      seekBar_endTime.setProgress(1); 
      textView_endTime = (TextView) findViewById(R.id.textView2); 

      btn_Date = (Button) findViewById(R.id.event_date_button); 
      event_Place = (TextView) findViewById(R.id.Enter_Place); 
      custom_Text = (TextView) findViewById(R.id.Custom_Text); 
      checkBox_onEventDay=(CheckBox) findViewById(R.id.cb_onEventDay); 
      checkBox_2DaysBefore=(CheckBox) findViewById(R.id.cb_2DaysBefore); 
      checkBox_aWeekBefore=(CheckBox) findViewById(R.id.cb_aWeekBefore); 

      btn_create_button = (Button) findViewById(R.id.create_button); 
      sharedpreferences = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); 
      dbHelper = new DatabaseHelper(context); 



      btn_Date.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        DatePickerFragment newFragment = new DatePickerFragment(); 
        newFragment.show(getFragmentManager(), "Occasion Date"); 

       } 
      }); 

      seekBar_startTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 


       @Override 
       public void onStopTrackingTouch(SeekBar seekBar) { 
       } 

       @Override 
       public void onStartTrackingTouch(SeekBar seekBar) { 
       } 

       @Override 
       public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) { 
        int minutes; 
        int hours; 
        minutes =progressValue % 60; 
        hours = progressValue/60; 
        int Hours=hours; 
        String am_pm; 
        if (hours==24) 
        { 
         hours = hours-12; 
         am_pm="AM"; 
        } 
        else if 
          (hours> 12) 
        { 
         hours= hours- 12; 
         am_pm = "PM"; 
        } 
        else if (hours ==0) { 

         hours =hours+12; 
         am_pm = "AM"; 
        } 
        else if (hours <12) { 
         am_pm = "AM"; 
        } 
        else 
        { 
         am_pm="PM"; 
        } 

        if(minutes < 10) 
        { 
         //It shows 12 hours format with AM/PM to user 
         start_Time = String.valueOf(" "+hours+":0"+minutes+am_pm); 
         textView_startTime.setText("Event Start Time :" + start_Time); 
         //It pass 24hours format to database 
         start_Time = String.valueOf(Hours+":0"+minutes); 
        } 

        else { 
         //It shows 12 hours format with AM/PM to user 
         start_Time = String.valueOf(" "+hours+":"+minutes+am_pm); 
         textView_startTime.setText("Event Start Time :" + start_Time); 
         //It pass 24hours format to database 
         start_Time = String.valueOf(Hours+":"+minutes); 
        } 


       } 

      }); 


      seekBar_endTime.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 
       @Override 
       public void onStopTrackingTouch(SeekBar seekBar) { 
       } 

       @Override 
       public void onStartTrackingTouch(SeekBar seekBar) { 
       } 

       @Override 
       public void onProgressChanged(SeekBar seekBar, int progressValue,boolean fromUser) { 
        int minutes; 
        int hours; 
        minutes = progressValue % 60; 
        hours = progressValue/60; 
        int Hours=hours; 
        String am_pm; 
        if (hours==24) 
        { 
         hours = hours-12; 
         am_pm="AM"; 
        } 
        else if(hours> 12) 
        { 
         hours= hours- 12; 
         am_pm = "PM"; 
        } 
        else if (hours ==0) { 

         hours =hours+12; 
         am_pm = "AM"; 
        } 
        else if (hours <12) { 
         am_pm = "AM"; 
        } 
        else 
        { 
         am_pm="PM"; 
        } 
        if(minutes < 10) 
        { 
         //It shows 12 hours format with AM/PM to user 
         end_Time = String.valueOf(""+hours+":0"+minutes+am_pm); 
         textView_endTime.setText("Event Start Time :" + end_Time); 
         //It pass 24hours format to database 
         end_Time = String.valueOf(Hours+":0"+minutes); 
        } 

        else { 
         //It shows 12 hours format with AM/PM to user 
         end_Time = String.valueOf(""+hours+":"+minutes+am_pm); 
         textView_endTime.setText("Event Start Time :" + end_Time); 
         //It pass 24hours format to database 
         end_Time = String.valueOf(Hours+":"+minutes); 
        } 
       } 
      }); 

      //eventid get from OccasionFragment here 
      Bundle bundle = this.getIntent().getExtras(); 
      if (bundle != null) { 

       Long eventId1 = bundle.getLong("EventID"); 
       eventId2 = eventId1; 
       String eventName = bundle.getString("EventName"); 
       custom_Text.setText(eventName); 
       String eventPlace = bundle.getString("EventPlace"); 
       event_Place.setText(eventPlace); 
       String eventDate = bundle.getString("EventDate"); 
       try { 
        //String eventDate = bundle.getString("EventDate"); 
        //time string removed from date here 
        String result = eventDate.split(" ")[0]; 
        DateFormat sourceDate = new SimpleDateFormat("yyyy-MM-dd"); 
        // parse the date string into Date object 
        Date date = sourceDate.parse(result); 
        DateFormat convertDate = new SimpleDateFormat("dd-M-yyyy"); 
        // format the date into another format 
        result = convertDate.format(date); 
        btn_Date.setText(result); 


       } catch (ParseException e) { 

        e.printStackTrace(); 
       } 
       //Event Start Time and Event End Time string added before the time here 
       String eventStart = bundle.getString("EventStart"); 
       textView_startTime.setText("Event Start Time :" + eventStart); 
       String eventEnd = bundle.getString("EventEnd"); 
       textView_endTime.setText("Event End Time :" + eventEnd); 

      } 
      checkBox_onEventDay.setOnClickListener(new View.OnClickListener(){ 

       public void onClick(View v) { 
        if (((CheckBox) v).isChecked()) { 
         Toast.makeText(OccasionActivity.this, 
           "Checked", Toast.LENGTH_LONG).show(); 
        }} 
      }); 
      checkBox_2DaysBefore.setOnClickListener(new View.OnClickListener(){ 

       public void onClick(View v) { 
        if (((CheckBox) v).isChecked()) { 
         Toast.makeText(OccasionActivity.this, 
           "Checked", Toast.LENGTH_LONG).show(); 
        }} 
      }); 
      checkBox_aWeekBefore.setOnClickListener(new View.OnClickListener(){ 

       public void onClick(View v) { 
        if (((CheckBox) v).isChecked()) { 
         Toast.makeText(OccasionActivity.this, 
           "Checked", Toast.LENGTH_LONG).show(); 
        }} 
      }); 

      btn_create_button.setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        doOnCLickAndOnOptionSelected(); 

       } 
      }); 


     } 

     @SuppressLint("ValidFragment") 
     public 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) { 
       Date = new StringBuilder().append(year).append("-").append(month + 1).append("-").append(day).append(" ").toString(); 
       btn_Date.setText(new StringBuilder().append(day).append("-").append(month + 1).append("-").append(year).append(" ").toString()); 
      } 
     } 

    } 

在這裏,我只需要顯示的刪除圖標時的細節更新.....我需要隱藏的圖標,同時創建事件...創建一個事件代碼在「上創建保護無效」。 ...如果我需要添加這種隱藏方法......我想這該創建方法裏面,但它顯示了一個錯誤......請幫我

回答

1

嘗試下面的代碼,

public class MainActivity extends Activity { 
Menu menu; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    this.menu = menu; 
    getMenuInflater().inflate(R.menu.menu_act_app_home_new, menu); 
    return true; 
} 

void show(){ 
    menu.findItem(R.id.menu_cancel_event).setVisible(true); 
} 

void hide(){ 
    menu.findItem(R.id.menu_cancel_event).setVisible(false); 
} 
} 

讓我知道如果這對你的作品...

並將其標記爲一個答案所以這將是對別人有用

+0

感謝烏爾Ĵ...我將檢查並告訴你 –

+0

只是複製粘貼到您的活動 –

+0

請幫我... –

0

一個菜單項是不是正規的觀​​點,即是你的佈局的一部分。它的特殊之處,完全不同。你的代碼返回null的項目,這是導致崩潰。你所需的是要做到:

MenuItem item = menu.findItem(R.id.addAction); 

然後得到菜單項指向這樣的項目,叫調用setVisible()就可以了調整其知名度,然後調用

invalidateOptionsMenu() 

您活動,以便相應地調整ActionBar菜單。根據您的病情

boolean eventUpdated = true/false; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_occasion, menu); 
    return true; 
} 

,並設置可見性:

+0

顯示錯誤,同時增加這對我onoptionsitem選擇 –

+0

你必須把這個在onCreateOptionsMenu(),而不是onOptionsItemSelected(),因爲onCreateOptionsMenu()是你創建的菜單選項。 –

+0

現在,它顯示了烏爾應用程序停止了工作......就像在創建活動 –

0

只需創建你的菜單

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    menu.getItem(R.id.menu_cancel_event).setVisible(eventUpdated); 
    return true; 
} 

呼叫invalidateOptionsMenu()每次病情變化刷新菜單。

+0

如何調用該onPrepareOptionsMenu –

+0

幫我........ –

+0

這是寫在那裏。調用'Activity'的方法'invalidateOptionsMenu()'。 – skywall