android - 为什么动态生成的ListView文字是灰色的?

标签 android listview text colors dynamically-generated

我正在开发一个应用程序,我正在为其构建评论列表。这个想法是用户可以添加评论,并在 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/

相关文章:

android - 使用首选项时不能将整数转换为 bool 值

android - 为自定义 ListView 创建搜索 View

android - ListView 在数据库更新和 adapter.notifyDataSetChanged() 后不更新;

C++ 程序在大型 .TXT 文件中缺少搜索字符串。适用于较小的 .TXT 文件

android - onreceive() 不适用于 Android O

android - Horizo​​ntal RecyclerView inside Vertical RecyclerView

android - 如何在没有完成加载上下文的情况下显示 toast

c - Win32 C++ ListView HDN_BEGINTRACK 不起作用

swift - 观察 UITextView 子类中的文本

HTML 辅助功能错误 - 标签文本为空?