ListView的空当SimpleCursorAdapter关闭() [英] ListView empty if SimpleCursorAdapter closed()

查看:203
本文介绍了ListView的空当SimpleCursorAdapter关闭()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题理解为什么我的ListActivity和其相应的的ListView 的空当我打电话的关闭()方式上的光标的对象吗?

让我解释一下我自己有点...

我retreive从数据库的一些价值观和得到的结果在我的光标对象。从那以后,我创造我的 SimpleCursorAdapter 的并绑定列从数据库有一个字段在我的的ListView 的。

工程就像一个魅力,但是...

如果我称之为 cursor.close()的onCreate()的方法,我的ListView显示空结束了吗?

如果我登录值从光标到LogCat中他们在那里,直到调用的 cursor.close()的,而且非常有意义的,但为什么是listAdapter空当的 cursor.close( )的被称为??? 我期望的 ListAdapter listAdapter =新SimpleCursorAdapter(...)的举办被绑定的值的的ListView 的,直到我们的活动被摧毁...

为什么会这样呢?什么时候和为什么要关闭游标?

 公共类ListTrainingsView扩展ListActivity {
私人SQLiteDatabase分贝;
私人ListAdapter listAdapter;
私人光标指针;

@覆盖
公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    的setContentView(R.layout.list_trainings);

    DatabaseHelper帮手=新DatabaseHelper(本);

    this.db = helper.getWritableDatabase();

    this.cursor = db.query(培训,字段,NULL,NULL,NULL,NULL,NULL);

    startManagingCursor(光标);

    this.listAdapter =新SimpleCursorAdapter(这一点,
            R.layout.list_trainings_item,
            光标,
            新的String [] {姓名},
            新的INT [] {} R.id.name_entry
    );

    this.setListAdapter(this.listAdapter);

//如果我打电话
//cursor.close();
//显示列表为空
 

另一个问题是,更多的问题,基本的Java语言类型......我来自PHP的面向对象的背景和那里,如果你定义成员变量,你必须与它的对象的方法使用语法工作的 $这个 - >光标。我注意到,在Android的/ Java的,我没有使用的 this.cursor.getCount()的,以从中得到参考/值。这足以说的 cursor.getCount()的如何走到这是允许的?

解决方案
  

为什么会这样呢?

适配器需要的数据提供了光标和prepare 的ListView 因此,当你调用 cursor.close()光标被解除,无效。 (装置没有数据。)

  

当,为什么要关闭游标?

有必要关闭光标,而你即将离开活动和回去的活动否则光标得到了泄漏为活动

  

这足以说cursor.getCount()如何走到这是允许的?

爪哇的结构是基于类。每一个动作都是一个特定的类内完成。即使呼应单行需要一个类。而当你在一个类中,您可以访问类成员与 直接的变量

I have a problem understanding why is my ListActivity and its corresponding listView empty when I call close() method on cursor object?

Let me explain myself a bit...

I retreive some values from a DB and get the result in my cursor object. After that I create my SimpleCursorAdapter and bind column from db with a field in my listView.

Works like a charm, but...

If I call cursor.close() at the end of onCreate() method my listView is shown empty?

If I log the values from cursor to LogCat they're there until calling cursor.close() and that makes perfect sense, but why is the listAdapter emptied when cursor.close() is called??? I would expect ListAdapter listAdapter = new SimpleCursorAdapter(...) to hold the values that are "binded" to a listView until we activity is destroyed...

Why is this so? When and why is necessary to close cursor?

public class ListTrainingsView extends ListActivity{
private SQLiteDatabase db;
private ListAdapter  listAdapter;
private Cursor cursor;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_trainings);

    DatabaseHelper helper = new DatabaseHelper(this);

    this.db = helper.getWritableDatabase();

    this.cursor = db.query("trainings", fields, null, null, null, null, null);

    startManagingCursor(cursor);

    this.listAdapter = new SimpleCursorAdapter(this,
            R.layout.list_trainings_item, 
            cursor, 
            new String[] {"name"},
            new int[] { R.id.name_entry}
    );

    this.setListAdapter(this.listAdapter);

//If I call 
//cursor.close();
//shown list is empty

Another question is more of basic Java language type of question... I come from PHP OO background and there if you define member variable you have to work with it in object methods using syntax $this->cursor. I've noticed that in Android/Java I don't have to use this.cursor.getCount() to get the reference/value from it. It's enough to say cursor.getCount() How come this is allowed?

解决方案

Why is this so?

Adapter needs data from provided Cursor and prepare ListView accordingly, and when you call cursor.close() the Cursor is released and made invalid. (means there is no data.)

When and why is necessary to close cursor?

It is necessary to close the Cursor while you are about to leave the Activity and going back from the Activity otherwise Cursor got leaked for that Activity.

It's enough to say cursor.getCount() How come this is allowed?

The structure of Java is based on classes. Every action is done within a specific class. Even echoing "single line" would need a class. and when you are in a class you can access class members with this OR with direct their variables

这篇关于ListView的空当SimpleCursorAdapter关闭()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆