滚动后ListView崩溃

时间:2015-05-26 22:10:04

标签: android listview adapter android-arrayadapter

我实现了一个无尽的滚动ListView,它工作得很好但是在一对" couple"滚动,让我们说10,应用程序崩溃与此错误

     java.lang.OutOfMemoryError
        at java.util.ArrayList.addAll(ArrayList.java:194)
        at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:195)
        at com.bellantoni.chetta.lieme.ProfileFragment.onScroll(ProfileFragment.java:300)
        at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1755)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:6554)
        at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3664)
        at android.widget.AbsListView.onTouchEvent(AbsListView.java:4492)
        at android.view.View.dispatchTouchEvent(View.java:7690)

这是应用程序崩溃的代码部分

public void onScroll(AbsListView view,
                         int firstVisible, int visibleCount, int totalCount) {

        boolean loadMore = 
                firstVisible + visibleCount >= totalCount;

        if(loadMore) {
            this.adapter.setCount(this.adapter.getCount()+8);
            //NEXT LINE CRASHES
             this.adapter.addAll(this.rows);
             adapter.notifyDataSetChanged();
        }
    }

onScroll方法在创建和填充列表的片段中实现。变量rows是我要放入的List<>。我的适配器是这样的

public class CustomListAdapter extends ArrayAdapter<RowItemProfile> {

private final Activity context;
private List<RowItemProfile> rows;
private int count = 8;

public CustomListAdapter(Activity context, List<RowItemProfile> rows ) {
    super(context, R.layout.mylist, rows);

    this.context = context;
    this.rows = rows;
}



public View getView(int position, View view, ViewGroup parent) {


      LayoutInflater inflater = context.getLayoutInflater();
    View rowView = inflater.inflate(R.layout.mylist, null, true);
    rowView.setPadding(0,10,0,10);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.nameList);
    txtTitle.setTextColor(Color.BLACK);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.imgList);
    TextView extratxt = (TextView) rowView.findViewById(R.id.question);
    TextView idfacebook = (TextView) rowView.findViewById(R.id.facebookId);
    txtTitle.setText(this.rows.get(position).getNameSurname());
    imageView.setImageResource(this.rows.get(position).getIdImg());
    extratxt.setText(this.rows.get(position).getQuestion());

    idfacebook.setText(this.rows.get(position).getId());
    return rowView;

}

@Override
public int getCount() {
    return count;
}

public void setCount(int count) {
    this.count = count;
}
}

我无法弄明白为什么!

3 个答案:

答案 0 :(得分:1)

对象在每个项目的getview中创建,这可能会导致异常。对于100%有效的滚动和对象创建,请使用以下代码替换您的getview方法。

首先在LayoutInflater的构造函数中初始化CustomListAdapter对象 并在ViewHolder

中创建一个新的内部类CustomListAdapter

ViewHolder

class ViewHolder {
    TextView txtTitle,idfacebook,extratxt  ;
    ImageView imageView ;
}  

替换你的getview()方法
public View getView(int position, View convertView, ViewGroup parent) {
    // Avoid unneccessary calls to findViewById() on each row, which is expensive!
    ViewHolder holder;

    /* 
     * If convertView is not null, we can reuse it directly, no inflation required!
     * We only inflate a new View when the convertView is null.
     */
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_item_pocket, null);

        // Create a ViewHolder and store references to the two children views
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.nameList);
        holder.extratxt = (TextView) convertView.findViewById(R.id.question);
        holder.idfacebook = (TextView) convertView.findViewById(R.id.facebookId);
        holder. imageView = (ImageView) convertView.findViewById(R.id.imgList);

        // The tag can be any Object, this just happens to be the ViewHolder
        convertView.setTag(holder);
    } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
    }

    // Bind that data efficiently!

    holder.txtTitle.setTextColor(Color.BLACK);
    holder.txtTitle.setText(this.rows.get(position).getNameSurname());
    holder.imageView.setImageResource(this.rows.get(position).getIdImg());
    holder.extratxt.setText(this.rows.get(position).getQuestion());

    holder.idfacebook.setText(this.rows.get(position).getId());

    return convertView;
}

如果它无法解决您的问题,那么您在listview中使用大尺寸的图像,那么您需要调整图像大小。希望它有所帮助。

答案 1 :(得分:1)

尝试在getView()方法

之前添加这些方法
public int getCount() {
    // return the length or size of your List rows
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

答案 2 :(得分:0)

我找到了解决方案并将其发布在此处,也许对其他人有用。 我不知道为什么这两件事的组合解决了我的问题:

  1. 使用Picasso库下载图像
  2. 一次只添加一个元素而不是同时添加8个元素的列表(我认为最重要的一个)。