【问题标题】:How I can populate a ListView with a Cursor?如何用光标填充 ListView?
【发布时间】:2015-08-27 10:02:35
【问题描述】:

是否可以一次加载适配器中的所有项目?

我正在使用 CursorLoader、ListView 和 Cursor 适配器,适配器中的项目通过滚动加载。 我知道我只有几个简单的项目(大约 10 个),所以我没有性能问题

谢谢

【问题讨论】:

  • 您的实际问题是什么?
  • 如果您已经知道列表视图项目的内容,最好将其设置在数组列表中并使用它定义适配器。没有必要使用光标
  • @Karan Mer:我需要更改我的应用程序结构,因为您不知道有效的解决方案?不,谢谢,我的问题很清楚,我需要什么,谢谢
  • "my question is clear"其实这里没有问题……至少JoxTraex和我看不到
  • 如果你只有 10 个项目,我猜,创建视图并将它们动态添加到布局(可能是滚动视图)会比使用 ListView 更容易。

标签: android android-listview android-adapter android-cursoradapter android-cursorloader


【解决方案1】:

阅读此Populating-a-ListView-with-a-CursorAdapter 或此Android Cursor Example

如果你想使用 ArrayList(来自Populating a ListView using an ArrayList):

lv = (ListView) findViewById(R.id.your_list_view_id);

     // Instanciating an array list (you don't need to do this, 
     // you already have yours).
     List<String> your_array_list = new ArrayList<String>();
     your_array_list.add("foo");
     your_array_list.add("bar");

     // This is the array adapter, it takes the context of the activity as a 
     // first parameter, the type of list view as a second parameter and your 
     // array as a third parameter.
     ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
             this, 
             android.R.layout.simple_list_item_1,
             your_array_list );

     lv.setAdapter(arrayAdapter); 

【讨论】:

  • 谢谢,第二个链接中CursorAdapter的使用和我的一样。每次滚动列表视图时都会调用 bindView 方法,并加载来自光标的新数据。我需要的是(类似于)在第一次调用 bindView 时加载所有项目,我不知道 ListView 或 CursorAdapter 对象中是否有可用的东西
  • 您可以用光标的数据创建一个List并使用这个List。我已经编辑了我的答案。看看吧。
  • 有用,类似的东西是我的实际解决方案,但这样我需要扫描光标数据两次,一次填充列表,另一次扫描将从 CursorAdapter 执行...我是确定我们可以做得更好,只使用 CursorAdapter 扫描 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
相关资源
最近更新 更多