私はコメントリストを作成しているアプリに取り組んでいます。アイデアは、ユーザーがコメントを追加して、それらを 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();
}
});