2015-10-18 67 views
0

我想讓我的代碼通過意圖傳遞布爾額外,但我fail.I調試並觀察到我的活動「B」將EXTRA_IS_ADDED的值分配爲true,因爲我想但比我撥打startActivity(intent)時,在我的活動「A」中,我無法正確使用此數據來指定isAdded的值。我無法通過並檢索意圖布爾額外

這裏的活動時,我稱之爲:

package com.example.recepinanc.whenfreedeneme; 

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.widget.ListView; 
import android.widget.Toast; 

import java.util.ArrayList; 

/** 
* Created by recepinanc on 10/10/15. 
*/ 
public class WorksActivity extends AppCompatActivity { 

    private Boolean isAdded; 
    private ListView todoListView; 
    private ArrayList<Work> works; 
    private Context mContext; 

    private int year; 
    private int month; 
    private int day; 
    private int hour; 
    private int minute; 

    private String title; 
    private String category; 

    //TODO Add school social todo overall buttons. 


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

     Log.i("WorksActivity", "works activity'te"); 

     mContext = getApplicationContext(); 
     works = new ArrayList<Work>(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     isAdded = getIntent().getBooleanExtra(AddWorkActivity.EXTRA_IS_ADDED, false); 
     year = getIntent().getIntExtra(AddWorkActivity.EXTRA_YEAR, 0); 
     month = getIntent().getIntExtra(AddWorkActivity.EXTRA_MONTH, 0); 
     day = getIntent().getIntExtra(AddWorkActivity.EXTRA_DAY,0); 
     hour = getIntent().getIntExtra(AddWorkActivity.EXTRA_HOUR,0); 
     minute = getIntent().getIntExtra(AddWorkActivity.EXTRA_MINUTE,0); 

     title = getIntent().getStringExtra(AddWorkActivity.EXTRA_TITLE); 
     category = getIntent().getStringExtra(AddWorkActivity.EXTRA_CATEGORY); 


     //TODO isAdded True pass edilemiyor 
     Log.i("is_Added",isAdded.toString()); 
     if (isAdded) { 
      //TODO examine the true isAdded case. 
      Log.i("isAddedTrue",isAdded.toString()); 
      Work work = new Work(); 

      Log.i("isAddedTrue","work creationdan sonra"); 
      work.setmTitle(title); 
      work.setmCategory(category); 
      work.setmYear(year); 
      work.setmMonth(monthConverter(month)); 
      work.setmDay(day); 
      work.setmHour(hour); 
      work.setmMinute(minuteConverter(minute)); 

      Log.i("isAddedTrue", "work.setlerden sonra"); 
      //works.add(work); 
      updateArray(works, work); 

      Log.i("isAddedTrue", "work.array addden sonra"); 
      Toast.makeText(WorksActivity.this, "NEWBIE ADDED.", Toast.LENGTH_SHORT).show(); 

      isAdded = false; 

      Log.i("isAddedTrue", "work is added false setden sivvaliiiiiim sonra"); 
     } 

     Log.i("isAddedTrue", "listview found"); 
     todoListView = (ListView) findViewById(R.id.listView); 

     Log.i("isAddedTrue", "listview founddan sonra"); 
     todoListView.setAdapter(new WorkAdapter(getApplicationContext(), works)); 

     Log.i("isAddedTrue", "listAdapter setten sonra"); 
    } 

    public void updateArray(ArrayList<Work> works, Work work) { 

     //Add a new item add its date and update the array 
     works.add(work); 
     Toast.makeText(WorksActivity.this, "A new work is created! " + 
       "\n" + work.getmTitle() + 
       "\n" + work.getmCategory() + 
       "\n" + work.getmDay() + 
       "\n" + work.getmMonth() + 
       "\n" + work.getmYear() + 
       "\n" + work.getmHour() + 
       "\n" + work.getmMinute(), Toast.LENGTH_LONG).show(); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.add_action: 
       Intent intent = new Intent(WorksActivity.this, AddWorkActivity.class); 
       Log.i("WorksActivity", "Created intent"); 
       startActivity(intent); 
       Log.i("WorksActivity", "Started Activity"); 
       break; 
      default: 
       Log.e("WorksActivity", "INVALID BUTTON"); 
       break; 
     } 
     return true; 
    } 

    public String monthConverter(int month) { 
     switch (month + 1) { 
      case 1: 
       return "January"; 
      case 2: 
       return "February"; 
      case 3: 
       return "March"; 
      case 4: 
       return "April"; 
      case 5: 
       return "May"; 
      case 6: 
       return "June"; 
      case 7: 
       return "July"; 
      case 8: 
       return "August"; 
      case 9: 
       return "September"; 
      case 10: 
       return "October"; 
      case 11: 
       return "November"; 
      case 12: 
       return "December"; 
      default: 
       return "UNKNOWN"; 
     } 
    } 

    public String minuteConverter(int minute) { 
     if (minute/10 <= 0) 
      return "0" + minute; 
     else 
      return minute + ""; 
    } 

} 

這裏是我的第二個-B-活動:

package com.example.recepinanc.whenfreedeneme; 

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.TimePicker; 

import java.util.Calendar; 

/** 
* Created by recepinanc on 11/10/15. 
*/ 
public class AddWorkActivity extends AppCompatActivity { 

    public static final String EXTRA_TITLE = "title"; 
    public static final String EXTRA_CATEGORY = "category"; 
    public static final String EXTRA_YEAR = "year"; 
    public static final String EXTRA_MONTH = "month"; 
    public static final String EXTRA_DAY = "day"; 
    public static final String EXTRA_HOUR = "hour"; 
    public static final String EXTRA_MINUTE = "minute"; 
    public static final String EXTRA_IS_ADDED = "isAdded"; 

    static final int DATE_DIALOG_ID = 999; 
    static final int TIME_DIALOG_ID = 111; 

    private EditText title; 
    private RadioGroup categoryRadioGroup; 
    private TextView datePicker; 
    private TextView timePicker; 

    private int year; 
    private int month; 
    private int day; 

    private int hour; 
    private int minute; 
    private boolean is24HourView = true; 


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

     Log.i("AddWorkActivity","addwork'te"); 

     title = (EditText) findViewById(R.id.todoTitle_editText); 
     categoryRadioGroup = (RadioGroup) findViewById(R.id.category_radioGroup); 
     datePicker = (TextView) findViewById(R.id.dateSpinner_textView); 
     timePicker = (TextView) findViewById(R.id.timeSpinner_textView); 

     Calendar calendar = Calendar.getInstance(); 
     year = calendar.get(Calendar.YEAR); 
     month = calendar.get(Calendar.MONTH); 
     day = calendar.get(Calendar.DAY_OF_MONTH); 

     hour = calendar.get(Calendar.HOUR_OF_DAY); 
     minute = calendar.get(Calendar.MINUTE); 


     setDateOnView(); 

     datePicker.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //todo call the create dialog method here. 
       showDialog(DATE_DIALOG_ID); 
      } 
     }); 

     timePicker.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //todo call create dialog for time picker. 
       showDialog(TIME_DIALOG_ID); 
      } 
     }); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
      case DATE_DIALOG_ID: 
       //set date as the current date 
       return new DatePickerDialog(this, dateSetListener, year, month, day); 
      case TIME_DIALOG_ID: 
       //set time as the current time 
       return new TimePickerDialog(this, timeSetListener, hour, minute, is24HourView); 
     } 
     return null; 
    } 

    private DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() { 
     @Override 
     public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { 

      //assign selected values 
      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDay; 

      //update the textView to selected date 
      setDateAsText(datePicker); 
     } 

    }; 

    private TimePickerDialog.OnTimeSetListener timeSetListener = new TimePickerDialog.OnTimeSetListener() { 
     @Override 
     public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { 

      //assign the selected time 
      hour = selectedHour; 
      minute = selectedMinute; 

      setTimeAsText(timePicker); 
     } 
    }; 

    private void setDateOnView() { 

     setDateAsText(datePicker); 
     setTimeAsText(timePicker); 

    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // handle item selection 
     switch (item.getItemId()) { 
      case R.id.cancel_action: 
       //TODO Make this return to its activity 
       finish(); 
       break; 
      case R.id.save_action: 
       //TODO Get info and pass it to the activity's updateArrayMethodString category = ""; 
       String category = ""; 
       int checkedRadioID = categoryRadioGroup.getCheckedRadioButtonId(); 
       switch (checkedRadioID) { 
        case R.id.todo_radioButton: 
         category = "To-Do"; 
         break; 
        case R.id.school_radioButton: 
         category = "School"; 
         break; 
        case R.id.social_radioButton: 
         category = "Social"; 
         break; 
        default: 
         Log.e("WorksActivity", "Unexpected RadioButton ID"); 
         break; 
       } 

       //pass data via intent 
       Intent intent = new Intent(getBaseContext(), WorksActivity.class); 
       intent.putExtra(EXTRA_TITLE, title.getText().toString()); 
       intent.putExtra(EXTRA_CATEGORY, category); 
       intent.putExtra(EXTRA_HOUR, hour); 
       intent.putExtra(EXTRA_MINUTE, minute); 
       intent.putExtra(EXTRA_YEAR, year); 
       intent.putExtra(EXTRA_MONTH, month); 
       intent.putExtra(EXTRA_DAY, day); 
       intent.putExtra(EXTRA_IS_ADDED, true); 
       Log.i("Addworkacitivty", "add work activity intent"); 
       startActivity(intent); 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 

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

    public String monthConverter(int month) { 
     switch (month + 1) { 
      case 1: 
       return "JANUARY"; 
      case 2: 
       return "FEBRUARY"; 
      case 3: 
       return "MARCH"; 
      case 4: 
       return "APRIL"; 
      case 5: 
       return "MAYY"; 
      case 6: 
       return "JUNE"; 
      case 7: 
       return "JULY"; 
      case 8: 
       return "AUGUST"; 
      case 9: 
       return "SEPTEMBER"; 
      case 10: 
       return "OCTOBER"; 
      case 11: 
       return "NOVEMBER"; 
      case 12: 
       return "DECEMBER"; 
      default: 
       return "UNKNOWN"; 
     } 
    } 

    public String minuteConverter(int minute) { 
     if (minute/10 <= 0) 
      return "0" + minute; 
     else 
      return minute + ""; 
    } 

    public void setDateAsText(TextView datePicker) { 
     datePicker.setText(new StringBuilder().append(day).append(" ") 
       .append(monthConverter(month)).append(" ").append(year)); 
    } 

    public void setTimeAsText(TextView timePicker) { 
     timePicker.setText(new StringBuilder().append(hour).append(":").append(minuteConverter(minute))); 
    } 
} 

這裏是我的適配器:

package com.example.recepinanc.whenfreedeneme; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 

/** 
* Created by recepinanc on 11/10/15. 
*/ 
public class WorkAdapter extends BaseAdapter { 

    private Context mContext; 
    ArrayList<Work> works; 

    public WorkAdapter(Context applicationContext, ArrayList<Work> works) { 
     this.mContext = applicationContext; 
     this.works = works; 
    } 

    @Override 
    public int getCount() { 
     return works.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return works.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //Upgrade this to view holder. 
     View v; 

     if (convertView == null) { 

      LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = layoutInflater.inflate(R.layout.work_item_layout, parent, false); 

      TextView title = (TextView) v.findViewById(R.id.title_textView); 
      TextView category = (TextView) v.findViewById(R.id.category_textView); 
      TextView date = (TextView) v.findViewById(R.id.date_textView); 
      TextView time = (TextView) v.findViewById(R.id.time_textView); 

      Work work = works.get(position); 

      titleResizer(title, work.getmTitle()); 
      category.setText(work.getmCategory()); 
      date.setText(new StringBuilder().append(work.getmDay()).append(" ") 
        .append(work.getmMonth()).append(" ").append(work.getmYear())); 
      time.setText(new StringBuilder().append(work.getmHour()) 
        .append(":").append(work.getmMinute())); 
     } else { 
      v = convertView; 
     } 
     return v; 
    } 

    public void titleResizer(TextView title, String titleText){ 
     if (titleText.length() > 25) 
      title.setTextSize(12); 
     else if (titleText.length() > 20) 
      title.setTextSize(15); 
     else if (titleText.length() > 15) 
      title.setTextSize(18); 
     else if (titleText.length() > 10) 
      title.setTextSize(20); 
     else 
      title.setTextSize(25); 

     title.setText(titleText); 
    } 
} 
+1

'私人布爾isadded;'是布爾'而不是'布爾值',一些自動裝箱和拆箱可能會發生在這裏。我找不到其他任何東西,但可能值得嘗試將其更改爲'private boolean isAdded;'。 –

+0

感謝兄弟,它似乎解決了我的問題,但現在我與另一one.I更新我的代碼與其中的其他文件更新我的問題。我點擊工具欄中的「保存」按鈕後,我的ListView永遠不會得到更新,而不是第一個項目一直在更換,我不能得到其他附加。 – Recomer

+0

這是一個完全不同的問題。 AddWorkActivity需要將數據返回給第一次啓動AddWorkActivity的WorksActivity,而不是啓動新的WorksActivity。請參閱http://stackoverflow.com/a/5852965/3286819以獲取完整答案。 –

回答

1

注意getBoolean返回布爾值(原始類型),而不是布爾值。

從文檔:

公共布爾getBooleanExtra(字符串名稱,布爾默認值)

在API級別1檢索來自意圖擴展數據。

參數名稱所需項目的名稱。如果 給定名稱沒有存儲所需類型的值,則返回值 。返回以前用 putExtra()添加的項目的值,如果沒有找到,則返回默認值。請參見 putExtra(字符串,布爾)

變化

private Boolean isAdded;

private boolean isAdded; 
+0

感謝您的詳細解答,請檢查我的更新代碼。 – Recomer

1

我搬到我原來的註釋的答案,所以你可以標記了一個問題,解決了。 ;)

private Boolean isAdded;Boolean(object)而不是boolean(原始類型),因此可能會發生一些自動裝箱和拆箱操作。我不能找到其他任何東西,但它可能是值得嘗試將其更改爲private boolean isAdded;