加previous元素得到替换的ArrayList [英] on add previous element getting replaced in ArrayList
问题描述
做调试。我可以后entryLogs.add(DL)清楚地看到;在entryLogs的值。第一数据被替换和第二已插入到ArrayList
正在发生的事情是我每次输入其添加到ArrayList中,但替换的第一项是相同第二个条目新的价值的时间。
例如:
我加入A的onclick它增加了A.
再次,加入B和的onclick它添加B.
但在该ArrayList的值是[17/4/2014:20:29 B,17/4/2014:20:29 B〕
请说明需要做什么。
整个code可以在这里找到:<一href=\"https://github.com/tirthoguha/DroidProject/blob/myDiary/src/com/example/s0217980_diary/Monday_fragment.java\" rel=\"nofollow\">https://github.com/tirthoguha/DroidProject/blob/myDiary/src/com/example/s0217980_diary/Monday_fragment.java
entryLogs =新的ArrayList&LT; DiaryLogs&GT;(); timeText =(EditText上)getView()findViewById(R.id.dateTimeEText)。 entryText =(EditText上)getView()findViewById(R.id.diaryEntryEText)。 按钮saveBtn =(按钮)getView()
.findViewById(R.id.saveDiaryEntryBtn);
saveBtn.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根 。timeEntry = timeText.getText()的toString(); 。entryEntered = entryText.getText()的toString(); DL =新DiaryLogs(1,timeEntry,entryEntered); entryLogs.add(分升);
在类 DiaryLogs.java
声明字符串timeEntry和entryEntered为静态
静态字符串timeEntry;
静态字符串entryEntered;
删除static修饰符,你会没事的。静态对象将有任何的类的实例相同的值(和可访问甚至不需要它的一个实例),所以如果你在另一个对象中设置的值,每一个实例都将使用该值。
did debugging. I can clearly see after entryLogs.add(dl); in the value of entryLogs. the first data is replaced and a second has been inserted to the ArrayList.
what is happening is every time i enter new value it adds it in the ArrayList but replaces the the first entry to be same as the second entry. for example: I add A and onclick it adds A. again, add B and onclick it add B. but in the Arraylist the value is [17/4/2014:20:29 B, 17/4/2014:20:29 B]
Please suggest what needs to be done. Entire code can be found here: https://github.com/tirthoguha/DroidProject/blob/myDiary/src/com/example/s0217980_diary/Monday_fragment.java
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);
In the class DiaryLogs.java
you declare the strings timeEntry and entryEntered as static
static String timeEntry;
static String entryEntered;
Remove the static modifier and you'll be fine. Static objects will have the same value for any instance of that class (and are accessible even without having an instance of it), so if you set the value in another object, every instance will use that value.
这篇关于加previous元素得到替换的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!