显示数据库的内容在列表视图中的Android

问题描述:

我在我的数据库中的数据,我打算使用列表视图来显示给用户。这是功能我写的显示内容显示数据库的内容在列表视图中的Android

public class List_View extends ListActivity { 

ListView lv; 

Databasehelp db = new Databasehelp(this); 

public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    setContentView(R.layout.displayitems); 

    List<String> items = new ArrayList<String>(); 

    lv = (ListView)findViewById(android.R.id.list); 


    Cursor cursor = db.getAllTable1(); cursor.moveToFirst(); 
    //startManagingCursor(cursor); 

    lv.setAdapter(new ArrayAdapter<String>(this, 
      R.layout.displayitems, items)); 
     lv.setTextFilterEnabled(true); 

     ListAdapter adapter=new SimpleCursorAdapter(this, 
       R.layout.list_example_entry, cursor, 
       new String[] {"name"}, 
       new int[] {R.id.name_entry}); 
     setListAdapter(adapter); 
      }    
    } 

的布局displayitems.xml包含id为“清单”和list_example_entry.xml列表视图,其包含与该ID name_entry

displayitems.xml

<ListView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:id="@+id/list" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

</ListView> 
线性布局内一个TextView

list_example_entry

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
<TextView 
    android:id="@+id/name_entry" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="28dip" /> 
    </LinearLayout> 

可以在任何一个你能帮我解决这个问题呢?

+0

究竟是什么问题? –

+0

使用Activity扩展您的类...并阅读[it](http://www.vogella.com/articles/AndroidListView/article.html) –

+0

@Anand我不能使用setListAdapter,如果我只是用Activity来扩展 – ARK

您调用游标,然后尝试设置阵列适配器。然后光标适配器...

摆脱ArrayAdapter调用,这是没有必要的。当您将光标送入适配器时,您也不需要拨打moveToFirst(),适配器负责处理该问题。

它应该是这样的:

public void onCreate(Bundle icicle) 
{ 
    super.onCreate(icicle); 
    setContentView(R.layout.displayitems); 

    Cursor cursor = db.getAllTable1(); 
    startManagingCursor(cursor); 

    SimpleCusrorAdapter adapter = new SimpleCursorAdapter(this, 
      R.layout.list_example_entry, cursor, 
      new String[] {"name"}, 
      new int[] {R.id.name_entry}); 
    setListAdapter(adapter); 
} 

编辑

的错误意味着正是它说。当您使用ListActivity时,它期望您的列表的编号为@id/android:list。因此,改变你的ListView XML看起来像这样:

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:id="@id/android:list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</ListView> 

如果您的评论你的最后一个问题是有关你为什么没有做findViewById,即becasue您使用的是ListActivity,它使某些假设。最主要的是你的布局中有一个ListView,并且它具有上面提到的特定的ID(这就是为什么你有这个错误)。由于只有一个,它知道这个ID是什么,所以没有必要专门调出它。

+0

雅我意识到它的SimpleCursorAdapter并且如上所述进行了更改。但是,如果我检查我的日志猫,它给出以下错误 **引起:java.lang。 RuntimeException:你的容器必须有一个属性为'android.R.id.list'的列表视图** 并且请让我们知道上面的代码如何限制list_example_entry中的文本视图来列出displayitems中的视图? – ARK

+0

更新了我的答案。 – Barak

+0

这是因为你从来没有得到你的db处理程序的实例(或打开你的数据库)。在你尝试拉动游标之前,你必须做到这两点。它通常看起来像'yourDBHelper mDbHelper = new yourDBHelper(this);'后面是'mDbHelper.open();'。如果你有更多的问题,你应该发布一个新的问题。 – Barak