Java Android-ListView单元格在滚动ListView时添加了更多的ImageView
我有一个布局,它定义了单元格在ListView中的外观,但当我滚动时,单元格中唯一在滚动时发生变化的部分是位于LinearLayout视图中的ImageView 应该发生的是,在这个线性布局中应该只显示5颗星,但当我滚动时,我会在我的线性布局中看到大量的这些星星图像,而此时应该只显示5颗星 这是我的自定义阵列适配器:Java Android-ListView单元格在滚动ListView时添加了更多的ImageView,java,android,android-layout,Java,Android,Android Layout,我有一个布局,它定义了单元格在ListView中的外观,但当我滚动时,单元格中唯一在滚动时发生变化的部分是位于LinearLayout视图中的ImageView 应该发生的是,在这个线性布局中应该只显示5颗星,但当我滚动时,我会在我的线性布局中看到大量的这些星星图像,而此时应该只显示5颗星 这是我的自定义阵列适配器: public class ThreadArrayAdapter extends ArrayAdapter<ThreadObj> { Context
public class ThreadArrayAdapter extends ArrayAdapter<ThreadObj> {
Context context;
int layoutResourceId;
public ArrayList<ThreadObj> data = null;
public ThreadArrayAdapter(Context context, int layoutResourceId, ArrayList<ThreadObj> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
//WeatherHolder holder = null;
ThreadHolder t = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//TextView tv = (TextView)row.findViewById(R.id.threadTitle);
//t = new ThreadObj(tv.getText().toString());
t = new ThreadHolder();
//holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
t.threadUsernameTV = (TextView)row.findViewById(R.id.threadUsername);
t.threadDateTV = (TextView)row.findViewById(R.id.threadDate);
t.threadRatingIV = (ImageView)row.findViewById(R.id.threadRating);
t.threadTitleTV = (TextView)row.findViewById(R.id.threadTitle);
t.threadCategoryTV = (TextView)row.findViewById(R.id.threadCategory );
t.threadSubCategoryTV = (TextView)row.findViewById(R.id.threadSubCategory);
t.threadCountryTV = (TextView)row.findViewById(R.id.threadCountry);
t.threadStarLL = (LinearLayout)row.findViewById(R.id.threadStarContainer);
t.threadFlagIV = (ImageView)row.findViewById(R.id.threadFlag);
row.setTag(t);
}
else
{
t = (ThreadHolder)row.getTag();
}
ThreadObj thread = data.get(position);
t.threadUsernameTV.setText(thread.firstname + " " + thread.lastname);
t.threadDateTV.setText(thread.date);
t.threadTitleTV.setText(thread.title);
t.threadCategoryTV.setText(thread.category);
t.threadSubCategoryTV.setText(thread.subCategory);
t.threadCountryTV.setText(thread.country);
if(thread.ageRating == 4){
t.threadRatingIV.setImageResource(R.drawable.agerating17);
}
else if(thread.ageRating == 3){
t.threadRatingIV.setImageResource(R.drawable.agerating12);
}
else if(thread.ageRating == 2){
t.threadRatingIV.setImageResource(R.drawable.agerating7);
}
else{
t.threadRatingIV.setImageResource(R.drawable.ageratingpg);
}
//holder.imgIcon.setImageResource(weather.icon);
for(int i = 0; i < 5; i++){
ImageView star1 = new ImageView(this.context);
star1.setImageResource(R.drawable.star1);
ImageView star2 = new ImageView(this.context);
star2.setImageResource(R.drawable.star2);
if(i < thread.rating){
t.threadStarLL.addView(star2);
}else{
t.threadStarLL.addView(star1);
}
}
if(thread.flags > 0){
t.threadFlagIV.setImageResource(R.drawable.flag2);
}else{
t.threadFlagIV.setImageResource(R.drawable.flag1);
}
return row;
}
static class ThreadHolder
{
//ImageView imgIcon;
TextView threadUsernameTV;
TextView threadDateTV;
ImageView threadRatingIV;
TextView threadTitleTV;
TextView threadCategoryTV;
TextView threadSubCategoryTV;
TextView threadCountryTV;
LinearLayout threadStarLL;
ImageView threadFlagIV;
}
}
公共类ThreadArrayAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
公共ArrayList数据=null;
公共线程ArrayAdapter(上下文上下文、int-layoutResourceId、ArrayList数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
//WeatherHolder=null;
螺纹夹持器t=null;
if(行==null)
{
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
//TextView tv=(TextView)row.findViewById(R.id.threadTitle);
//t=newthreadobj(tv.getText().toString());
t=新螺纹固定器();
//holder.imgIcon=(ImageView)row.findViewById(R.id.imgIcon);
t、 threadUsernameTV=(TextView)row.findViewById(R.id.threadUsername);
t、 threadDateTV=(TextView)row.findViewById(R.id.threadDate);
t、 threadRatingIV=(ImageView)row.findViewById(R.id.threadRating);
t、 threadTitleTV=(TextView)row.findViewById(R.id.threadTitle);
t、 threadCategoryTV=(TextView)row.findViewById(R.id.threadCategory);
t、 threadSubCategory TV=(TextView)row.findViewById(R.id.threadSubCategory);
t、 threadCountryTV=(TextView)row.findViewById(R.id.threadCountry);
t、 threadStarLL=(LinearLayout)row.findViewById(R.id.threadStarContainer);
t、 threadFlagIV=(ImageView)row.findViewById(R.id.threadFlag);
row.setTag(t);
}
其他的
{
t=(ThreadHolder)row.getTag();
}
ThreadObj thread=data.get(位置);
t、 threadUsernameTV.setText(thread.firstname+“”+thread.lastname);
t、 threadDateTV.setText(thread.date);
t、 threadTitleTV.setText(thread.title);
t、 threadCategoryTV.setText(thread.category);
t、 threadsubcategory tv.setText(thread.subCategory);
t、 threadCountryTV.setText(thread.country);
if(thread.ageRating==4){
t、 threadRatingIV.setImageResource(R.drawable.agerating17);
}
else if(thread.ageRating==3){
t、 threadRatingIV.setImageResource(R.drawable.agerating12);
}
else if(thread.ageRating==2){
t、 threadRatingIV.setImageResource(R.drawable.agerating7);
}
否则{
t、 threadRatingIV.setImageResource(R.drawable.ageratingp);
}
//holder.imgIcon.setImageResource(weather.icon);
对于(int i=0;i<5;i++){
ImageView star1=新的ImageView(this.context);
star1.setImageResource(R.drawable.star1);
ImageView star2=新的ImageView(this.context);
star2.setImageResource(R.drawable.star2);
如果(i<螺纹额定值){
t、 threadStarLL.addView(star2);
}否则{
t、 threadStarLL.addView(star1);
}
}
如果(thread.flags>0){
t、 threadFlagIV.setImageResource(R.drawable.flag2);
}否则{
t、 threadFlagIV.setImageResource(R.drawable.flag1);
}
返回行;
}
静态级螺纹固定器
{
//ImageView imgIcon;
TextView线程用户名电视;
TextView-threadDateTV;
ImageView线程比率;
TextView-threadTitleTV;
TextView线程类别TV;
TextView线程子类别TV;
TextView电视;
线性布置螺纹标准;
ImageView-threadFlagIV;
}
}
您总是在创建新的ImageView
s并将其添加到t.threadStarLL
布局中--即使在重新使用现有视图时也是如此。解决此问题的一种方法是在添加新星号之前清除threadStarLL
布局
尝试在创建星号的for
循环之前添加此行:
t.threadStarLL.removeAllViews();