Android 列表视图显示的项目不超过10个

Android 列表视图显示的项目不超过10个,android,Android,我试图在列表视图中显示一些数据。 每行包含一个图像、文本视图和一个复选框。 我创建了自己的适配器类:MyAdapter,它扩展了ArrayAdapter。 当传递给适配器的数组字符串长度超过10时,该列表根本不显示 以下是我设置适配器的方式: setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1, phonecontacts)); 这是我的适配器类: private class

我试图在列表视图中显示一些数据。 每行包含一个图像、文本视图和一个复选框。 我创建了自己的适配器类:MyAdapter,它扩展了ArrayAdapter。 当传递给适配器的数组字符串长度超过10时,该列表根本不显示

以下是我设置适配器的方式:

setListAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.textView1, phonecontacts));
这是我的适配器类:

 private class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int resource,
                int textViewResourceId, String[] phonecontacts) {
            super(context, resource, textViewResourceId, phonecontacts);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_item, parent, false);//layout , paremt , false
            ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
            TextView name = (TextView) row.findViewById(R.id.textView1);
            CheckBox checkbox = (CheckBox) row.findViewById(R.id.checkBox1);
            name.setText(phonecontacts[position]);
            iv.setImageResource(R.drawable.defaultcontact);
            return row;
        }

    }
私有类MyAdapter扩展了ArrayAdapter{
公共MyAdapter(上下文、int资源、,
int textViewResourceId,字符串[]电话联系人){
超级(上下文、资源、textViewResourceId、电话联系人);
//TODO自动生成的构造函数存根
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
视图行=充气器。充气(R.layout.list_项,父项,false);//布局,参数,false
ImageView iv=(ImageView)row.findViewById(R.id.imageView1);
TextView name=(TextView)row.findViewById(R.id.textView1);
复选框=(复选框)row.findViewById(R.id.checkBox1);
name.setText(电话联系人[位置]);
iv.setImageResource(R.drawable.defaultcontact);
返回行;
}
}

可能是内存问题,请按此处所述尝试使用:


在你的位置,我会使用电话联系人作为数组列表,而不是数组

每次调用getView()时,您都在膨胀视图

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = inflater.inflate(R.layout.list_item, parent, false);//layout , paremt , false
    ...
}
添加检查视图是否存在,如果为false,则充气

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_item, null);
    ....
}

getView
方法更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = null;
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =   inflater.inflate(R.layout.list_item, parent, false);//layout , paremt , false            
    }

    row = convertView;

    ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
    TextView name = (TextView) row.findViewById(R.id.textView1);
    CheckBox checkbox = (CheckBox) row.findViewById(R.id.checkBox1);
    name.setText(phonecontacts[position]);
    iv.setImageResource(R.drawable.defaultcontact);

    return row;
}

每次给新的一排充气是非常危险的。由于内存泄漏,它将导致崩溃。尝试重用这些行

如果您的问题是由于内存泄漏导致崩溃,那么您应该在适配器上重新使用您的行。使用viewholder会更好。你的应用程序崩溃了吗感谢你的解决方案,我想这是因为内存泄漏导致的崩溃,我没有更改代码,我重新启动了手机,手机现在可以正常工作了。