Sectioned Listview内存问题

时间:2012-03-22 15:31:09

标签: android listview baseadapter

HI Guyz我创建了一个使用baseadapter的分段自定义列表视图,它的工作正常,但只有一个问题,当我再次滚动它时它会崩溃并给出内存异常。我很喜欢我的getview()代码plz帮助我。

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View row = convertView;

    Image cached = images[position];
    boolean flag = cached.sender_user;
    // System.out.println("Timeline: Position: "+position+", Length: "+data.length);
    // if(position!=data.length-1){
    System.out.println(position + " ===== " + images[position].pic_url);

    if (cached.sender_user == false) {
        System.out.println("If");

        row = vi.inflate(R.layout.list_row_layout_sender, null);
        txt_msg = (TextView) row.findViewById(R.id.txt_msg);
        txt_name = (TextView) row.findViewById(R.id.txt_name);
        txt_time = (TextView) row.findViewById(R.id.txt_time);
        img = (ImageView) row.findViewById(R.id.user_img);


        btn_loc=(Button)row.findViewById(R.id.btn_loc);
        btn_loc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("-------------- btn_loc ------------------- Sender");
                Developer.callIntent_userlocation(context, "18.50929796 ", "73.79045371");
            }
        });
        txt_name.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("----Name----");

                Developer.callIntent(context,images[position].id);
            }
        });
        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("----Image----");
                Developer.callIntent(context,images[position].id);
            }
        });
        txt_name.setText(cached.sender_name);
        txt_time.setText(cached.time);
        txt_msg.setText(cached.messege);
        if (position != 0) {
            cached = images[position - 1];
            if (cached.sender_user == flag) {
                img.setVisibility(ImageView.INVISIBLE);
                txt_name.setVisibility(TextView.INVISIBLE);
            } else
                imageLoader.DisplayImage(images[position].pic_url.trim()
                        .replace(" ", "%20"),
                        ReadMessageActivityNew.activity, img);
        } else
            imageLoader.DisplayImage(images[position].pic_url.trim()
                    .replace(" ", "%20"), ReadMessageActivityNew.activity,
                    img);
        System.out.println("Downloading=> for Reciver" + cached.pic_url);
        return row;
    }

    else {

        System.out.println("Else");

        row = vi.inflate(R.layout.list_row_layout_receiver, parent, false);
        txt_msg = (TextView) row.findViewById(R.id.txt_msg);
        txt_name = (TextView) row.findViewById(R.id.txt_name);
        txt_time = (TextView) row.findViewById(R.id.txt_time);
        img = (ImageView) row.findViewById(R.id.user_img);
        btn_loc=(Button)row.findViewById(R.id.btn_loc);

        btn_loc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("-------------- btn_loc ------------------- Reciever");


                Developer.callIntent_userlocation(context, "18.50929796 ", "73.79045371");
            }
        });
        txt_name.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Developer.callIntent(context, images[position].id);
            }
        });
        img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Developer.callIntent(context, images[position].id);
            }
        });
        txt_name.setText("Me");
        txt_time.setText(cached.time);
        txt_msg.setText(cached.messege);
        if (position != 0) {
            cached = images[position - 1];
            if (cached.sender_user == flag) {
                img.setVisibility(ImageView.INVISIBLE);
                txt_name.setVisibility(TextView.INVISIBLE);
            } else
                imageLoader.DisplayImage(images[position].pic_url.trim()
                        .replace(" ", "%20"),
                        ReadMessageActivityNew.activity, img);
        } else
            imageLoader.DisplayImage(images[position].pic_url.trim()
                    .replace(" ", "%20"), ReadMessageActivityNew.activity,
                    img);

        System.out.println("Downloading=> for User" + cached.pic_url);
        return row;
    }

}

1 个答案:

答案 0 :(得分:1)

问题在于,每次调用此方法时,您都要创建一个新视图。如果convertView为null,则只需要从布局中扩展视图。如果convertView不为null,则它是一个已被回收的视图供您使用。