4

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

2 に答える 2

3

同様の問題がありました。私の場合は、アクティビティ コンテキストの代わりにアプリケーション コンテキストを使用したことが原因でした (これは、ここでも当てはまるようです -- new ArrayAdapter<String>(getApplicationContext(),...);)。私の意見では、適切な配色は、アプリケーションのコンテキストではなく、アクティビティのコンテキストに関連付けられています。

これが役立つことを願っています。

ライトテーマで android.R.layout.simple_list_item_1 を使用するも参照してください。

于 2014-08-06T17:13:03.823 に答える
1

あなたのコードの一部をコメントアウトしましたが、それは少し不必要だと思われました。Commentただし、クラスに関連するコードについてはわかりません。この文脈では、少なくとも冗長に見えました。

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

    }
});
于 2013-08-04T12:14:47.003 に答える