我正在开发一个应用程序,我正在为其构建评论列表。这个想法是用户可以添加评论,并在 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();
}
});
最佳答案
我遇到了类似的问题。在我的例子中,它是由使用应用程序上下文而不是 Activity 上下文引起的(在我看来也是这里的情况 -- new ArrayAdapter<String>(getApplicationContext(),...);
)。在我看来,正确的配色方案与 Activity 上下文相关,而不是与应用程序上下文相关。
希望这对您有所帮助。
另见 Use android.R.layout.simple_list_item_1 with a light theme
关于android - 为什么动态生成的ListView文字是灰色的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18042599/