如何动态地继续从单个EditText向自定义ArrayList添加数据

时间:2014-04-18 06:15:06

标签: java android arraylist

我在用户输入内容的片段中有一个EditText,OnClick将数据添加到自定义ArrayList中。然后是另一个按钮,它接收ListView片段,其中应列出所有数据,但只能看到用户输入的最新项目。

在按钮的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;


    }


}

更新 班级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 个答案:

答案 0 :(得分:1)

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

使该对象成为全局变量。在包含edittext的活动/片段中,将其分解为:

ArrayList<DiaryLogs> entryLogs;

并且

entryLogs = new ArrayList<DiaryLogs>();部分转移到-on活动或片段创建。

现在发生的事情是每次输入数据时都会创建entryLogs arrayList。所以你只能看到它的最后一个条目。

add()对象全局arrayList时,它会逐步添加每个对象。

对于您的实现,我认为myStringList应该初始化,填充方式与entryLogs列表相同。因此,将 ADDING 对象保留到myStringList而不是分配功能。

在这种情况下,改变必须如下:

myStringList = toStringList(entryLogs);  

需要:

myStringList.addAll(toStringList(entryLogs));