如何在android中的自定义listview中在所需位置显示ImageView

如何在android中的自定义listview中在所需位置显示ImageView,android,listview,android-listview,android-custom-view,listviewitem,Android,Listview,Android Listview,Android Custom View,Listviewitem,最近我制作了一个应用程序,可以在自定义列表视图中显示数据列表。 这个定制的listview将显示一个文本列表和一个图像视图,我实现了一个简单的逻辑,如果日期字符串与json中的日期字符串匹配,并且列表项的位置在0(第一个位置),那么将显示一个图标“new info”(新信息)。当日期字符串匹配并且位置在0位置时“新图标”被设置,我希望它只被设置在第一个位置,然而,“新图标”被设置在一个随机的位置,比如第9或第18个位置。我实现的逻辑是错误的吗?尽管这个逻辑很简单,但我开始忘记发生了什么,更多的是

最近我制作了一个应用程序,可以在自定义列表视图中显示数据列表。 这个定制的listview将显示一个文本列表和一个图像视图,我实现了一个简单的逻辑,如果日期字符串与json中的日期字符串匹配,并且列表项的位置在0(第一个位置),那么将显示一个图标“new info”(新信息)。当日期字符串匹配并且位置在0位置时“新图标”被设置,我希望它只被设置在第一个位置,然而,“新图标”被设置在一个随机的位置,比如第9或第18个位置。我实现的逻辑是错误的吗?尽管这个逻辑很简单,但我开始忘记发生了什么,更多的是“新图标”“当我向下滚动列表视图时开始显示。出于测试目的,我将Toast设置为查看items的位置,但是我看到即使if语句中的位置和日期不匹配,图标仍然会显示。 我已经在下面设置了示例代码

    //Compares the ArrayList<String> with the String and checks if position is in the first place

    if (lastdays.get(position).trim().toString().equals("2015/05/14")&&(position==0)) {

                    logicflag = true;
            //displays a "new icon" if the condition is true

                    holder.left.setBackgroundResource(R.drawable.blinker);
                    AnimationDrawable frameAnimation = (AnimationDrawable) holder.left
                            .getBackground();

                    // Start the animation (looped playback by default).
                    frameAnimation.start();

                }else {
//if false, displays nothing
                    holder.left.setImageResource(android.R.color.transparent);
                }
    //will display a "2nd and a 3rd new icon" to the 2nd & 3nd position if the logicflag = true
                if((position==1||position==2)&&logicflag){

                    holder.left.setImageResource(R.drawable.new_icon);

                }else{
                    // holder.left.setImageResource(android.R.color.transparent);
                }

//always keeps the last position with nothing
                if(position==getCount()-1){
                     holder.left.setImageResource(android.R.color.transparent);
                }

            }
//将ArrayList与字符串进行比较,并检查位置是否在第一位
if(lastdays.get(position.trim().toString().equals(“2015/05/14”)&&(position==0)){
logicflag=true;
//如果条件为真,则显示“新图标”
支架。左侧。收进地面资源(右侧可抽出式。闪烁灯);
AnimationDrawable frameAnimation=(AnimationDrawable)holder.left
.getBackground();
//启动动画(默认情况下为循环播放)。
frameAnimation.start();
}否则{
//如果为false,则不显示任何内容
holder.left.setImageResource(android.R.color.transparent);
}
//如果logicflag=true,将在第二和第三个位置显示“第二和第三个新图标”
if((位置==1 | |位置==2)和&logicflag){
holder.left.setImageResource(R.drawable.new_图标);
}否则{
//holder.left.setImageResource(android.R.color.transparent);
}
//始终保持最后一个位置,没有任何内容
if(position==getCount()-1){
holder.left.setImageResource(android.R.color.transparent);
}
}
毕竟,我想做的就是在第0个位置显示图像“newicon”,如果字符串匹配的话


PS:我已设置虚拟日期字符串来测试它,但失败。

您的
lastdays.get(position).trim().toString().equals(“2015/05/14”)
不满足。您应该使用有效的
SimpleDataFormat
,然后将该值与日期进行比较。实际上,为了显示一个示例,我写了“2015/05/14”,但实际上我比较的是我从日本时间转换的字符串,如Time
Time=new Time(“亚洲/东京”);time.setToNow();字符串今天=time.year+”年" + (时间月份+1)+”月“+time.monthDay+”日“;
。因此,我实际上是在比较字符串“2015”年05月14日"