我正在开发一个应用程序,我正在构建一个评论列表。这个想法是用户可以添加注释,并在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();
}
});
答案 0 :(得分:3)
我有类似的问题。在我的情况下,它是由使用应用程序上下文而不是活动上下文引起的(在我看来也是这种情况 - new ArrayAdapter<String>(getApplicationContext(),...);
)。在我看来,正确的配色方案与活动上下文相关联,而不是与应用程序上下文相关联。
希望这会有所帮助。
另见Use android.R.layout.simple_list_item_1 with a light theme
答案 1 :(得分:1)
我注释掉了代码的某些部分,这似乎有点不必要。我不确定与Comment
类相关的代码。在这种情况下,至少它似乎是多余的。
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);
// COMMENT: Is creating a comment object really neccessary, if it only serves the purpose of saving a text ?
Comment comment = new Comment();
comment.text = commentEditText.getText().toString();
// DrawView.comments.add(comment); COMMENT: -> Is this neccessary?
Toast.makeText(getApplicationContext(), "Comment saved", Toast.LENGTH_SHORT).show();
// commentsArrayList = new ArrayList<String>();
// for (Comment comment2 : DrawView.comments) {
commentsArrayList.add(comment.text);
// }
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, commentsArrayList);
// previousCommentsList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});