gpt4 book ai didi

Android ListView 在值上更改行颜色(不在选择上)

转载 作者:行者123 更新时间:2023-11-30 00:12:18 24 4
gpt4 key购买 nike

一切正常。即数据从 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();
}

最佳答案

多行出现红色的原因是没有else block 。添加一个 else block 将解决此问题。

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

关于Android ListView 在值上更改行颜色(不在选择上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47955199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com