0

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

    @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;
    }

}
4

1 回答 1

1

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

于 2012-03-22T15:35:36.553 回答