2012-01-25 58 views
1

我是新来的android和经历了许多教程后,我仍然无法得到的东西。在ListView中显示数据库

我已经创建了一个数据库,现在正在阅读这个数据库。 此数据库和3列所有三种字符串类型。

现在我想在R.layout.viewDatabase

使用ListView.I有这个ListView ID为“listDatabaseItems”创建,以显示该数据库目前,我的代码是:

Cursor c = sqliteDatabase.query(MY_DATABASE_TABLE, null, null, null, null, null, null); 

int firstNameColumn = c.getColumnIndex("name_column"); 
int protectedColumn = c.getColumnIndex("protected_id_column"); 

ArrayList<String> results = new ArrayList<String>(); 
int i = 0; 

c.moveToFirst(); 
do { 
    i++; 
    String firstName = c.getString(firstNameColumn); 
    String protectedID = c.getString(protectedColumn); 
    results.add(firstName + protectedID); // NEED TO add here 
} while (c.moveToNext()); 

sqliteDatabase.close(); 

请告诉我,我做什么样的变化以及如何在ListView中显示我的数据库。

最好的问候。

+0

请注明正确,你所面对的问题......? –

+0

我的代码缺少详细信息,如何将结果数组链接到我的ListView。 –

+0

我认为你在通过游标迭代时遇到问题......? –

回答

4

做一个AdapterListArray值添加到它,然后setAdapter到你的ListView简单,

你的代码后,补充一点,两线,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, results); 
mListView.setAdapter(adapter); 

而且,你的问题听起来很基本的Android所以只要看看这些链接..

SimpleCursorAdapters and ListViews

Android SQLite Database and ContentProvider - Tutorial

Filling the Layout with Data

+0

语法错了。它不接受结果。你确定ArrayAdapter ? –

+0

'mListView。setAdapter(adapter);'(其中mListView是ListView的一个实例,而不是ListView类) - 或者,如果您在ListActivity的子类中,则可以使用getListView()。setAdapter(adapter) ;' – dokkaebi

+0

感谢您的帮助,它的工作,但我如何列出数据到不同的列,而不是将它们列入一行? –

0

由于您使用的首位光标,其实我有SimpleCursorAdapter去。 CursorAdapter允许您避免将整个数据集加载到内存中,从而加快初始加载时间并避免大数据集的大量分配。

要扩展本文的基本设置(SimpleCursorAdapters and ListViews),您还需要设置视图活页夹以自定义数据绑定到视图的方式,即。连接你的两个字符串。

事情是这样的:

SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() { 
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      //you're only using one view, so we can ignore columnIndex 
      TextView text = view.findViewById(R.id.list_item_text); 

      //the cursor is already in position for this list item 
      int firstNameColumn = c.getColumnIndex("name_column"); 
      int protectedColumn = c.getColumnIndex("protected_id_column"); 
      String firstName = c.getString(firstNameColumn); 
      String protectedID = c.getString(protectedColumn); 
      text.setText(firstName+protectedID); 
      return true; //we have handled binding the data 
     } 
}; 

然后,当你设置你的适配器,你通过它可以用来为每个项目的布局。布局应该包含一个TextView来保存您想要显示的文本项目。您可以使用android.R.layout.simple_list_item_1作为user370305的建议,但我不确定它的TextView的id是什么 - 它可能是android.R.id.text1

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    context,   //can be your Activity 
    R.layout.list_item, //xml layout containing textview with id 'list_item_text' 
    c,     //the cursor 
    new String[] {"name_column"}, //the column to read (we'll do more in the binder) 
    new int[] {R.id.list_item_text}, //the view to hold the data 
    0 //no flags 
); 
adapter.setViewBinder(binder);  

它添加到您的ListView以同样的方式:

ListView listView = //your ListView reference 
listView.setAdapter(adapter); 

//or in a ListActivity: 
setListAdapter(adapter); 
相关问题