我有一个自定义列表视图,其中包含一个按钮以添加更多元素 但是,当我添加并添加应用程序崩溃元素时,但是当我重新启动时,我发现该元素已添加,(很少它不会崩溃)
我
我使用自定义适配器
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();
}
答案 0 :(得分:0)
如果您的ListView滞后,因为您使用wrap_content
作为listView&#39> 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
}