我正在开发一个我正在为其构建评论列表的应用程序。这个想法是用户可以添加评论,并在 ListView 中查看它们。问题是 ListView 中项目的文本颜色是浅灰色(难以阅读)而不是黑色,除非重新启动应用程序并使用已经可用的评论列表。换句话说,只有动态添加评论时,文本才会显示为灰色。你们知道为什么会这样吗?我的代码如下:
previousCommentsList = (ListView) findViewById(R.id.previous_comments_list);
commentsArrayList = new ArrayList<String>();
for (Comment comment : DrawView.comments) {
commentsArrayList.add(comment.text);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, commentsArrayList);
previousCommentsList.setAdapter(adapter);
saveCommentButton = (Button) findViewById(R.id.save_comment_button);
saveCommentButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText commentEditText = (EditText) findViewById(R.id.comment_edittext);
// Add the comment
Comment comment = new Comment();
comment.text = commentEditText.getText().toString();
DrawView.comments.add(comment);
Toast.makeText(getApplicationContext(), "Comment saved", Toast.LENGTH_SHORT).show();
commentsArrayList = new ArrayList<String>();
for (Comment comment2 : DrawView.comments) {
commentsArrayList.add(comment2.text);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, commentsArrayList);
previousCommentsList.setAdapter(adapter);
// Probably using both notifyDataSetChanged() and invalidate() is redundant
adapter.notifyDataSetChanged();
previousCommentsList.invalidate();
}
});