【问题标题】:Android ListView Change Row Color On Value (not on Select)Android ListView 在值上更改行颜色(不在选择上)
【发布时间】:2017-12-23 19:05:52
【问题描述】:

一切正常。即从 Firebase 读取数据并显示在 FirebaseListAdapter 中。

这是有问题的代码

        int urg = model.getUrgency();
        if(urg == 1) v.setBackgroundColor(Color.RED);

问题是它随机将更多的行染成红色。即对于我的测试,我只制作了 1 条记录 urg = 1。但是当我滚动时,超过 1 行变成红色。我该如何解决?

private void refreshListView(){

    fbAdapter = new FirebaseListAdapter<Post>(this, Post.class,R.layout.post_message, query) {
        @Override
        protected void populateView(View v, Post model, int position) {
            TextView messageText = (TextView)v.findViewById(R.id.message_text);
            TextView messageTime = (TextView)v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText());
            messageTime.setText(shortTime.format(model.getPostTimeStamp()));
            int urg = model.getUrgency();
            if(urg == 1) v.setBackgroundColor(Color.RED);
        }
    };

    mListView.setAdapter(fbAdapter);
    mListView.requestFocus();
}

【问题讨论】:

标签: android listview firebase


【解决方案1】:

多行红色的原因是,没有其他块。添加 else 块将解决此问题。

 int urg = model.getUrgency();
 if(urg == 1){
     v.setBackgroundColor(Color.RED);
 }else{
     v.setBackgroundColor(Color.WHITE);
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2011-09-28
    • 2023-04-06
    相关资源
    最近更新 更多