Listview包含图片和两个textview。如果没有要打印或显示的对象,我第一次进入时会崩溃。如果arraylist为空,则无法获得空白列表视图。
public contactoAdapter(Context context, ArrayList<contactoAgenda> datos) {
super(context, R.layout.listview_item, datos);
// Guardamos los par�metros en variables de clase.
this.context = context;
this.datos = datos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
contactoHolder holder;
if (item == null) {
item = LayoutInflater.from(context).inflate(R.layout.listview_item,
null);
holder = new contactoHolder();
holder.imgContacto = (ImageView) item.findViewById(R.id.imagenContacto);
holder.tvMail = (TextView) item.findViewById(R.id.tvMail);
holder.tvNombre = (TextView) item.findViewById(R.id.tvNombre);
// Almacenamos el holder en el Tag de la vista.
item.setTag(holder);
}
holder = (contactoHolder) item.getTag();
holder.imgContacto.setImageResource(datos.get(position).getDrawableImageID());
holder.tvNombre.setText(datos.get(position).getNombre());
holder.tvMail.setText(datos.get(position).getDireccion());
return item;
}
我用一张桌子中的所有联系人填充了arraylist。所以第一次表是空的,直到你填写一些联系人。
public ArrayList<contactoAgenda> recuperarTodosContactos() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<contactoAgenda> lista_contactos = new ArrayList<contactoAgenda>();
String[] valores_recuperar = {"nombre","direccion","telefono","email","miembrofacebook","miembrotwitter","miembrogoogle","miembrolinkedin","sexo","tipocontacto","imagen"};
Cursor c = db.query("contactos", valores_recuperar, null, null, null, null, null, null);
c.moveToFirst();
do {
contactoAgenda contactos = new contactoAgenda(c.getString(0), c.getString(1), c.getString(2), c.getString(3),c.getInt(4), c.getInt(5), c.getInt(6), c.getInt(7),c.getInt(8), c.getString(9), c.getInt(10));
lista_contactos.add(contactos);
} while (c.moveToNext());
db.close();
c.close();
return lista_contactos;
}
我把它称为使用全局变量,重要的是我得到一个联系人的arraylist显示在你的适配器。除特定情况外,每个案例都可以正常工作arraylist第一次是空的。我已尝试与联系人列表视图并删除它们,我可以将列表视图空白...不知道原因。如果arraylist为空,则必须显示空白列表视图:
AgendaGlobal.getInstance().miAgenda = BaseDatosGlobal.getInstance(this).agendaBaseDatos.recuperarTodosContactos();
Toast.makeText(getApplicationContext(),"Hasta aqui ya no", Toast.LENGTH_LONG).show();
adapter = new contactoAdapter(this, AgendaGlobal.getInstance().miAgenda);
lvContactos = (ListView) findViewById(R.id.lvItems);
// Asignamos el Adapter al ListView, en este punto hacemos que el
// ListView muestre los datos que queremos.
lvContactos.setAdapter(adapter);
logcat:请注意,在我在全局变量中有一个arraylist并且同样的事情发生之前...如果我得到一个空数据的arraylist它会崩溃。不是数据库唯一相关。
01-29 18:53:43.425: E/AndroidRuntime(1712): FATAL EXCEPTION: main
01-29 18:53:43.425: E/AndroidRuntime(1712): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.informacion/com.example.informacion.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.os.Looper.loop(Looper.java:137)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.ActivityThread.main(ActivityThread.java:4424)
01-29 18:53:43.425: E/AndroidRuntime(1712): at java.lang.reflect.Method.invokeNative(Native Method)
01-29 18:53:43.425: E/AndroidRuntime(1712): at java.lang.reflect.Method.invoke(Method.java:511)
01-29 18:53:43.425: E/AndroidRuntime(1712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-29 18:53:43.425: E/AndroidRuntime(1712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-29 18:53:43.425: E/AndroidRuntime(1712): at dalvik.system.NativeStart.main(Native Method)
01-29 18:53:43.425: E/AndroidRuntime(1712): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
01-29 18:53:43.425: E/AndroidRuntime(1712): at com.example.informacion.BaseDatosContactos.recuperarTodosContactos(BaseDatosContactos.java:295)
01-29 18:53:43.425: E/AndroidRuntime(1712): at com.example.informacion.MainActivity.onCreate(MainActivity.java:68)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.Activity.performCreate(Activity.java:4465)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-29 18:53:43.425: E/AndroidRuntime(1712): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-29 18:53:43.425: E/AndroidRuntime(1712): ... 11 more
答案 0 :(得分:1)
不确定问题是什么,你放在那里的代码确实没有显示问题,但有些事情可能会有所不同:
您应该在适配器中覆盖getCount(),getItem(int position)和getItemId(int position)。这很重要,getCount()方法告诉getView()它将调用多少次,如果list为空,则getView()永远不会被调用。在你的情况下应该是这样的:
@Override
public int getCount() {
return datos.size();
}
@Override
public String getItem(int position) {
return datos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
这与你的问题无关,但这是一个很好的做法:直接使用View convertView而不是创建一个新的View项目,它很好但是你正在使用内存实例化一个新的你不需要的项目。
这是一个最重要的建议:从数据库加载游标时请求getCount()&gt;在进行循环之前为0。像这样:
Cursor c = db.query("contactos", valores_recuperar, null, null, null, null, null, null);
if (c.getCount() > 0) {
c.moveToFirst();
while (cursor.moveToNext()) {
contactoAgenda contactos = new contactoAgenda(c.getString(0), c.getString(1), c.getString(2), c.getString(3),c.getInt(4), c.getInt(5), c.getInt(6), c.getInt(7),c.getInt(8), c.getString(9), c.getInt(10));
lista_contactos.add(contactos);
}
}
c.close();
不知道这对你有帮助,但要看一看。如果您发布更多信息(logcat,更多代码),我们可以准确地找到问题。希望它有所帮助
编辑:
ok log log你粘贴错误是在这一行: com.example.informacion.BaseDatosContactos.recuperarTodosContactos(BaseDatosContactos.java:295)
并且是一个CursorOutOfBounds错误,这个答案的第4点应该修复该错误。问题是你正在尝试创建一个循环并且你要求项目为空的游标,这就是为什么我告诉你当你将循环游标时要求计数高于0 ,对于那个问题你放在那里解决方案是替换你在recuperarContactos中的代码:
Cursor c = db.query("contactos", valores_recuperar, null, null, null, null, null, null);
if (c.getCount() > 0) {
c.moveToFirst();
while (cursor.moveToNext()) {
contactoAgenda contactos = new contactoAgenda(c.getString(0), c.getString(1), c.getString(2), c.getString(3),c.getInt(4), c.getInt(5), c.getInt(6), c.getInt(7),c.getInt(8), c.getString(9), c.getInt(10));
lista_contactos.add(contactos);
}
}
c.close();
答案 1 :(得分:0)
示例:在rellenarArrayList中,我不得不在miAgenda中放置一些联系人,这是contactoAgenda的ArrayList。如果arraylist为空,则适配器崩溃。所以你的注释在sqlite情况下很有用,但这里不是问题..我猜
rellenarArrayList();
adapter = new contactoAdapter(this, AgendaGlobal.getInstance().miAgenda);
lvContactos = (ListView) findViewById(R.id.lvItems);
// Asignamos el Adapter al ListView, en este punto hacemos que el
// ListView muestre los datos que queremos.
lvContactos.setAdapter(adapter);
// Asignamos el Listener al ListView para cuando pulsamos sobre uno de
// sus items.
以下是适用于自定义列表视图的适配器。
public contactoAdapter(Context context, ArrayList<contactoAgenda> datos) {
super(context, R.layout.listview_item, datos);
// Guardamos los par�metros en variables de clase.
this.context = context;
this.datos = datos;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
contactoHolder holder;
if (item == null) {
item = LayoutInflater.from(context).inflate(R.layout.listview_item,
null);
holder = new contactoHolder();
holder.imgContacto = (ImageView) item.findViewById(R.id.imagenContacto);
holder.tvMail = (TextView) item.findViewById(R.id.tvMail);
holder.tvNombre = (TextView) item.findViewById(R.id.tvNombre);
// Almacenamos el holder en el Tag de la vista.
item.setTag(holder);
}
holder = (contactoHolder) item.getTag();
holder.imgContacto.setImageResource(datos.get(position).getDrawableImageID());
holder.tvNombre.setText(datos.get(position).getNombre());
holder.tvMail.setText(datos.get(position).getDireccion());
return item;
}