2012-01-03 50 views
0

我是Android新手。我正在使用來自sqlite數據庫的數據創建列表視圖時遇到問題。 我發現下面這段代碼來創建聯繫人數據的列表視圖來自數據庫的Android Listview

public class Test extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Get a cursor with all people 
     Cursor c = getContentResolver().query(Contacts.CONTENT_URI,CONTACT_PROJECTION, null, null, null); 
     startManagingCursor(c); 

     ListAdapter adapter = new SimpleCursorAdapter(this, 
       // Use a template that displays a text view 
       android.R.layout.simple_list_item_1, 
       // Give the cursor to the list adatper 
       c, 
       // Map the NAME column in the people database to... 
       new String[] {Contacts.DISPLAY_NAME}, 
       // The "text1" view defined in the XML template 
       new int[] {android.R.id.text1}); 
     setListAdapter(adapter); 
    } 

    private static final String[] CONTACT_PROJECTION = new String[] { 
     Contacts._ID, 
     Contacts.DISPLAY_NAME 
    }; 
} 

現在我用這個代碼從數據庫

SQLiteDatabase myDB= null; 
    String TableName = "myTable"; 
    myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null); 

    // Get a cursor with all people 
    Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null); 

現在拉的數據我怎麼能結合這兩種代碼,以便我可以顯示數據從此列表中的數據庫中拉出。

在此先感謝。

回答

0

如果你想列出來自兩個來源(聯繫人和數據庫)的數據,我認爲你只有選擇是構建一個數組,其中包含來自兩個來源的返回值。

List finalList = new ArrayList(); 
Cursor c = getContentResolver().query(Contacts.CONTENT_URI,CONTACT_PROJECTION, null, null, null); 
     startManagingCursor(c); 
c.moveToFirst(); 

迭代這個光標,並填充字符串值到finalList

SQLiteDatabase myDB= null; 
    String TableName = "myTable"; 
    myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null); 

    // Get a cursor with all people 
    Cursor c2 = myDB.rawQuery("SELECT * FROM " + TableName , null); 
    startManagingCursor(c2); 
c2.moveToFirst(); 

迭代C2光標並添加值finalList。通過列表

轉換finalList到陣列通過迭代finalList

String[] ipArray = new String[finalList.size()]; 

環和填充值來ipArray。

我沒有IDE方便,所以我輸入了流程。

+0

我只需要使用DB數據代碼。那麼代碼是什麼? – 2012-01-04 00:10:00

+0

如果是這樣的情況下,只需複製數據庫的代碼在你的問題並更換光標C = getcontentaresolver(......) – kosa 2012-01-04 00:13:47

+0

我問這個嘗試過question.But其示值誤差 這裏檢查日誌文件 HTTP: //polbd.com/log.txt – 2012-01-04 00:34:34

相關問題