Android 滚动后ListView崩溃
我实现了一个无止境的滚动列表视图,它工作得很好,但是在“两次”滚动之后,比如说10次,应用程序会因为这个错误而崩溃Android 滚动后ListView崩溃,android,listview,adapter,android-arrayadapter,Android,Listview,Adapter,Android Arrayadapter,我实现了一个无止境的滚动列表视图,它工作得很好,但是在“两次”滚动之后,比如说10次,应用程序会因为这个错误而崩溃 java.lang.OutOfMemoryError at java.util.ArrayList.addAll(ArrayList.java:194) at android.widget.ArrayAdapter.addAll(ArrayAdapter.java:195) at com.bellantoni.chetta.
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
是我想输入的内容的列表。我的适配器是这样的
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;
}
}
公共类CustomListAdapter扩展了ArrayAdapter{
私人最终活动背景;
私有列表行;
私有整数计数=8;
公共CustomListAdapter(活动上下文,列表行){
super(上下文,R.layout.mylist,行);
this.context=上下文;
this.rows=行;
}
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(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);
setText(this.rows.get(position.getNameSurname());
setImageResource(this.rows.get(position.getIdImg());
setText(this.rows.get(position.getQuestion());
idfacebook.setText(this.rows.get(position.getId());
返回行视图;
}
@凌驾
public int getCount(){
返回计数;
}
公共无效集合计数(整数计数){
this.count=计数;
}
}
我不明白为什么 正在getview中为每个项目创建对象,这可能会导致异常。为了实现100%的高效滚动和对象创建,请使用以下代码替换getview方法
首先初始化CustomListAdapter的构造函数中的LayoutInflater
对象
并在CustomListAdapter
观众
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中的图像,那么您需要调整图像大小。希望有帮助。尝试在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;
}
我找到了解决方案,我把它贴在这里,也许它会对其他人有用。
我不知道为什么,但这两件事的结合解决了mywind的问题:
使用毕加索图书馆下载图片
一次只添加一个元素,而不是同时添加8个元素的列表(我认为最重要的一个)
我不明白为什么有人在没有留下评论的情况下否决了这个问题。这个问题提供了很好的信息,对我来说,这似乎是一个完全合理的问题。仅仅因为有几行代码没有正确格式化!--“或者更好,只有一行代码没有格式化,只是多了一些空格!询问@Bella:显示getView(…)的代码”
方法可能会有所帮助。您是否在列表项中使用了位图?
这并不能解决问题,我还尝试使用small imagesTry从您的适配器重新定义getItem和getItemId方法。我只是想大声说出来,但绝望的时候需要绝望的措施。如果您想让我访问您的项目,这样我就可以ee发生了什么。一旦我找到问题,我会在这里发布解决方案。