2014-04-18 22 views
0

將數據添加到一個自定義的ArrayList我有一個EditText在用戶輸入的數據被添加到自定義的ArrayList東西和的OnClick的片段。然後是另一個按鈕,該按鈕將ListView片段應用於所有數據,但只能查看用戶輸入的最新項目。如何動態繼續從一個單一的EditText

準則按按鈕的OnClick添加數據,我覺得這是我做錯了

EditText timeText = (EditText) getView().findViewById(
         R.id.dateTimeEText); 

       EditText entryText = (EditText) getView().findViewById(
         R.id.diaryEntryEText); 
       String timeEntry = timeText.getText().toString(); 

       String entryEntered = entryText.getText().toString(); 

       ArrayList<DiaryLogs> entryLogs = new ArrayList<DiaryLogs>(); 

       DiaryLogs dl = new DiaryLogs(1, timeEntry, entryEntered); 
       entryLogs.add(dl); 

其他代碼如下

自定義對象類DiaryLogs

public class DiaryLogs { 

    //public static ArrayList<DiaryLogs> entryLogs; 

    String timeEntry, entryEntered; 
    int day; 

    // single constructor that takes an integer and two string 
    public DiaryLogs(int day, String timeEntry, String entryEntered) { 
     super(); 
     this.day = day; 
     this.timeEntry = timeEntry; 
     this.entryEntered = entryEntered; 

    } 

    public String getTimeEntry() { 
     return timeEntry; 
    } 

    public void setTimeEntry(String timeEntry) { 
     this.timeEntry = timeEntry; 
    } 

    public String getEntryEntered() { 
     return entryEntered; 
    } 

    public void setEntryEntered(String entryEntered) { 
     this.entryEntered = entryEntered; 
    } 

    public int getDay() { 
     return day; 
    } 

    public void setDay(int day) { 
     this.day = day; 
    } 

    @Override 
    public String toString() { 
     // TODO Auto-generated method stub 
     return this.timeEntry + "\n" + this.entryEntered; 


    } 


} 

UPDATE 類Monday_fragment

public class Monday_fragment extends Fragment { 

    public ArrayList<String> myStringList; 
    Bundle bundle; 
    ArrayList<DiaryLogs> entryLogs; 
    EditText timeText; 
    EditText entryText; 
    DiaryLogs dl; 
    String timeEntry; 
    String entryEntered; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     return inflater.inflate(R.layout.monday_fragment, container, false); 

    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     currentDateTime(); 
     super.onViewCreated(view, savedInstanceState); 

    } 

    public void currentDateTime() { 
     EditText timeText = (EditText) getView().findViewById(
       R.id.dateTimeEText); 
     SimpleDateFormat df = new SimpleDateFormat("d/M/yyyy:H:m"); 
     String dateTime = df.format(Calendar.getInstance().getTime()); 
     timeText.setText(dateTime); 
    } 

    public ArrayList<String> toStringList(Collection<DiaryLogs> entryLogs) { 
     ArrayList<String> stringList = new ArrayList<String>(); 

     for (DiaryLogs myobj : entryLogs) { 
      stringList.add(myobj.toString()); 
     } 

     return stringList; 
    } 

    @Override 
    public void onStart() { 
     entryLogs = new ArrayList<DiaryLogs>(); 
     timeText = (EditText) getView().findViewById(R.id.dateTimeEText); 

     entryText = (EditText) getView().findViewById(R.id.diaryEntryEText); 

     Button saveBtn = (Button) getView() 
       .findViewById(R.id.saveDiaryEntryBtn); 
     saveBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       timeEntry = timeText.getText().toString(); 

       entryEntered = entryText.getText().toString(); 

       dl = new DiaryLogs(1, timeEntry, entryEntered); 

       entryLogs.add(dl); 
       //convert entryLogs to string array list 
       myStringList = toStringList(entryLogs); 

       Toast.makeText(getActivity(), "Entry added \n" + dl, 
         Toast.LENGTH_SHORT).show(); 
         entryText.setText(""); 






      } 

     } 

     ); 
     System.out.println(entryLogs); 

     Button showBtn = (Button) getView().findViewById(
       R.id.showDiaryEntriesBtn); 
     showBtn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if (myStringList != null) { 
        bundle = new Bundle(); 
        FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction fragmentTransaction = fragmentManager 
          .beginTransaction(); 
        Monday_list_fragment mlf = new Monday_list_fragment(); 

        bundle.putStringArrayList("list", myStringList); 
        mlf.setArguments(bundle); 

        fragmentTransaction.replace(android.R.id.content, mlf); 
        fragmentTransaction.commit(); 
       } 
       if (myStringList == null) { 
        Toast.makeText(getActivity(), 
          "No entry have been added yet", Toast.LENGTH_SHORT) 
          .show(); 
       } 
      } 
     }); 

     super.onStart(); 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
    } 

} 

類Monday_list_fragment

 public class Monday_list_fragment extends ListFragment { 
    ArrayList<String> logs; 
    Bundle bundle; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater 
       .inflate(R.layout.monday_list_fragment, container, false); 

    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 

     super.onViewCreated(view, savedInstanceState); 
     bundle = this.getArguments(); 
     if (bundle != null) { 
      logs = bundle.getStringArrayList("list"); 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        getActivity(), android.R.layout.simple_list_item_1, logs); 
      setListAdapter(adapter); 

     } 

    } 

} 

回答

1
ArrayList<DiaryLogs> entryLogs = new ArrayList<DiaryLogs>(); 

創建該對象的全局變量。在與edittext活動/片段,分解成:

ArrayList<DiaryLogs> entryLogs; 

而且

entryLogs = new ArrayList<DiaryLogs>();部分-on活動或片段的創建。

眼下正在發生的事情是,entryLogs ArrayList中正在創建的每個輸入數據的時間。所以你只能看到最後一個條目。

當你add()對象爲全球ArrayList中,it'l逐步增加的每一個對象。

而且爲你實現,我覺得myStringList應該被初始化,填充以同樣的方式爲entryLogs列表。因此,保持ADDING對象myStringList,而不是分配功能。

在這種情況下,改變必須是這樣的:

myStringList = toStringList(entryLogs); 

需求是:

myStringList.addAll(toStringList(entryLogs)); 
+0

我已經改變了代碼檢查**更新**,但還是與替換最新的價值。 –

+0

是不是隻添加「d1」對象的代碼? [它通過與-每到toStringList,很好看即用,但你傳遞更多?] – user2450263

+0

是的,但我要的是對新的onClick創造新的DL和不斷增加的退出ArrayList的 那麼整個ArrayList使用方法toStringList轉換爲String Array,因爲我必須通過bundle將參數傳遞到Monday_list_fragment的ListView –

相關問題