English 中文(简体)
为什么滚动列表时项目会消失查看?
原标题:Why do items disappear when I scroll the listView?

我的应用程序使用ListView,它在单元格中显示不同的项。它必须有照片和名字,但它碰巧有描述和图标。

问题是:当我滚动ListView并返回顶部时,描述和图标已经消失,只有图像和名称。我的印象是,描述和图标已被删除,每行的大小相同。

我使用自定义适配器(BaseAdapter)作为我的ListView

public class MyAdapterPark extends BaseAdapter {

    private ArrayList<DataPark> datapark;
    private LayoutInflater myInflater;

    public MyAdapterPark (Context context, ArrayList<DataPark> _datapark)
    {
        this.myInflater = LayoutInflater.from(context);
        this.datapark = _datapark;

    }


    @Override
    public int getCount() {
        return this.datapark.size();
    }

    @Override
    public Object getItem(int arg0) {
        return this.datapark.get(arg0);

    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    public static class ViewHolder {
        ImageView image;
        TextView text01;
        ImageView facebook;
        ImageView twitter;
        ImageView linkedin;
        ImageView blog;
        ImageView music;
        TextView bio;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if(convertView == null)
        {
            convertView = myInflater.inflate(R.layout.affichageitem, null);
            holder = new ViewHolder();
            holder.image = (ImageView)convertView.findViewById(R.id.img);
            holder.text01 = (TextView)convertView.findViewById(R.id.user);
            holder.facebook = (ImageView)convertView.findViewById(R.id.facebook);
            holder.twitter = (ImageView)convertView.findViewById(R.id.twitter);
            holder.linkedin = (ImageView)convertView.findViewById(R.id.linkedin);
            holder.blog = (ImageView)convertView.findViewById(R.id.blog);
            holder.music = (ImageView)convertView.findViewById(R.id.music);
            holder.bio = (TextView)convertView.findViewById(R.id.bio);



            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text01.setText(datapark.get(position).nickname);

        if (!(datapark.get(position).pic.equals(""))){
        try {
            //byte[] decodedString;
            //decodedString = Base64.decode(datapark.get(position).pic);
            //Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            //holder.image.setImageBitmap(bitmap);
            holder.image.setImageBitmap(telechargerImage(datapark.get(position).pic));



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }else{
            holder.image.setImageResource(R.drawable.profilbase);

        }

        if (!(datapark.get(position).facebook.equals(""))){

            holder.facebook.setImageResource(R.drawable.fb);

            }else{
                ((ImageView)convertView.findViewById(R.id.facebook)).setVisibility(View.GONE);
            }

        if (!(datapark.get(position).twitter.equals(""))){

            holder.twitter.setImageResource(R.drawable.twitter);

            }else{
                ((ImageView)convertView.findViewById(R.id.twitter)).setVisibility(View.GONE);
            }

        if (!(datapark.get(position).linkedin.equals(""))){

            holder.linkedin.setImageResource(R.drawable.linkedin);

            }else{
                ((ImageView)convertView.findViewById(R.id.linkedin)).setVisibility(View.GONE);
            }

        if (! (datapark.get(position).blog.equals(""))){

            holder.blog.setImageResource(R.drawable.blog);

            }else{
                ((ImageView)convertView.findViewById(R.id.blog)).setVisibility(View.GONE);
            }


        if (!(datapark.get(position).music.equals(""))){

            holder.music.setImageResource(R.drawable.music);

            }else{
                ((ImageView)convertView.findViewById(R.id.music)).setVisibility(View.GONE);
            }

        if (!(datapark.get(position).bio.equals(""))){          

            holder.bio.setText(datapark.get(position).bio);

            }else{
                ((TextView)convertView.findViewById(R.id.bio)).setVisibility(View.GONE);
            }




        return convertView;
    }

    public static Bitmap telechargerImage(String url) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("Erreur","Erreur lors de la récupération de l image : " + e.getMessage().toString());
        }
        return bm;
    }

}

以及我的适配器的XML:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#eaf4f0" 
    >

    <ImageView
        android:id="@+id/img"
        android:layout_width="70px"
        android:layout_height="70px"
        android:background="@drawable/carre"
        android:scaleType="fitXY"
        android:padding="5sp"
        android:layout_marginLeft="0sp"
        android:src="@drawable/profilbase"
        android:layout_margin="5sp"
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:paddingLeft="0sp"
        android:layout_weight="1"
        >

        <TextView android:id="@+id/user"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:textSize="14px"
             android:textColor="#000000"
             android:textStyle="bold"
             android:text=""    
             android:layout_marginTop="5sp"  
             />

             <TextView android:id="@+id/bio"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:textSize="14px"
             android:textColor="#000000"
             android:layout_marginTop="1sp"  
             android:layout_marginRight="2sp"   
             android:text=""      
             />

        <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4sp"
        android:layout_marginBottom="4sp"
        >
        <ImageView
        android:id="@+id/facebook"
        android:layout_width="17px"
        android:layout_height="17px"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        />
         <ImageView
        android:id="@+id/twitter"
        android:layout_width="17px"
        android:layout_height="17px"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        android:layout_marginLeft="5sp"
        />
         <ImageView
        android:id="@+id/linkedin"
        android:layout_width="17px"
        android:layout_height="17px"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        android:layout_marginLeft="5sp"
        />
         <ImageView
        android:id="@+id/blog"
        android:layout_width="17px"
        android:layout_height="17px"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        android:layout_marginLeft="5sp"
        />
         <ImageView
        android:id="@+id/music"
        android:layout_width="17px"
        android:layout_height="17px"
        android:scaleType="fitXY"
        android:layout_gravity="center"
        android:layout_marginLeft="5sp"
        />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

谢谢

最佳回答

这是因为你有时会隐藏自己的一些观点,然后再也不会表现出来。示例:

        if (!(datapark.get(position).bio.equals(""))){          
          holder.bio.setText(datapark.get(position).bio);
        }else{
          ((TextView)convertView.findViewById(R.id.bio)).setVisibility(View.GONE);
        }

请尝试:

        if (!(datapark.get(position).bio.equals(""))){ 
          holder.bio.setVisibility(View.VISIBLE);         
          holder.bio.setText(datapark.get(position).bio);
        }else{
          holder.bio.setVisibility(View.GONE);
        }

请记住,视图是可回收的,因此,除非下次返回视图时显示这些视图,否则它们将永远不会再显示。此外,请记住holder.bio与findViewById返回的内容相同,因此您可以在else块中执行holder.bio.setVisibility(View.GONE)

问题回答

我也有同样的问题。

        if (!imageuri.isEmpty()) {
            rl.setVisibility(View.VISIBLE);
            SmartImageView imageView = (SmartImageView) view.findViewById(R.id.detailimageView1);
            imageView.setImageUrl(imageuri);
        } else {            
            rl.setVisibility(View.GONE);
        }

感谢您的解决方案!





相关问题
Very basic WPF layout question

How do I get this listbox to be maxiumum size, i.e. fill the group box its in. I ve tried width="auto" Height="auto", width="stretch" Height="stretch" annoyingly at the moment, it dynamicly sizes to ...

How to align the text to top of TextView?

How to align the text to top of a TextView? Equivalent Android API for Swings setInsets()? that is top of text should start be in (0,0) of TextView <?xml version="1.0" encoding="utf-8"?> <...

Centring a flow in Shoes

Can I center the contents of a flow in Shoes? I know that a paragraph can be centred like: para Centred paragrpah , :align=> center However, this does not work with flows: flow(:align=> ...

Overlaying with CSS

I want to load a website in an iframe, and overlay the website with some hints about the website that is shown. However, i got stuck at the point that the website has a variable passive white space at ...

How do you normally make a program look beautiful? [closed]

How can I make an Application look nice and not like an amateur pulled it together? I mean graphic-wise. Is there some sort of book you can read regarding beautiful program layouts, etc? I put this ...

热门标签