滚动listView时为什么项目会消失?

时间:2011-05-25 00:47:37

标签: android xml listview layout scrollview

我的应用程序使用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>

谢谢!

2 个答案:

答案 0 :(得分:46)

这是因为你有时会隐藏你的一些观点,而你再也不会展示它们了。例如:

        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)

答案 1 :(得分:0)

我遇到了同样的问题。

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

感谢您的解决方案!