4

I am working on an app for which I am building a comments list. The idea is that the user can add a comment, and review them in a ListView. The problem is that the text color of the items in the ListView is light grey (hard to read) instead of black, unless the app is re-launched with the list of comments already available. In other words, the text is grey only if the comments are added dynamically. Do you guys know why this happens? My code is as follows:

    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();
        }
    });
Zero
  • 1,864
  • 3
  • 21
  • 39
  • Set the text color by yourself. What is the problem? – EvZ Aug 04 '13 at 11:59
  • The text color is black which is what I want, unless the listview is updated dynamically. Then it's grey. That's the problem :) . – Zero Aug 04 '13 at 12:00
  • Why are you resetting the adapter every time someone adds a comment? You can just add the comment to the existing dataset and call `notifyDataSetChanged()` on the adapter. This may be the problem. – Ahmad Aug 04 '13 at 12:01
  • Works! Thank you :) . It was kind of silly to approach it like this. – Zero Aug 04 '13 at 12:07
  • Thanks so much for asking this question and for describing the question well so it has been answered. I found multiple other answers on SO but none that directly answered this. – raddevus Feb 18 '16 at 13:53

2 Answers2

3

I had a similar problem. In my case, it was caused by using the application context instead of the activity context (which seems to me to be the case also here -- new ArrayAdapter<String>(getApplicationContext(),...);). In my opinion, the proper color scheme is associated with the ativity context rather than with the application context.

Hope that this helps.

See also Use android.R.layout.simple_list_item_1 with a light theme

Community
  • 1
  • 1
Jaromír Adamec
  • 579
  • 4
  • 13
  • This is the exact solution to my problem. Thanks so much for answering this question and for describing the answer in detail. I found multiple other answers on SO but none that directly answered this. – raddevus Feb 18 '16 at 13:54
1

I commented out some parts of your code, that seemed a bit unnecessary. I am not sure about the code related to the Comment class though. In this context at least it seemed redundant.

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();

    }
});
Ahmad
  • 69,608
  • 17
  • 111
  • 137