如何阻止SimpleDateFormat更新时间

时间:2016-08-18 20:39:07

标签: java android

我在How to get current timestamp in Android without it updating like a clock看到过这个问题,但是它没有被标记为已回答,我也不理解给出的一些建议。

无论如何,我已经超越了Java,并且正在学习制作一个简单的todo应用程序(https://guides.codepath.com/android/Basic-Todo-App-Tutorial)的教程,我决定尝试为每个列表项添加时间戳。我得到了它增加当前时间,但它不是静态的。我尝试添加新项目或者关闭并重新打开应用程序时,时间会不断更新。我无法在任何地方弄清楚/找到答案。

这就是我得到的: This is what I get

这就是我想要的: This is what I want

这是代码。 MainActivity.java

public class MainActivity extends AppCompatActivity {

private ArrayList<String> items;
private ArrayAdapter<String> TodoAdapter;
private ListView lvItems;

private void readItems() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, "todo.txt");
    try {
        items = new ArrayList<String>(FileUtils.readLines(todoFile));
    } catch (IOException e) {
        items = new ArrayList<String>();
    }
}

private void writeItems() {
    File filesDir = getFilesDir();
    File todoFile = new File(filesDir, "todo.txt");
    try {
        FileUtils.writeLines(todoFile, items);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

    items = new ArrayList<String>();
    readItems();
    TodoAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, android.R.id.text1, items);
    lvItems = (ListView) findViewById(R.id.lvItems);
    lvItems.setAdapter(TodoAdapter);
    // Setup remove listener method call
    setupListViewListener();
}

// Attaches a long click listener to the listview
// Removes item on long press
private void setupListViewListener() {
    lvItems.setOnItemLongClickListener(
            new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapter, View item, int pos, long id) {
                    // Remove the item within array at position
                    items.remove(pos);
                    // Refresh the adapter
                    TodoAdapter.notifyDataSetChanged();
                    writeItems();
                    // Return true consumes the long click event (marks it handled)
                    return true;
                }

            });
}

public void onAddItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    TodoAdapter.add(itemText); // Add items to new Adapter type
    etNewItem.setText("");
    writeItems();
}
}

TodoAdapter.java

public class TodoAdapter extends ArrayAdapter<Todo> {
    public TodoAdapter(Context context, ArrayList<Todo> items) {
        super(context, 0, items);
    }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Todo item = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
    }

    TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
    TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);

    text1.setText(item.itemText);
    text2.setText(item.getCreationTimeString());
    text2.setTextSize(10);

    return convertView;
    }
}

Todo.java

public class Todo {
    String itemText;
    private Date creationTime;

    public Todo(String text) {
        this.itemText = text;
        this.creationTime = new Date();
    }

    public String getCreationTimeString() {
        return new SimpleDateFormat("h:mm a").format(creationTime);
    }
}

非常感谢提供的任何帮助。另外请记住,我几乎不了解我在做什么。 :)

1 个答案:

答案 0 :(得分:0)

  

我尝试添加新项目时,时间会不断更新

是的,这是因为适配器正在调用new Date()总是获取为适配器项创建视图的当前时间。

看起来您希望项目与创建项目的时间相关联。如果是这种情况,那么你可以创建一个类

public class Todo {
   String itemText;
   private Date creationTime;

   public Todo(String text) {
      this.itemText = text;
      this.creationTime = new Date();
   }

   public String getCreationTimeString() {
       return new SimpleDateFormat("h:mm a").format(creationTime);
   }
}

然后,您应该make an ArrayAdapter<Todo>显示这些项目并显示如下内容

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Todo item = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
    }

    TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
    TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);

    text1.setText(item.itemText);
    text2.setText(item.getCreationTimeString());
    text2.setTextSize(10);

    return convertView;
}

并更新添加方法

public void onAddItem(View v) {
    EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
    String itemText = etNewItem.getText().toString();
    itemsAdapter.add(new Todo(itemText)); // Add items to new Adapter type
    etNewItem.setText("");
    writeItems();
}