Why is the dynamically generated ListView text gray?

I am working on an application for which I am creating a list of comments. The idea is that the user can add a comment and view them in a ListView. The problem is that the text color of the elements in the ListView is light gray (hard to read) instead of black if the application is not restarted with an existing list of comments. In other words, the text is gray only if comments are added dynamically. Do you guys know why this is happening? 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(); } }); 
+4
source share
2 answers

I had a similar problem. In my case, this was caused by using the application context instead of the activity context (which seems to me here as well - new ArrayAdapter<String>(getApplicationContext(),...); ). In my opinion, the correct color scheme is related to the ativity context, not the application context.

Hope this helps.

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

+4
source

I commented on some parts of your code that seemed a bit unnecessary. However, I'm not sure about the code associated with the Comment class. 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(); } }); 
+1
source

Source: https://habr.com/ru/post/1495181/


All Articles