【问题标题】:cursor to listview android 2.2+光标到listview android 2.2+
【发布时间】:2013-09-05 19:47:13
【问题描述】:

我试图从光标创建一个列表视图已经好几个小时了,但它没有工作,这真的让我很不高兴!!我有一个充满所需数据的光标,我需要获取这些数据并将它们添加到我的列表视图中我创建了一个从列表活动扩展的活动,并将以下代码放入其中:

ListView listv=(ListView)findViewById(R.id.ListView1);
        Cursor cu;              
        cu=myDbHelper.GetCursor();   

        ListAdapter adapter =new SimpleCursorAdapter(this,R.layout.rating,cu,new String[]{"Title"}, new int[]{R.id.listText},flag_that_I_dont_know_how_to_use);   
        listv.setAdapter(adapter);
        myDbHelper.close();
        cu.close();

我不知道如何使用该标志,并且不推荐使用 simplecursoradapter 的另一个构造函数,我有两种布局,一种是包含单个文本视图的评级布局,一种是包含列表视图的布局......我没有得到我想要什么我得到一个空页面或者一个错误取决于使用的标志......请帮助!

编辑1:

我将 listview1 替换为 andriod:id/list 并在我的代码中写道:

Cursor cu;              
        cu=myDbHelper.GetCursor();   
        SimpleCursorAdapter adapter =new SimpleCursorAdapter(this,R.layout.rating,cu,new String[]{"Title"}, new int[]{R.id.listText},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);   
        setListAdapter(adapter);

这给了我一个空白页;

编辑 有人有什么主意吗?或者有没有人可以查看任何示例,看看为什么我的列表视图不起作用..?

【问题讨论】:

    标签: android android-listview adapter


    【解决方案1】:

    试试这个代码

    ListView listv=(ListView)findViewById(R.id.ListView1);
                    Cursor cu;              
                    cu=myDbHelper.GetCursor();   
                    SimpleCursorAdapter adapter =new SimpleCursorAdapter(this,R.layout.rating,cu,new String[]{"Title"}, new int[]{R.id.listText},0);   
                    listv.setAdapter(adapter);
                    myDbHelper.close();
                    cu.close();
    

    【讨论】:

    • 嘿,谢谢,但我已经这样做了,我得到了:java.lang.RuntimeException:您的内容必须有一个 id 属性为 'android.R.id.list' 的 ListView,尽管当我使用列表时从 android 预定义我得到一个空页面...
    【解决方案2】:

    所以 edit1 工作的标志是: CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER 我只需要删除 cu.close();当我关闭光标时,我有一个白页,因为该页面正在从光标中获取其内容,而当它关闭时,该页面就没有更多内容了! 我删除了扩展列表视图,不需要我添加扩展活动......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多