【问题标题】:Listview lag when scrolling, and crash when add itemListview滚动时滞后,添加项目时崩溃
【发布时间】:2018-06-08 05:02:40
【问题描述】:

我有一个自定义列表视图,带有一个添加更多元素的按钮 但是当我添加和元素时应用程序崩溃,但是当我重新启动时我发现添加了元素,(很少它不会崩溃)

还有我

我使用自定义适配器

 class CustomAdapter extends BaseAdapter {

    ArrayList<ListItem> listItems = new ArrayList<ListItem>();

    CustomAdapter(ArrayList<ListItem> list){
        this.listItems = list;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position).name;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {

        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.list_item,null);

        TextView name = (TextView) view.findViewById(R.id.name);
        TextView lastm = (TextView) view.findViewById(R.id.last);
        TextView time = (TextView) view.findViewById(R.id.time);
        CircleImageView pic= (CircleImageView) view.findViewById(R.id.pic);

        name.setText(listItems.get(i).name);
        lastm.setText(listItems.get(i).lastm);
        time.setText(listItems.get(i).time);
        Bitmap bmp = BitmapFactory.decodeByteArray(listItems.get(i).pic,0,listItems.get(i).pic.length);
        pic.setImageBitmap(bmp);




        return view;
    }
}

当我添加一个元素时,应用程序崩溃

add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText editText=(EditText) mView.findViewById(R.id.name);
                    String name=editText.getText().toString();
                    boolean result=myDataBase.insertData(imageViewToByte(img),name,"no messages yet","");
                    if (result) {
                        Toast.makeText(Main2Activity.this, "Saved in DATABASE", Toast.LENGTH_SHORT).show();
                        viewLastData();
                        dialog.dismiss();
                    }

【问题讨论】:

  • "崩溃" ..;你有这种崩溃的堆栈跟踪吗?
  • 你能添加你的xml代码吗?
  • Stulktuske 我没听懂,“stacktrace?”
  • 表示错误日志
  • 我觉得你应该用 Picasso 来做图片,用 RecyclerView 来做列表。

标签: java android listview scroll crash


【解决方案1】:

如果您的 ListView 滞后,那是因为您使用 wrap_content 作为 listView 的 layout_height。它会强制您的 ListView 计算其中的所有项目,并且会对性能产生巨大影响。

所以将wrap_content 替换为match_parent 以避免这些滞后。

编辑:在 Adapter 中也使用 ViewHolder 模式,请参阅 this link

这是一个例子:

// ViewHolder Pattern
private static class ViewHolder {
    TextView name, status;
}

@Override @NonNull
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater vi = LayoutInflater.from(getContext());
        convertView = vi.inflate(R.layout.list_item, parent, false);

        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.text_name);
        holder.status = (TextView) convertView.findViewById(R.id.another_text);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    // Then do the other stuff with your item
    // to populate your listView
    // ...

    return convertView  
}

【讨论】:

  • 我把它改成了 match_parent 但我还是有同样的问题
猜你喜欢
  • 2016-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-31
相关资源
最近更新 更多